net/slirp: Deprecate the [hub_id name] parameter tuple
[qemu.git] / qobject / qlist.c
blobb3274af88b0b82bc640363737715ae6872288274
1 /*
2 * QList Module
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include "qapi/qmp/qbool.h"
15 #include "qapi/qmp/qlist.h"
16 #include "qapi/qmp/qnull.h"
17 #include "qapi/qmp/qnum.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qemu/queue.h"
21 /**
22 * qlist_new(): Create a new QList
24 * Return strong reference.
26 QList *qlist_new(void)
28 QList *qlist;
30 qlist = g_malloc(sizeof(*qlist));
31 qobject_init(QOBJECT(qlist), QTYPE_QLIST);
32 QTAILQ_INIT(&qlist->head);
34 return qlist;
37 static void qlist_copy_elem(QObject *obj, void *opaque)
39 QList *dst = opaque;
41 qobject_ref(obj);
42 qlist_append_obj(dst, obj);
45 QList *qlist_copy(QList *src)
47 QList *dst = qlist_new();
49 qlist_iter(src, qlist_copy_elem, dst);
51 return dst;
54 /**
55 * qlist_append_obj(): Append an QObject into QList
57 * NOTE: ownership of 'value' is transferred to the QList
59 void qlist_append_obj(QList *qlist, QObject *value)
61 QListEntry *entry;
63 entry = g_malloc(sizeof(*entry));
64 entry->value = value;
66 QTAILQ_INSERT_TAIL(&qlist->head, entry, next);
69 void qlist_append_int(QList *qlist, int64_t value)
71 qlist_append(qlist, qnum_from_int(value));
74 void qlist_append_bool(QList *qlist, bool value)
76 qlist_append(qlist, qbool_from_bool(value));
79 void qlist_append_str(QList *qlist, const char *value)
81 qlist_append(qlist, qstring_from_str(value));
84 void qlist_append_null(QList *qlist)
86 qlist_append(qlist, qnull());
89 /**
90 * qlist_iter(): Iterate over all the list's stored values.
92 * This function allows the user to provide an iterator, which will be
93 * called for each stored value in the list.
95 void qlist_iter(const QList *qlist,
96 void (*iter)(QObject *obj, void *opaque), void *opaque)
98 QListEntry *entry;
100 QTAILQ_FOREACH(entry, &qlist->head, next)
101 iter(entry->value, opaque);
104 QObject *qlist_pop(QList *qlist)
106 QListEntry *entry;
107 QObject *ret;
109 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
110 return NULL;
113 entry = QTAILQ_FIRST(&qlist->head);
114 QTAILQ_REMOVE(&qlist->head, entry, next);
116 ret = entry->value;
117 g_free(entry);
119 return ret;
122 QObject *qlist_peek(QList *qlist)
124 QListEntry *entry;
126 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
127 return NULL;
130 entry = QTAILQ_FIRST(&qlist->head);
132 return entry->value;
135 int qlist_empty(const QList *qlist)
137 return QTAILQ_EMPTY(&qlist->head);
140 static void qlist_size_iter(QObject *obj, void *opaque)
142 size_t *count = opaque;
143 (*count)++;
146 size_t qlist_size(const QList *qlist)
148 size_t count = 0;
149 qlist_iter(qlist, qlist_size_iter, &count);
150 return count;
154 * qlist_is_equal(): Test whether the two QLists are equal
156 * In order to be considered equal, the respective two objects at each
157 * index of the two lists have to compare equal (regarding
158 * qobject_is_equal()), and both lists have to have the same number of
159 * elements.
160 * That means both lists have to contain equal objects in equal order.
162 bool qlist_is_equal(const QObject *x, const QObject *y)
164 const QList *list_x = qobject_to(QList, x);
165 const QList *list_y = qobject_to(QList, y);
166 const QListEntry *entry_x, *entry_y;
168 entry_x = qlist_first(list_x);
169 entry_y = qlist_first(list_y);
171 while (entry_x && entry_y) {
172 if (!qobject_is_equal(qlist_entry_obj(entry_x),
173 qlist_entry_obj(entry_y)))
175 return false;
178 entry_x = qlist_next(entry_x);
179 entry_y = qlist_next(entry_y);
182 return !entry_x && !entry_y;
186 * qlist_destroy_obj(): Free all the memory allocated by a QList
188 void qlist_destroy_obj(QObject *obj)
190 QList *qlist;
191 QListEntry *entry, *next_entry;
193 assert(obj != NULL);
194 qlist = qobject_to(QList, obj);
196 QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
197 QTAILQ_REMOVE(&qlist->head, entry, next);
198 qobject_unref(entry->value);
199 g_free(entry);
202 g_free(qlist);