qemu/include/qapi/util.h
Paolo Bonzini eb350d1d01 include: move typeof_strip_qual to compiler.h, use it in QAPI_LIST_LENGTH()
The typeof_strip_qual() is most useful for the atomic fetch-and-modify
operations in atomic.h, but it can be used elsewhere as well.  For example,
QAPI_LIST_LENGTH() assumes that the argument is not const, which is not a
requirement.

Move the macro to compiler.h and, while at it, move it under #ifndef
__cplusplus to emphasize that it uses C-only constructs.  A C++ version
of typeof_strip_qual() using type traits is possible[1], but beyond the
scope of this patch because the little C++ code that is in QEMU does not
use QAPI.

The patch was tested by changing the declaration of strv_from_str_list()
in qapi/qapi-type-helpers.c to:

    char **strv_from_str_list(const strList *const list)

This is valid C code, and it fails to compile without this change.

[1] https://lore.kernel.org/qemu-devel/20240624205647.112034-1-flwu@google.com/

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Tested-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2024-06-28 14:44:52 +02:00

73 lines
2.2 KiB
C

/*
* 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);
/*
* 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_strip_qual(list) _tail; \
for (_tail = list; _tail != NULL; _tail = _tail->next) { \
_len++; \
} \
_len; \
})
#endif