4 * Copyright (C) 2009 Red Hat Inc.
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"
22 * qlist_new(): Create a new QList
24 * Return strong reference.
26 QList *qlist_new(void)
30 qlist = g_malloc(sizeof(*qlist));
31 qobject_init(QOBJECT(qlist), QTYPE_QLIST);
32 QTAILQ_INIT(&qlist->head);
37 QList *qlist_copy(QList *src)
39 QList *dst = qlist_new();
43 QLIST_FOREACH_ENTRY(src, entry) {
44 elt = qlist_entry_obj(entry);
46 qlist_append_obj(dst, elt);
52 * qlist_append_obj(): Append an QObject into QList
54 * NOTE: ownership of 'value' is transferred to the QList
56 void qlist_append_obj(QList *qlist, QObject *value)
60 entry = g_malloc(sizeof(*entry));
63 QTAILQ_INSERT_TAIL(&qlist->head, entry, next);
66 void qlist_append_int(QList *qlist, int64_t value)
68 qlist_append(qlist, qnum_from_int(value));
71 void qlist_append_bool(QList *qlist, bool value)
73 qlist_append(qlist, qbool_from_bool(value));
76 void qlist_append_str(QList *qlist, const char *value)
78 qlist_append(qlist, qstring_from_str(value));
81 void qlist_append_null(QList *qlist)
83 qlist_append(qlist, qnull());
86 QObject *qlist_pop(QList *qlist)
91 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
95 entry = QTAILQ_FIRST(&qlist->head);
96 QTAILQ_REMOVE(&qlist->head, entry, next);
104 QObject *qlist_peek(QList *qlist)
108 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
112 entry = QTAILQ_FIRST(&qlist->head);
117 int qlist_empty(const QList *qlist)
119 return QTAILQ_EMPTY(&qlist->head);
122 size_t qlist_size(const QList *qlist)
127 QLIST_FOREACH_ENTRY(qlist, entry) {
134 * qlist_is_equal(): Test whether the two QLists are equal
136 * In order to be considered equal, the respective two objects at each
137 * index of the two lists have to compare equal (regarding
138 * qobject_is_equal()), and both lists have to have the same number of
140 * That means both lists have to contain equal objects in equal order.
142 bool qlist_is_equal(const QObject *x, const QObject *y)
144 const QList *list_x = qobject_to(QList, x);
145 const QList *list_y = qobject_to(QList, y);
146 const QListEntry *entry_x, *entry_y;
148 entry_x = qlist_first(list_x);
149 entry_y = qlist_first(list_y);
151 while (entry_x && entry_y) {
152 if (!qobject_is_equal(qlist_entry_obj(entry_x),
153 qlist_entry_obj(entry_y)))
158 entry_x = qlist_next(entry_x);
159 entry_y = qlist_next(entry_y);
162 return !entry_x && !entry_y;
166 * qlist_destroy_obj(): Free all the memory allocated by a QList
168 void qlist_destroy_obj(QObject *obj)
171 QListEntry *entry, *next_entry;
174 qlist = qobject_to(QList, obj);
176 QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
177 QTAILQ_REMOVE(&qlist->head, entry, next);
178 qobject_unref(entry->value);