virtio: introduce virtqueue_alloc_element
[qemu.git] / qobject / qlist.c
blob3c045aed11e95b3b4af9662b34c5e38c1b572cd7
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 "qapi/qmp/qlist.h"
14 #include "qapi/qmp/qobject.h"
15 #include "qemu/queue.h"
16 #include "qemu-common.h"
18 /**
19 * qlist_new(): Create a new QList
21 * Return strong reference.
23 QList *qlist_new(void)
25 QList *qlist;
27 qlist = g_malloc(sizeof(*qlist));
28 qobject_init(QOBJECT(qlist), QTYPE_QLIST);
29 QTAILQ_INIT(&qlist->head);
31 return qlist;
34 static void qlist_copy_elem(QObject *obj, void *opaque)
36 QList *dst = opaque;
38 qobject_incref(obj);
39 qlist_append_obj(dst, obj);
42 QList *qlist_copy(QList *src)
44 QList *dst = qlist_new();
46 qlist_iter(src, qlist_copy_elem, dst);
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 /**
67 * qlist_iter(): Iterate over all the list's stored values.
69 * This function allows the user to provide an iterator, which will be
70 * called for each stored value in the list.
72 void qlist_iter(const QList *qlist,
73 void (*iter)(QObject *obj, void *opaque), void *opaque)
75 QListEntry *entry;
77 QTAILQ_FOREACH(entry, &qlist->head, next)
78 iter(entry->value, opaque);
81 QObject *qlist_pop(QList *qlist)
83 QListEntry *entry;
84 QObject *ret;
86 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
87 return NULL;
90 entry = QTAILQ_FIRST(&qlist->head);
91 QTAILQ_REMOVE(&qlist->head, entry, next);
93 ret = entry->value;
94 g_free(entry);
96 return ret;
99 QObject *qlist_peek(QList *qlist)
101 QListEntry *entry;
102 QObject *ret;
104 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
105 return NULL;
108 entry = QTAILQ_FIRST(&qlist->head);
110 ret = entry->value;
112 return ret;
115 int qlist_empty(const QList *qlist)
117 return QTAILQ_EMPTY(&qlist->head);
120 static void qlist_size_iter(QObject *obj, void *opaque)
122 size_t *count = opaque;
123 (*count)++;
126 size_t qlist_size(const QList *qlist)
128 size_t count = 0;
129 qlist_iter(qlist, qlist_size_iter, &count);
130 return count;
134 * qobject_to_qlist(): Convert a QObject into a QList
136 QList *qobject_to_qlist(const QObject *obj)
138 if (!obj || qobject_type(obj) != QTYPE_QLIST) {
139 return NULL;
141 return container_of(obj, QList, base);
145 * qlist_destroy_obj(): Free all the memory allocated by a QList
147 void qlist_destroy_obj(QObject *obj)
149 QList *qlist;
150 QListEntry *entry, *next_entry;
152 assert(obj != NULL);
153 qlist = qobject_to_qlist(obj);
155 QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
156 QTAILQ_REMOVE(&qlist->head, entry, next);
157 qobject_decref(entry->value);
158 g_free(entry);
161 g_free(qlist);