util.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * QAPI util functions
  3. *
  4. * Copyright Fujitsu, Inc. 2014
  5. *
  6. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  7. * See the COPYING.LIB file in the top-level directory.
  8. *
  9. */
  10. #ifndef QAPI_UTIL_H
  11. #define QAPI_UTIL_H
  12. typedef enum {
  13. QAPI_DEPRECATED,
  14. QAPI_UNSTABLE,
  15. } QapiSpecialFeature;
  16. typedef struct QEnumLookup {
  17. const char *const *array;
  18. const uint64_t *const features;
  19. const int size;
  20. } QEnumLookup;
  21. const char *qapi_enum_lookup(const QEnumLookup *lookup, int val);
  22. int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
  23. int def, Error **errp);
  24. bool qapi_bool_parse(const char *name, const char *value, bool *obj,
  25. Error **errp);
  26. int parse_qapi_name(const char *name, bool complete);
  27. /*
  28. * For any GenericList @list, insert @element at the front.
  29. *
  30. * Note that this macro evaluates @element exactly once, so it is safe
  31. * to have side-effects with that argument.
  32. */
  33. #define QAPI_LIST_PREPEND(list, element) do { \
  34. typeof(list) _tmp = g_malloc(sizeof(*(list))); \
  35. _tmp->value = (element); \
  36. _tmp->next = (list); \
  37. (list) = _tmp; \
  38. } while (0)
  39. /*
  40. * For any pointer to a GenericList @tail (usually the 'next' member of a
  41. * list element), insert @element at the back and update the tail.
  42. *
  43. * Note that this macro evaluates @element exactly once, so it is safe
  44. * to have side-effects with that argument.
  45. */
  46. #define QAPI_LIST_APPEND(tail, element) do { \
  47. *(tail) = g_malloc0(sizeof(**(tail))); \
  48. (*(tail))->value = (element); \
  49. (tail) = &(*(tail))->next; \
  50. } while (0)
  51. /*
  52. * For any GenericList @list, return its length.
  53. */
  54. #define QAPI_LIST_LENGTH(list) \
  55. ({ \
  56. size_t _len = 0; \
  57. typeof_strip_qual(list) _tail; \
  58. for (_tail = list; _tail != NULL; _tail = _tail->next) { \
  59. _len++; \
  60. } \
  61. _len; \
  62. })
  63. #endif