qemu/include/qapi/util.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.2 KiB
C
Raw Normal View History

/*
* QAPI util functions
*
* Copyright Fujitsu, Inc. 2014
*
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
* See the COPYING.LIB file in the top-level directory.
*
*/
#ifndef QAPI_UTIL_H
#define QAPI_UTIL_H
typedef enum {
QAPI_DEPRECATED,
QAPI_UNSTABLE,
} QapiSpecialFeature;
typedef struct QEnumLookup {
const char *const *array;
const unsigned char *const special_features;
const int size;
} QEnumLookup;
const char *qapi_enum_lookup(const QEnumLookup *lookup, int val);
int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
int def, Error **errp);
bool qapi_bool_parse(const char *name, const char *value, bool *obj,
Error **errp);
int parse_qapi_name(const char *name, bool complete);
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]
2020-10-27 08:05:47 +03:00
/*
* 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)
/*
* For any pointer to a GenericList @tail (usually the 'next' member of a
* list element), insert @element at the back and update the tail.
*
* Note that this macro evaluates @element exactly once, so it is safe
* to have side-effects with that argument.
*/
#define QAPI_LIST_APPEND(tail, element) do { \
*(tail) = g_malloc0(sizeof(**(tail))); \
(*(tail))->value = (element); \
(tail) = &(*(tail))->next; \
} while (0)
/*
* For any GenericList @list, return its length.
*/
#define QAPI_LIST_LENGTH(list) \
({ \
size_t _len = 0; \
typeof(list) _tail; \
for (_tail = list; _tail != NULL; _tail = _tail->next) { \
_len++; \
} \
_len; \
})
#endif