1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "qapi/qmp/qlist.h"
15#include "qapi/qmp/qobject.h"
16#include "qemu/queue.h"
17#include "qemu-common.h"
18
19
20
21
22
23
24QList *qlist_new(void)
25{
26 QList *qlist;
27
28 qlist = g_malloc(sizeof(*qlist));
29 qobject_init(QOBJECT(qlist), QTYPE_QLIST);
30 QTAILQ_INIT(&qlist->head);
31
32 return qlist;
33}
34
35static void qlist_copy_elem(QObject *obj, void *opaque)
36{
37 QList *dst = opaque;
38
39 qobject_incref(obj);
40 qlist_append_obj(dst, obj);
41}
42
43QList *qlist_copy(QList *src)
44{
45 QList *dst = qlist_new();
46
47 qlist_iter(src, qlist_copy_elem, dst);
48
49 return dst;
50}
51
52
53
54
55
56
57void qlist_append_obj(QList *qlist, QObject *value)
58{
59 QListEntry *entry;
60
61 entry = g_malloc(sizeof(*entry));
62 entry->value = value;
63
64 QTAILQ_INSERT_TAIL(&qlist->head, entry, next);
65}
66
67
68
69
70
71
72
73void qlist_iter(const QList *qlist,
74 void (*iter)(QObject *obj, void *opaque), void *opaque)
75{
76 QListEntry *entry;
77
78 QTAILQ_FOREACH(entry, &qlist->head, next)
79 iter(entry->value, opaque);
80}
81
82QObject *qlist_pop(QList *qlist)
83{
84 QListEntry *entry;
85 QObject *ret;
86
87 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
88 return NULL;
89 }
90
91 entry = QTAILQ_FIRST(&qlist->head);
92 QTAILQ_REMOVE(&qlist->head, entry, next);
93
94 ret = entry->value;
95 g_free(entry);
96
97 return ret;
98}
99
100QObject *qlist_peek(QList *qlist)
101{
102 QListEntry *entry;
103
104 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
105 return NULL;
106 }
107
108 entry = QTAILQ_FIRST(&qlist->head);
109
110 return entry->value;
111}
112
113int qlist_empty(const QList *qlist)
114{
115 return QTAILQ_EMPTY(&qlist->head);
116}
117
118static void qlist_size_iter(QObject *obj, void *opaque)
119{
120 size_t *count = opaque;
121 (*count)++;
122}
123
124size_t qlist_size(const QList *qlist)
125{
126 size_t count = 0;
127 qlist_iter(qlist, qlist_size_iter, &count);
128 return count;
129}
130
131
132
133
134QList *qobject_to_qlist(const QObject *obj)
135{
136 if (!obj || qobject_type(obj) != QTYPE_QLIST) {
137 return NULL;
138 }
139 return container_of(obj, QList, base);
140}
141
142
143
144
145void qlist_destroy_obj(QObject *obj)
146{
147 QList *qlist;
148 QListEntry *entry, *next_entry;
149
150 assert(obj != NULL);
151 qlist = qobject_to_qlist(obj);
152
153 QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
154 QTAILQ_REMOVE(&qlist->head, entry, next);
155 qobject_decref(entry->value);
156 g_free(entry);
157 }
158
159 g_free(qlist);
160}
161