]>
Commit | Line | Data |
---|---|---|
a6fd08eb | 1 | /* |
41836a9f | 2 | * QList Module |
a6fd08eb LC |
3 | * |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <[email protected]> | |
8 | * | |
41836a9f LC |
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. | |
a6fd08eb | 11 | */ |
41836a9f | 12 | |
f2ad72b3 | 13 | #include "qemu/osdep.h" |
7b1b5d19 PB |
14 | #include "qapi/qmp/qlist.h" |
15 | #include "qapi/qmp/qobject.h" | |
1de7afc9 | 16 | #include "qemu/queue.h" |
a6fd08eb LC |
17 | #include "qemu-common.h" |
18 | ||
a6fd08eb LC |
19 | /** |
20 | * qlist_new(): Create a new QList | |
21 | * | |
22 | * Return strong reference. | |
23 | */ | |
24 | QList *qlist_new(void) | |
25 | { | |
26 | QList *qlist; | |
27 | ||
7267c094 | 28 | qlist = g_malloc(sizeof(*qlist)); |
55e1819c | 29 | qobject_init(QOBJECT(qlist), QTYPE_QLIST); |
a6fd08eb | 30 | QTAILQ_INIT(&qlist->head); |
a6fd08eb LC |
31 | |
32 | return qlist; | |
33 | } | |
34 | ||
033815fe AL |
35 | static 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 | ||
43 | QList *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 | ||
a6fd08eb LC |
52 | /** |
53 | * qlist_append_obj(): Append an QObject into QList | |
54 | * | |
55 | * NOTE: ownership of 'value' is transferred to the QList | |
56 | */ | |
57 | void qlist_append_obj(QList *qlist, QObject *value) | |
58 | { | |
59 | QListEntry *entry; | |
60 | ||
7267c094 | 61 | entry = g_malloc(sizeof(*entry)); |
a6fd08eb LC |
62 | entry->value = value; |
63 | ||
64 | QTAILQ_INSERT_TAIL(&qlist->head, entry, next); | |
65 | } | |
66 | ||
67 | /** | |
68 | * qlist_iter(): Iterate over all the list's stored values. | |
69 | * | |
70 | * This function allows the user to provide an iterator, which will be | |
71 | * called for each stored value in the list. | |
72 | */ | |
73 | void 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 | ||
033815fe AL |
82 | QObject *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; | |
7267c094 | 95 | g_free(entry); |
033815fe AL |
96 | |
97 | return ret; | |
98 | } | |
99 | ||
100 | QObject *qlist_peek(QList *qlist) | |
101 | { | |
102 | QListEntry *entry; | |
033815fe AL |
103 | |
104 | if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) { | |
105 | return NULL; | |
106 | } | |
107 | ||
108 | entry = QTAILQ_FIRST(&qlist->head); | |
109 | ||
9be38598 | 110 | return entry->value; |
033815fe AL |
111 | } |
112 | ||
113 | int qlist_empty(const QList *qlist) | |
114 | { | |
115 | return QTAILQ_EMPTY(&qlist->head); | |
116 | } | |
117 | ||
a86a4c2f MR |
118 | static void qlist_size_iter(QObject *obj, void *opaque) |
119 | { | |
120 | size_t *count = opaque; | |
121 | (*count)++; | |
122 | } | |
123 | ||
124 | size_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 | ||
a6fd08eb LC |
131 | /** |
132 | * qobject_to_qlist(): Convert a QObject into a QList | |
133 | */ | |
134 | QList *qobject_to_qlist(const QObject *obj) | |
135 | { | |
2d6421a9 | 136 | if (!obj || qobject_type(obj) != QTYPE_QLIST) { |
a6fd08eb LC |
137 | return NULL; |
138 | } | |
a6fd08eb LC |
139 | return container_of(obj, QList, base); |
140 | } | |
141 | ||
b38dd678 HR |
142 | /** |
143 | * qlist_is_equal(): Test whether the two QLists are equal | |
144 | * | |
145 | * In order to be considered equal, the respective two objects at each | |
146 | * index of the two lists have to compare equal (regarding | |
147 | * qobject_is_equal()), and both lists have to have the same number of | |
148 | * elements. | |
149 | * That means both lists have to contain equal objects in equal order. | |
150 | */ | |
151 | bool qlist_is_equal(const QObject *x, const QObject *y) | |
152 | { | |
153 | const QList *list_x = qobject_to_qlist(x); | |
154 | const QList *list_y = qobject_to_qlist(y); | |
155 | const QListEntry *entry_x, *entry_y; | |
156 | ||
157 | entry_x = qlist_first(list_x); | |
158 | entry_y = qlist_first(list_y); | |
159 | ||
160 | while (entry_x && entry_y) { | |
161 | if (!qobject_is_equal(qlist_entry_obj(entry_x), | |
162 | qlist_entry_obj(entry_y))) | |
163 | { | |
164 | return false; | |
165 | } | |
166 | ||
167 | entry_x = qlist_next(entry_x); | |
168 | entry_y = qlist_next(entry_y); | |
169 | } | |
170 | ||
171 | return !entry_x && !entry_y; | |
172 | } | |
173 | ||
a6fd08eb LC |
174 | /** |
175 | * qlist_destroy_obj(): Free all the memory allocated by a QList | |
176 | */ | |
55e1819c | 177 | void qlist_destroy_obj(QObject *obj) |
a6fd08eb LC |
178 | { |
179 | QList *qlist; | |
180 | QListEntry *entry, *next_entry; | |
181 | ||
182 | assert(obj != NULL); | |
183 | qlist = qobject_to_qlist(obj); | |
184 | ||
185 | QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) { | |
186 | QTAILQ_REMOVE(&qlist->head, entry, next); | |
187 | qobject_decref(entry->value); | |
7267c094 | 188 | g_free(entry); |
a6fd08eb LC |
189 | } |
190 | ||
7267c094 | 191 | g_free(qlist); |
a6fd08eb | 192 | } |