diff options
author | Eric Blake | 2020-10-27 06:05:47 +0100 |
---|---|---|
committer | Eric Blake | 2020-10-30 21:10:14 +0100 |
commit | 9812e7125b83ba6e4645237150ca5f61bf66197f (patch) | |
tree | fc381c3afbee4152574578ba08e157310c8fb613 /include/qapi | |
parent | block: Simplify QAPI_LIST_ADD (diff) | |
download | qemu-9812e7125b83ba6e4645237150ca5f61bf66197f.tar.gz qemu-9812e7125b83ba6e4645237150ca5f61bf66197f.tar.xz qemu-9812e7125b83ba6e4645237150ca5f61bf66197f.zip |
qapi: Add QAPI_LIST_PREPEND() macro
block.c has a useful macro QAPI_LIST_ADD() for inserting at the front
of any QAPI-generated list; move it from block.c to qapi/util.h so
more places can use it, including one earlier place in block.c, and
rename it to something more obvious (since we also have a lot of
places that append, rather than prepend, to a list).
There are many more places in the codebase that can benefit from using
the macro, but converting them will be left to later patches.
In theory, all QAPI list types are child classes of GenericList; but
in practice, that relationship is not explicitly spelled out in the C
type declarations (rather, it is something that happens implicitly due
to C compatible layouts), and the macro does not actually depend on
the GenericList type. We considered moving GenericList from visitor.h
into util.h to group related code; however, such a move would be
awkward if we do not also move GenericAlternate. Unfortunately,
moving GenericAlternate would introduce its own problems of
declaration circularity (qapi-builtin-types.h needs a complete
definition of QEnumLookup from util.h, but GenericAlternate needs a
complete definition of QType from qapi-builtin-types.h).
Suggested-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20201027050556.269064-3-eblake@redhat.com>
[eblake: s/ADD/PREPEND/ per suggestion by Markus]
Diffstat (limited to 'include/qapi')
-rw-r--r-- | include/qapi/util.h | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/include/qapi/util.h b/include/qapi/util.h index a7c3c64148..bc312e90aa 100644 --- a/include/qapi/util.h +++ b/include/qapi/util.h @@ -22,4 +22,17 @@ int qapi_enum_parse(const QEnumLookup *lookup, const char *buf, int parse_qapi_name(const char *name, bool complete); +/* + * For any GenericList @list, insert @element at the front. + * + * Note that this macro evaluates @element exactly once, so it is safe + * to have side-effects with that argument. + */ +#define QAPI_LIST_PREPEND(list, element) do { \ + typeof(list) _tmp = g_malloc(sizeof(*(list))); \ + _tmp->value = (element); \ + _tmp->next = (list); \ + (list) = _tmp; \ +} while (0) + #endif |