xref: /qemu/qobject/qlist.c (revision 54e91d1523b412b4cff7cb36c898fa9dc133e886)
1a6fd08ebSLuiz Capitulino /*
241836a9fSLuiz Capitulino  * QList Module
3a6fd08ebSLuiz Capitulino  *
4a6fd08ebSLuiz Capitulino  * Copyright (C) 2009 Red Hat Inc.
5a6fd08ebSLuiz Capitulino  *
6a6fd08ebSLuiz Capitulino  * Authors:
7a6fd08ebSLuiz Capitulino  *  Luiz Capitulino <lcapitulino@redhat.com>
8a6fd08ebSLuiz Capitulino  *
941836a9fSLuiz Capitulino  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
1041836a9fSLuiz Capitulino  * See the COPYING.LIB file in the top-level directory.
11a6fd08ebSLuiz Capitulino  */
1241836a9fSLuiz Capitulino 
13f2ad72b3SPeter Maydell #include "qemu/osdep.h"
14*407bc4bfSDaniel P. Berrangé #include "qobject/qbool.h"
15*407bc4bfSDaniel P. Berrangé #include "qobject/qlist.h"
16*407bc4bfSDaniel P. Berrangé #include "qobject/qnull.h"
17*407bc4bfSDaniel P. Berrangé #include "qobject/qnum.h"
18*407bc4bfSDaniel P. Berrangé #include "qobject/qstring.h"
191de7afc9SPaolo Bonzini #include "qemu/queue.h"
2080d71121SMarkus Armbruster #include "qobject-internal.h"
21a6fd08ebSLuiz Capitulino 
22a6fd08ebSLuiz Capitulino /**
23a6fd08ebSLuiz Capitulino  * qlist_new(): Create a new QList
24a6fd08ebSLuiz Capitulino  *
25a6fd08ebSLuiz Capitulino  * Return strong reference.
26a6fd08ebSLuiz Capitulino  */
qlist_new(void)27a6fd08ebSLuiz Capitulino QList *qlist_new(void)
28a6fd08ebSLuiz Capitulino {
29a6fd08ebSLuiz Capitulino     QList *qlist;
30a6fd08ebSLuiz Capitulino 
317267c094SAnthony Liguori     qlist = g_malloc(sizeof(*qlist));
3255e1819cSEric Blake     qobject_init(QOBJECT(qlist), QTYPE_QLIST);
33a6fd08ebSLuiz Capitulino     QTAILQ_INIT(&qlist->head);
34a6fd08ebSLuiz Capitulino 
35a6fd08ebSLuiz Capitulino     return qlist;
36a6fd08ebSLuiz Capitulino }
37a6fd08ebSLuiz Capitulino 
qlist_copy(QList * src)38033815feSAnthony Liguori QList *qlist_copy(QList *src)
39033815feSAnthony Liguori {
40033815feSAnthony Liguori     QList *dst = qlist_new();
412f2ec111SMarkus Armbruster     QListEntry *entry;
422f2ec111SMarkus Armbruster     QObject *elt;
43033815feSAnthony Liguori 
442f2ec111SMarkus Armbruster     QLIST_FOREACH_ENTRY(src, entry) {
452f2ec111SMarkus Armbruster         elt = qlist_entry_obj(entry);
462f2ec111SMarkus Armbruster         qobject_ref(elt);
472f2ec111SMarkus Armbruster         qlist_append_obj(dst, elt);
482f2ec111SMarkus Armbruster     }
49033815feSAnthony Liguori     return dst;
50033815feSAnthony Liguori }
51033815feSAnthony Liguori 
52a6fd08ebSLuiz Capitulino /**
53a6fd08ebSLuiz Capitulino  * qlist_append_obj(): Append an QObject into QList
54a6fd08ebSLuiz Capitulino  *
55a6fd08ebSLuiz Capitulino  * NOTE: ownership of 'value' is transferred to the QList
56a6fd08ebSLuiz Capitulino  */
qlist_append_obj(QList * qlist,QObject * value)57a6fd08ebSLuiz Capitulino void qlist_append_obj(QList *qlist, QObject *value)
58a6fd08ebSLuiz Capitulino {
59a6fd08ebSLuiz Capitulino     QListEntry *entry;
60a6fd08ebSLuiz Capitulino 
617267c094SAnthony Liguori     entry = g_malloc(sizeof(*entry));
62a6fd08ebSLuiz Capitulino     entry->value = value;
63a6fd08ebSLuiz Capitulino 
64a6fd08ebSLuiz Capitulino     QTAILQ_INSERT_TAIL(&qlist->head, entry, next);
65a6fd08ebSLuiz Capitulino }
66a6fd08ebSLuiz Capitulino 
qlist_append_int(QList * qlist,int64_t value)6715280c36SMarkus Armbruster void qlist_append_int(QList *qlist, int64_t value)
6815280c36SMarkus Armbruster {
6915280c36SMarkus Armbruster     qlist_append(qlist, qnum_from_int(value));
7015280c36SMarkus Armbruster }
7115280c36SMarkus Armbruster 
qlist_append_bool(QList * qlist,bool value)7215280c36SMarkus Armbruster void qlist_append_bool(QList *qlist, bool value)
7315280c36SMarkus Armbruster {
7415280c36SMarkus Armbruster     qlist_append(qlist, qbool_from_bool(value));
7515280c36SMarkus Armbruster }
7615280c36SMarkus Armbruster 
qlist_append_str(QList * qlist,const char * value)7715280c36SMarkus Armbruster void qlist_append_str(QList *qlist, const char *value)
7815280c36SMarkus Armbruster {
7915280c36SMarkus Armbruster     qlist_append(qlist, qstring_from_str(value));
8015280c36SMarkus Armbruster }
8115280c36SMarkus Armbruster 
qlist_append_null(QList * qlist)8215280c36SMarkus Armbruster void qlist_append_null(QList *qlist)
8315280c36SMarkus Armbruster {
8415280c36SMarkus Armbruster     qlist_append(qlist, qnull());
8515280c36SMarkus Armbruster }
8615280c36SMarkus Armbruster 
qlist_pop(QList * qlist)87033815feSAnthony Liguori QObject *qlist_pop(QList *qlist)
88033815feSAnthony Liguori {
89033815feSAnthony Liguori     QListEntry *entry;
90033815feSAnthony Liguori     QObject *ret;
91033815feSAnthony Liguori 
92033815feSAnthony Liguori     if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
93033815feSAnthony Liguori         return NULL;
94033815feSAnthony Liguori     }
95033815feSAnthony Liguori 
96033815feSAnthony Liguori     entry = QTAILQ_FIRST(&qlist->head);
97033815feSAnthony Liguori     QTAILQ_REMOVE(&qlist->head, entry, next);
98033815feSAnthony Liguori 
99033815feSAnthony Liguori     ret = entry->value;
1007267c094SAnthony Liguori     g_free(entry);
101033815feSAnthony Liguori 
102033815feSAnthony Liguori     return ret;
103033815feSAnthony Liguori }
104033815feSAnthony Liguori 
qlist_peek(QList * qlist)105033815feSAnthony Liguori QObject *qlist_peek(QList *qlist)
106033815feSAnthony Liguori {
107033815feSAnthony Liguori     QListEntry *entry;
108033815feSAnthony Liguori 
109033815feSAnthony Liguori     if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
110033815feSAnthony Liguori         return NULL;
111033815feSAnthony Liguori     }
112033815feSAnthony Liguori 
113033815feSAnthony Liguori     entry = QTAILQ_FIRST(&qlist->head);
114033815feSAnthony Liguori 
1159be38598SEduardo Habkost     return entry->value;
116033815feSAnthony Liguori }
117033815feSAnthony Liguori 
qlist_empty(const QList * qlist)118033815feSAnthony Liguori int qlist_empty(const QList *qlist)
119033815feSAnthony Liguori {
120033815feSAnthony Liguori     return QTAILQ_EMPTY(&qlist->head);
121033815feSAnthony Liguori }
122033815feSAnthony Liguori 
qlist_size(const QList * qlist)123a86a4c2fSMichael Roth size_t qlist_size(const QList *qlist)
124a86a4c2fSMichael Roth {
125a86a4c2fSMichael Roth     size_t count = 0;
1262f2ec111SMarkus Armbruster     QListEntry *entry;
1272f2ec111SMarkus Armbruster 
1282f2ec111SMarkus Armbruster     QLIST_FOREACH_ENTRY(qlist, entry) {
1292f2ec111SMarkus Armbruster         count++;
1302f2ec111SMarkus Armbruster     }
131a86a4c2fSMichael Roth     return count;
132a86a4c2fSMichael Roth }
133a86a4c2fSMichael Roth 
134a6fd08ebSLuiz Capitulino /**
135b38dd678SMax Reitz  * qlist_is_equal(): Test whether the two QLists are equal
136b38dd678SMax Reitz  *
137b38dd678SMax Reitz  * In order to be considered equal, the respective two objects at each
138b38dd678SMax Reitz  * index of the two lists have to compare equal (regarding
139b38dd678SMax Reitz  * qobject_is_equal()), and both lists have to have the same number of
140b38dd678SMax Reitz  * elements.
141b38dd678SMax Reitz  * That means both lists have to contain equal objects in equal order.
142b38dd678SMax Reitz  */
qlist_is_equal(const QObject * x,const QObject * y)143b38dd678SMax Reitz bool qlist_is_equal(const QObject *x, const QObject *y)
144b38dd678SMax Reitz {
1457dc847ebSMax Reitz     const QList *list_x = qobject_to(QList, x);
1467dc847ebSMax Reitz     const QList *list_y = qobject_to(QList, y);
147b38dd678SMax Reitz     const QListEntry *entry_x, *entry_y;
148b38dd678SMax Reitz 
149b38dd678SMax Reitz     entry_x = qlist_first(list_x);
150b38dd678SMax Reitz     entry_y = qlist_first(list_y);
151b38dd678SMax Reitz 
152b38dd678SMax Reitz     while (entry_x && entry_y) {
153b38dd678SMax Reitz         if (!qobject_is_equal(qlist_entry_obj(entry_x),
154b38dd678SMax Reitz                               qlist_entry_obj(entry_y)))
155b38dd678SMax Reitz         {
156b38dd678SMax Reitz             return false;
157b38dd678SMax Reitz         }
158b38dd678SMax Reitz 
159b38dd678SMax Reitz         entry_x = qlist_next(entry_x);
160b38dd678SMax Reitz         entry_y = qlist_next(entry_y);
161b38dd678SMax Reitz     }
162b38dd678SMax Reitz 
163b38dd678SMax Reitz     return !entry_x && !entry_y;
164b38dd678SMax Reitz }
165b38dd678SMax Reitz 
166b38dd678SMax Reitz /**
167a6fd08ebSLuiz Capitulino  * qlist_destroy_obj(): Free all the memory allocated by a QList
168a6fd08ebSLuiz Capitulino  */
qlist_destroy_obj(QObject * obj)16955e1819cSEric Blake void qlist_destroy_obj(QObject *obj)
170a6fd08ebSLuiz Capitulino {
171a6fd08ebSLuiz Capitulino     QList *qlist;
172a6fd08ebSLuiz Capitulino     QListEntry *entry, *next_entry;
173a6fd08ebSLuiz Capitulino 
174a6fd08ebSLuiz Capitulino     assert(obj != NULL);
1757dc847ebSMax Reitz     qlist = qobject_to(QList, obj);
176a6fd08ebSLuiz Capitulino 
177a6fd08ebSLuiz Capitulino     QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
178a6fd08ebSLuiz Capitulino         QTAILQ_REMOVE(&qlist->head, entry, next);
179cb3e7f08SMarc-André Lureau         qobject_unref(entry->value);
1807267c094SAnthony Liguori         g_free(entry);
181a6fd08ebSLuiz Capitulino     }
182a6fd08ebSLuiz Capitulino 
1837267c094SAnthony Liguori     g_free(qlist);
184a6fd08ebSLuiz Capitulino }
185d709bbf3SMarc-André Lureau 
qlist_unref(QList * q)186d709bbf3SMarc-André Lureau void qlist_unref(QList *q)
187d709bbf3SMarc-André Lureau {
188d709bbf3SMarc-André Lureau     qobject_unref(q);
189d709bbf3SMarc-André Lureau }
190