qdev: Use returned bool to check for failure, Coccinelle part
[qemu.git] / qobject / qlist.c
blob1be95367d1a0b7299f1a32de1df4139de8a0d635
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 QList *qlist_copy(QList *src)
39 QList *dst = qlist_new();
40 QListEntry *entry;
41 QObject *elt;
43 QLIST_FOREACH_ENTRY(src, entry) {
44 elt = qlist_entry_obj(entry);
45 qobject_ref(elt);
46 qlist_append_obj(dst, elt);
48 return dst;
51 /**
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)
58 QListEntry *entry;
60 entry = g_malloc(sizeof(*entry));
61 entry->value = value;
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)
88 QListEntry *entry;
89 QObject *ret;
91 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
92 return NULL;
95 entry = QTAILQ_FIRST(&qlist->head);
96 QTAILQ_REMOVE(&qlist->head, entry, next);
98 ret = entry->value;
99 g_free(entry);
101 return ret;
104 QObject *qlist_peek(QList *qlist)
106 QListEntry *entry;
108 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
109 return NULL;
112 entry = QTAILQ_FIRST(&qlist->head);
114 return entry->value;
117 int qlist_empty(const QList *qlist)
119 return QTAILQ_EMPTY(&qlist->head);
122 size_t qlist_size(const QList *qlist)
124 size_t count = 0;
125 QListEntry *entry;
127 QLIST_FOREACH_ENTRY(qlist, entry) {
128 count++;
130 return count;
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
139 * elements.
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)))
155 return false;
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)
170 QList *qlist;
171 QListEntry *entry, *next_entry;
173 assert(obj != NULL);
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);
179 g_free(entry);
182 g_free(qlist);