acpi: add interface to build device specific AML
[qemu.git] / qobject / qlist.c
blob356ad946b00c59b645612c4268085bcba522ccf3
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"
20 #include "qobject-internal.h"
22 /**
23 * qlist_new(): Create a new QList
25 * Return strong reference.
27 QList *qlist_new(void)
29 QList *qlist;
31 qlist = g_malloc(sizeof(*qlist));
32 qobject_init(QOBJECT(qlist), QTYPE_QLIST);
33 QTAILQ_INIT(&qlist->head);
35 return qlist;
38 QList *qlist_copy(QList *src)
40 QList *dst = qlist_new();
41 QListEntry *entry;
42 QObject *elt;
44 QLIST_FOREACH_ENTRY(src, entry) {
45 elt = qlist_entry_obj(entry);
46 qobject_ref(elt);
47 qlist_append_obj(dst, elt);
49 return dst;
52 /**
53 * qlist_append_obj(): Append an QObject into QList
55 * NOTE: ownership of 'value' is transferred to the QList
57 void qlist_append_obj(QList *qlist, QObject *value)
59 QListEntry *entry;
61 entry = g_malloc(sizeof(*entry));
62 entry->value = value;
64 QTAILQ_INSERT_TAIL(&qlist->head, entry, next);
67 void qlist_append_int(QList *qlist, int64_t value)
69 qlist_append(qlist, qnum_from_int(value));
72 void qlist_append_bool(QList *qlist, bool value)
74 qlist_append(qlist, qbool_from_bool(value));
77 void qlist_append_str(QList *qlist, const char *value)
79 qlist_append(qlist, qstring_from_str(value));
82 void qlist_append_null(QList *qlist)
84 qlist_append(qlist, qnull());
87 QObject *qlist_pop(QList *qlist)
89 QListEntry *entry;
90 QObject *ret;
92 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
93 return NULL;
96 entry = QTAILQ_FIRST(&qlist->head);
97 QTAILQ_REMOVE(&qlist->head, entry, next);
99 ret = entry->value;
100 g_free(entry);
102 return ret;
105 QObject *qlist_peek(QList *qlist)
107 QListEntry *entry;
109 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
110 return NULL;
113 entry = QTAILQ_FIRST(&qlist->head);
115 return entry->value;
118 int qlist_empty(const QList *qlist)
120 return QTAILQ_EMPTY(&qlist->head);
123 size_t qlist_size(const QList *qlist)
125 size_t count = 0;
126 QListEntry *entry;
128 QLIST_FOREACH_ENTRY(qlist, entry) {
129 count++;
131 return count;
135 * qlist_is_equal(): Test whether the two QLists are equal
137 * In order to be considered equal, the respective two objects at each
138 * index of the two lists have to compare equal (regarding
139 * qobject_is_equal()), and both lists have to have the same number of
140 * elements.
141 * That means both lists have to contain equal objects in equal order.
143 bool qlist_is_equal(const QObject *x, const QObject *y)
145 const QList *list_x = qobject_to(QList, x);
146 const QList *list_y = qobject_to(QList, y);
147 const QListEntry *entry_x, *entry_y;
149 entry_x = qlist_first(list_x);
150 entry_y = qlist_first(list_y);
152 while (entry_x && entry_y) {
153 if (!qobject_is_equal(qlist_entry_obj(entry_x),
154 qlist_entry_obj(entry_y)))
156 return false;
159 entry_x = qlist_next(entry_x);
160 entry_y = qlist_next(entry_y);
163 return !entry_x && !entry_y;
167 * qlist_destroy_obj(): Free all the memory allocated by a QList
169 void qlist_destroy_obj(QObject *obj)
171 QList *qlist;
172 QListEntry *entry, *next_entry;
174 assert(obj != NULL);
175 qlist = qobject_to(QList, obj);
177 QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
178 QTAILQ_REMOVE(&qlist->head, entry, next);
179 qobject_unref(entry->value);
180 g_free(entry);
183 g_free(qlist);
186 void qlist_unref(QList *q)
188 qobject_unref(q);