Delayed IP packets
[qemu/agraf.git] / qlist.c
blob5730fb87f7bd848e2493d55bde055bf9e3965faf
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 "qlist.h"
14 #include "qobject.h"
15 #include "qemu-queue.h"
16 #include "qemu-common.h"
18 static void qlist_destroy_obj(QObject *obj);
20 static const QType qlist_type = {
21 .code = QTYPE_QLIST,
22 .destroy = qlist_destroy_obj,
25 /**
26 * qlist_new(): Create a new QList
28 * Return strong reference.
30 QList *qlist_new(void)
32 QList *qlist;
34 qlist = qemu_malloc(sizeof(*qlist));
35 QTAILQ_INIT(&qlist->head);
36 QOBJECT_INIT(qlist, &qlist_type);
38 return qlist;
41 static void qlist_copy_elem(QObject *obj, void *opaque)
43 QList *dst = opaque;
45 qobject_incref(obj);
46 qlist_append_obj(dst, obj);
49 QList *qlist_copy(QList *src)
51 QList *dst = qlist_new();
53 qlist_iter(src, qlist_copy_elem, dst);
55 return dst;
58 /**
59 * qlist_append_obj(): Append an QObject into QList
61 * NOTE: ownership of 'value' is transferred to the QList
63 void qlist_append_obj(QList *qlist, QObject *value)
65 QListEntry *entry;
67 entry = qemu_malloc(sizeof(*entry));
68 entry->value = value;
70 QTAILQ_INSERT_TAIL(&qlist->head, entry, next);
73 /**
74 * qlist_iter(): Iterate over all the list's stored values.
76 * This function allows the user to provide an iterator, which will be
77 * called for each stored value in the list.
79 void qlist_iter(const QList *qlist,
80 void (*iter)(QObject *obj, void *opaque), void *opaque)
82 QListEntry *entry;
84 QTAILQ_FOREACH(entry, &qlist->head, next)
85 iter(entry->value, opaque);
88 QObject *qlist_pop(QList *qlist)
90 QListEntry *entry;
91 QObject *ret;
93 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
94 return NULL;
97 entry = QTAILQ_FIRST(&qlist->head);
98 QTAILQ_REMOVE(&qlist->head, entry, next);
100 ret = entry->value;
101 qemu_free(entry);
103 return ret;
106 QObject *qlist_peek(QList *qlist)
108 QListEntry *entry;
109 QObject *ret;
111 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
112 return NULL;
115 entry = QTAILQ_FIRST(&qlist->head);
117 ret = entry->value;
119 return ret;
122 int qlist_empty(const QList *qlist)
124 return QTAILQ_EMPTY(&qlist->head);
128 * qobject_to_qlist(): Convert a QObject into a QList
130 QList *qobject_to_qlist(const QObject *obj)
132 if (qobject_type(obj) != QTYPE_QLIST) {
133 return NULL;
136 return container_of(obj, QList, base);
140 * qlist_destroy_obj(): Free all the memory allocated by a QList
142 static void qlist_destroy_obj(QObject *obj)
144 QList *qlist;
145 QListEntry *entry, *next_entry;
147 assert(obj != NULL);
148 qlist = qobject_to_qlist(obj);
150 QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
151 QTAILQ_REMOVE(&qlist->head, entry, next);
152 qobject_decref(entry->value);
153 qemu_free(entry);
156 qemu_free(qlist);