blob: 1ec74de2b39434fef249bb18a8f8c8015c2bb558 [file] [log] [blame]
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -03001/*
Luiz Capitulino41836a92010-05-12 16:34:42 -03002 * QList Module
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -03003 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
8 *
Luiz Capitulino41836a92010-05-12 16:34:42 -03009 * 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.
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -030011 */
Luiz Capitulino41836a92010-05-12 16:34:42 -030012
Peter Maydellf2ad72b2016-01-29 17:50:01 +000013#include "qemu/osdep.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010014#include "qapi/qmp/qlist.h"
15#include "qapi/qmp/qobject.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/queue.h"
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -030017#include "qemu-common.h"
18
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -030019/**
20 * qlist_new(): Create a new QList
21 *
22 * Return strong reference.
23 */
24QList *qlist_new(void)
25{
26 QList *qlist;
27
Anthony Liguori7267c092011-08-20 22:09:37 -050028 qlist = g_malloc(sizeof(*qlist));
Eric Blake55e18192015-12-01 22:20:45 -070029 qobject_init(QOBJECT(qlist), QTYPE_QLIST);
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -030030 QTAILQ_INIT(&qlist->head);
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -030031
32 return qlist;
33}
34
Anthony Liguori033815f2009-11-11 10:48:51 -060035static void qlist_copy_elem(QObject *obj, void *opaque)
36{
37 QList *dst = opaque;
38
39 qobject_incref(obj);
40 qlist_append_obj(dst, obj);
41}
42
43QList *qlist_copy(QList *src)
44{
45 QList *dst = qlist_new();
46
47 qlist_iter(src, qlist_copy_elem, dst);
48
49 return dst;
50}
51
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -030052/**
53 * qlist_append_obj(): Append an QObject into QList
54 *
55 * NOTE: ownership of 'value' is transferred to the QList
56 */
57void qlist_append_obj(QList *qlist, QObject *value)
58{
59 QListEntry *entry;
60
Anthony Liguori7267c092011-08-20 22:09:37 -050061 entry = g_malloc(sizeof(*entry));
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -030062 entry->value = value;
63
64 QTAILQ_INSERT_TAIL(&qlist->head, entry, next);
65}
66
67/**
68 * qlist_iter(): Iterate over all the list's stored values.
69 *
70 * This function allows the user to provide an iterator, which will be
71 * called for each stored value in the list.
72 */
73void qlist_iter(const QList *qlist,
74 void (*iter)(QObject *obj, void *opaque), void *opaque)
75{
76 QListEntry *entry;
77
78 QTAILQ_FOREACH(entry, &qlist->head, next)
79 iter(entry->value, opaque);
80}
81
Anthony Liguori033815f2009-11-11 10:48:51 -060082QObject *qlist_pop(QList *qlist)
83{
84 QListEntry *entry;
85 QObject *ret;
86
87 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
88 return NULL;
89 }
90
91 entry = QTAILQ_FIRST(&qlist->head);
92 QTAILQ_REMOVE(&qlist->head, entry, next);
93
94 ret = entry->value;
Anthony Liguori7267c092011-08-20 22:09:37 -050095 g_free(entry);
Anthony Liguori033815f2009-11-11 10:48:51 -060096
97 return ret;
98}
99
100QObject *qlist_peek(QList *qlist)
101{
102 QListEntry *entry;
103 QObject *ret;
104
105 if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) {
106 return NULL;
107 }
108
109 entry = QTAILQ_FIRST(&qlist->head);
110
111 ret = entry->value;
112
113 return ret;
114}
115
116int qlist_empty(const QList *qlist)
117{
118 return QTAILQ_EMPTY(&qlist->head);
119}
120
Michael Rotha86a4c22012-08-15 13:45:42 -0500121static void qlist_size_iter(QObject *obj, void *opaque)
122{
123 size_t *count = opaque;
124 (*count)++;
125}
126
127size_t qlist_size(const QList *qlist)
128{
129 size_t count = 0;
130 qlist_iter(qlist, qlist_size_iter, &count);
131 return count;
132}
133
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -0300134/**
135 * qobject_to_qlist(): Convert a QObject into a QList
136 */
137QList *qobject_to_qlist(const QObject *obj)
138{
Markus Armbruster2d6421a2015-10-15 16:15:36 +0200139 if (!obj || qobject_type(obj) != QTYPE_QLIST) {
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -0300140 return NULL;
141 }
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -0300142 return container_of(obj, QList, base);
143}
144
145/**
146 * qlist_destroy_obj(): Free all the memory allocated by a QList
147 */
Eric Blake55e18192015-12-01 22:20:45 -0700148void qlist_destroy_obj(QObject *obj)
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -0300149{
150 QList *qlist;
151 QListEntry *entry, *next_entry;
152
153 assert(obj != NULL);
154 qlist = qobject_to_qlist(obj);
155
156 QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) {
157 QTAILQ_REMOVE(&qlist->head, entry, next);
158 qobject_decref(entry->value);
Anthony Liguori7267c092011-08-20 22:09:37 -0500159 g_free(entry);
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -0300160 }
161
Anthony Liguori7267c092011-08-20 22:09:37 -0500162 g_free(qlist);
Luiz Capitulinoa6fd08e2009-10-07 13:41:48 -0300163}