2
0

qlist.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * QList Module
  3. *
  4. * Copyright (C) 2009 Red Hat Inc.
  5. *
  6. * Authors:
  7. * Luiz Capitulino <lcapitulino@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  10. * See the COPYING.LIB file in the top-level directory.
  11. */
  12. #include "qlist.h"
  13. #include "qobject.h"
  14. #include "qemu-queue.h"
  15. #include "qemu-common.h"
  16. static void qlist_destroy_obj(QObject *obj);
  17. static const QType qlist_type = {
  18. .code = QTYPE_QLIST,
  19. .destroy = qlist_destroy_obj,
  20. };
  21. /**
  22. * qlist_new(): Create a new QList
  23. *
  24. * Return strong reference.
  25. */
  26. QList *qlist_new(void)
  27. {
  28. QList *qlist;
  29. qlist = g_malloc(sizeof(*qlist));
  30. QTAILQ_INIT(&qlist->head);
  31. QOBJECT_INIT(qlist, &qlist_type);
  32. return qlist;
  33. }
  34. static void qlist_copy_elem(QObject *obj, void *opaque)
  35. {
  36. QList *dst = opaque;
  37. qobject_incref(obj);
  38. qlist_append_obj(dst, obj);
  39. }
  40. QList *qlist_copy(QList *src)
  41. {
  42. QList *dst = qlist_new();
  43. qlist_iter(src, qlist_copy_elem, dst);
  44. return dst;
  45. }
  46. /**
  47. * qlist_append_obj(): Append an QObject into QList
  48. *
  49. * NOTE: ownership of 'value' is transferred to the QList
  50. */
  51. void qlist_append_obj(QList *qlist, QObject *value)
  52. {
  53. QListEntry *entry;
  54. entry = g_malloc(sizeof(*entry));
  55. entry->value = value;
  56. QTAILQ_INSERT_TAIL(&qlist->head, entry, next);
  57. }
  58. /**
  59. * qlist_iter(): Iterate over all the list's stored values.
  60. *
  61. * This function allows the user to provide an iterator, which will be
  62. * called for each stored value in the list.
  63. */
  64. void qlist_iter(const QList *qlist,
  65. void (*iter)(QObject *obj, void *opaque), void *opaque)
  66. {
  67. QListEntry *entry;
  68. QTAILQ_FOREACH(entry, &qlist->head, next)
  69. iter(entry->value, opaque);
  70. }
  71. QObject *qlist_pop(QList *qlist)
  72. {
  73. QListEntry *entry;
  74. QObject *ret;
  75. if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
  76. return NULL;
  77. }
  78. entry = QTAILQ_FIRST(&qlist->head);
  79. QTAILQ_REMOVE(&qlist->head, entry, next);
  80. ret = entry->value;
  81. g_free(entry);
  82. return ret;
  83. }
  84. QObject *qlist_peek(QList *qlist)
  85. {
  86. QListEntry *entry;
  87. QObject *ret;
  88. if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
  89. return NULL;
  90. }
  91. entry = QTAILQ_FIRST(&qlist->head);
  92. ret = entry->value;
  93. return ret;
  94. }
  95. int qlist_empty(const QList *qlist)
  96. {
  97. return QTAILQ_EMPTY(&qlist->head);
  98. }
  99. static void qlist_size_iter(QObject *obj, void *opaque)
  100. {
  101. size_t *count = opaque;
  102. (*count)++;
  103. }
  104. size_t qlist_size(const QList *qlist)
  105. {
  106. size_t count = 0;
  107. qlist_iter(qlist, qlist_size_iter, &count);
  108. return count;
  109. }
  110. /**
  111. * qobject_to_qlist(): Convert a QObject into a QList
  112. */
  113. QList *qobject_to_qlist(const QObject *obj)
  114. {
  115. if (qobject_type(obj) != QTYPE_QLIST) {
  116. return NULL;
  117. }
  118. return container_of(obj, QList, base);
  119. }
  120. /**
  121. * qlist_destroy_obj(): Free all the memory allocated by a QList
  122. */
  123. static void qlist_destroy_obj(QObject *obj)
  124. {
  125. QList *qlist;
  126. QListEntry *entry, *next_entry;
  127. assert(obj != NULL);
  128. qlist = qobject_to_qlist(obj);
  129. QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
  130. QTAILQ_REMOVE(&qlist->head, entry, next);
  131. qobject_decref(entry->value);
  132. g_free(entry);
  133. }
  134. g_free(qlist);
  135. }