bsd-user: Suppress gcc 4.x -Wpointer-sign (included in -Wall) warning
[qemu/ar7.git] / qobject / qlist.c
blob1ec74de2b39434fef249bb18a8f8c8015c2bb558
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/qlist.h"
15 #include "qapi/qmp/qobject.h"
16 #include "qemu/queue.h"
17 #include "qemu-common.h"
19 /**
20 * qlist_new(): Create a new QList
22 * Return strong reference.
24 QList *qlist_new(void)
26 QList *qlist;
28 qlist = g_malloc(sizeof(*qlist));
29 qobject_init(QOBJECT(qlist), QTYPE_QLIST);
30 QTAILQ_INIT(&qlist->head);
32 return qlist;
35 static void qlist_copy_elem(QObject *obj, void *opaque)
37 QList *dst = opaque;
39 qobject_incref(obj);
40 qlist_append_obj(dst, obj);
43 QList *qlist_copy(QList *src)
45 QList *dst = qlist_new();
47 qlist_iter(src, qlist_copy_elem, dst);
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 /**
68 * qlist_iter(): Iterate over all the list's stored values.
70 * This function allows the user to provide an iterator, which will be
71 * called for each stored value in the list.
73 void qlist_iter(const QList *qlist,
74 void (*iter)(QObject *obj, void *opaque), void *opaque)
76 QListEntry *entry;
78 QTAILQ_FOREACH(entry, &qlist->head, next)
79 iter(entry->value, opaque);
82 QObject *qlist_pop(QList *qlist)
84 QListEntry *entry;
85 QObject *ret;
87 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
88 return NULL;
91 entry = QTAILQ_FIRST(&qlist->head);
92 QTAILQ_REMOVE(&qlist->head, entry, next);
94 ret = entry->value;
95 g_free(entry);
97 return ret;
100 QObject *qlist_peek(QList *qlist)
102 QListEntry *entry;
103 QObject *ret;
105 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
106 return NULL;
109 entry = QTAILQ_FIRST(&qlist->head);
111 ret = entry->value;
113 return ret;
116 int qlist_empty(const QList *qlist)
118 return QTAILQ_EMPTY(&qlist->head);
121 static void qlist_size_iter(QObject *obj, void *opaque)
123 size_t *count = opaque;
124 (*count)++;
127 size_t qlist_size(const QList *qlist)
129 size_t count = 0;
130 qlist_iter(qlist, qlist_size_iter, &count);
131 return count;
135 * qobject_to_qlist(): Convert a QObject into a QList
137 QList *qobject_to_qlist(const QObject *obj)
139 if (!obj || qobject_type(obj) != QTYPE_QLIST) {
140 return NULL;
142 return container_of(obj, QList, base);
146 * qlist_destroy_obj(): Free all the memory allocated by a QList
148 void qlist_destroy_obj(QObject *obj)
150 QList *qlist;
151 QListEntry *entry, *next_entry;
153 assert(obj != NULL);
154 qlist = qobject_to_qlist(obj);
156 QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
157 QTAILQ_REMOVE(&qlist->head, entry, next);
158 qobject_decref(entry->value);
159 g_free(entry);
162 g_free(qlist);