2
0

qobject.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * QEMU Object Model.
  3. *
  4. * Based on ideas by Avi Kivity <avi@redhat.com>
  5. *
  6. * Copyright (C) 2009 Red Hat Inc.
  7. *
  8. * Authors:
  9. * Luiz Capitulino <lcapitulino@redhat.com>
  10. *
  11. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  12. * See the COPYING.LIB file in the top-level directory.
  13. *
  14. * QObject Reference Counts Terminology
  15. * ------------------------------------
  16. *
  17. * - Returning references: A function that returns an object may
  18. * return it as either a weak or a strong reference. If the reference
  19. * is strong, you are responsible for calling QDECREF() on the reference
  20. * when you are done.
  21. *
  22. * If the reference is weak, the owner of the reference may free it at
  23. * any time in the future. Before storing the reference anywhere, you
  24. * should call QINCREF() to make the reference strong.
  25. *
  26. * - Transferring ownership: when you transfer ownership of a reference
  27. * by calling a function, you are no longer responsible for calling
  28. * QDECREF() when the reference is no longer needed. In other words,
  29. * when the function returns you must behave as if the reference to the
  30. * passed object was weak.
  31. */
  32. #ifndef QOBJECT_H
  33. #define QOBJECT_H
  34. #include <stddef.h>
  35. #include <assert.h>
  36. typedef enum {
  37. QTYPE_NONE,
  38. QTYPE_QINT,
  39. QTYPE_QSTRING,
  40. QTYPE_QDICT,
  41. QTYPE_QLIST,
  42. QTYPE_QFLOAT,
  43. QTYPE_QBOOL,
  44. QTYPE_QERROR,
  45. } qtype_code;
  46. struct QObject;
  47. typedef struct QType {
  48. qtype_code code;
  49. void (*destroy)(struct QObject *);
  50. } QType;
  51. typedef struct QObject {
  52. const QType *type;
  53. size_t refcnt;
  54. } QObject;
  55. /* Objects definitions must include this */
  56. #define QObject_HEAD \
  57. QObject base
  58. /* Get the 'base' part of an object */
  59. #define QOBJECT(obj) (&(obj)->base)
  60. /* High-level interface for qobject_incref() */
  61. #define QINCREF(obj) \
  62. qobject_incref(QOBJECT(obj))
  63. /* High-level interface for qobject_decref() */
  64. #define QDECREF(obj) \
  65. qobject_decref(QOBJECT(obj))
  66. /* Initialize an object to default values */
  67. #define QOBJECT_INIT(obj, qtype_type) \
  68. obj->base.refcnt = 1; \
  69. obj->base.type = qtype_type
  70. /**
  71. * qobject_incref(): Increment QObject's reference count
  72. */
  73. static inline void qobject_incref(QObject *obj)
  74. {
  75. if (obj)
  76. obj->refcnt++;
  77. }
  78. /**
  79. * qobject_decref(): Decrement QObject's reference count, deallocate
  80. * when it reaches zero
  81. */
  82. static inline void qobject_decref(QObject *obj)
  83. {
  84. if (obj && --obj->refcnt == 0) {
  85. assert(obj->type != NULL);
  86. assert(obj->type->destroy != NULL);
  87. obj->type->destroy(obj);
  88. }
  89. }
  90. /**
  91. * qobject_type(): Return the QObject's type
  92. */
  93. static inline qtype_code qobject_type(const QObject *obj)
  94. {
  95. assert(obj->type != NULL);
  96. return obj->type->code;
  97. }
  98. #endif /* QOBJECT_H */