]> git.ipfire.org Git - thirdparty/qemu.git/blame - qobject/qlist.c
net: Clean up includes
[thirdparty/qemu.git] / qobject / qlist.c
CommitLineData
a6fd08eb 1/*
41836a9f 2 * QList Module
a6fd08eb
LC
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
41836a9f
LC
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.
a6fd08eb 11 */
41836a9f 12
7b1b5d19
PB
13#include "qapi/qmp/qlist.h"
14#include "qapi/qmp/qobject.h"
1de7afc9 15#include "qemu/queue.h"
a6fd08eb
LC
16#include "qemu-common.h"
17
a6fd08eb
LC
18/**
19 * qlist_new(): Create a new QList
20 *
21 * Return strong reference.
22 */
23QList *qlist_new(void)
24{
25 QList *qlist;
26
7267c094 27 qlist = g_malloc(sizeof(*qlist));
55e1819c 28 qobject_init(QOBJECT(qlist), QTYPE_QLIST);
a6fd08eb 29 QTAILQ_INIT(&qlist->head);
a6fd08eb
LC
30
31 return qlist;
32}
33
033815fe
AL
34static void qlist_copy_elem(QObject *obj, void *opaque)
35{
36 QList *dst = opaque;
37
38 qobject_incref(obj);
39 qlist_append_obj(dst, obj);
40}
41
42QList *qlist_copy(QList *src)
43{
44 QList *dst = qlist_new();
45
46 qlist_iter(src, qlist_copy_elem, dst);
47
48 return dst;
49}
50
a6fd08eb
LC
51/**
52 * qlist_append_obj(): Append an QObject into QList
53 *
54 * NOTE: ownership of 'value' is transferred to the QList
55 */
56void qlist_append_obj(QList *qlist, QObject *value)
57{
58 QListEntry *entry;
59
7267c094 60 entry = g_malloc(sizeof(*entry));
a6fd08eb
LC
61 entry->value = value;
62
63 QTAILQ_INSERT_TAIL(&qlist->head, entry, next);
64}
65
66/**
67 * qlist_iter(): Iterate over all the list's stored values.
68 *
69 * This function allows the user to provide an iterator, which will be
70 * called for each stored value in the list.
71 */
72void qlist_iter(const QList *qlist,
73 void (*iter)(QObject *obj, void *opaque), void *opaque)
74{
75 QListEntry *entry;
76
77 QTAILQ_FOREACH(entry, &qlist->head, next)
78 iter(entry->value, opaque);
79}
80
033815fe
AL
81QObject *qlist_pop(QList *qlist)
82{
83 QListEntry *entry;
84 QObject *ret;
85
86 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
87 return NULL;
88 }
89
90 entry = QTAILQ_FIRST(&qlist->head);
91 QTAILQ_REMOVE(&qlist->head, entry, next);
92
93 ret = entry->value;
7267c094 94 g_free(entry);
033815fe
AL
95
96 return ret;
97}
98
99QObject *qlist_peek(QList *qlist)
100{
101 QListEntry *entry;
102 QObject *ret;
103
104 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
105 return NULL;
106 }
107
108 entry = QTAILQ_FIRST(&qlist->head);
109
110 ret = entry->value;
111
112 return ret;
113}
114
115int qlist_empty(const QList *qlist)
116{
117 return QTAILQ_EMPTY(&qlist->head);
118}
119
a86a4c2f
MR
120static void qlist_size_iter(QObject *obj, void *opaque)
121{
122 size_t *count = opaque;
123 (*count)++;
124}
125
126size_t qlist_size(const QList *qlist)
127{
128 size_t count = 0;
129 qlist_iter(qlist, qlist_size_iter, &count);
130 return count;
131}
132
a6fd08eb
LC
133/**
134 * qobject_to_qlist(): Convert a QObject into a QList
135 */
136QList *qobject_to_qlist(const QObject *obj)
137{
2d6421a9 138 if (!obj || qobject_type(obj) != QTYPE_QLIST) {
a6fd08eb
LC
139 return NULL;
140 }
a6fd08eb
LC
141 return container_of(obj, QList, base);
142}
143
144/**
145 * qlist_destroy_obj(): Free all the memory allocated by a QList
146 */
55e1819c 147void qlist_destroy_obj(QObject *obj)
a6fd08eb
LC
148{
149 QList *qlist;
150 QListEntry *entry, *next_entry;
151
152 assert(obj != NULL);
153 qlist = qobject_to_qlist(obj);
154
155 QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
156 QTAILQ_REMOVE(&qlist->head, entry, next);
157 qobject_decref(entry->value);
7267c094 158 g_free(entry);
a6fd08eb
LC
159 }
160
7267c094 161 g_free(qlist);
a6fd08eb 162}