root / qlist.c @ 1cd3af54
History | View | Annotate | Download (2.1 kB)
1 |
/*
|
---|---|
2 |
* QList data type.
|
3 |
*
|
4 |
* Copyright (C) 2009 Red Hat Inc.
|
5 |
*
|
6 |
* Authors:
|
7 |
* Luiz Capitulino <lcapitulino@redhat.com>
|
8 |
*
|
9 |
* This work is licensed under the terms of the GNU GPL, version 2. See
|
10 |
* the COPYING file in the top-level directory.
|
11 |
*/
|
12 |
#include "qlist.h" |
13 |
#include "qobject.h" |
14 |
#include "qemu-queue.h" |
15 |
#include "qemu-common.h" |
16 |
|
17 |
static void qlist_destroy_obj(QObject *obj); |
18 |
|
19 |
static const QType qlist_type = { |
20 |
.code = QTYPE_QLIST, |
21 |
.destroy = qlist_destroy_obj, |
22 |
}; |
23 |
|
24 |
/**
|
25 |
* qlist_new(): Create a new QList
|
26 |
*
|
27 |
* Return strong reference.
|
28 |
*/
|
29 |
QList *qlist_new(void)
|
30 |
{ |
31 |
QList *qlist; |
32 |
|
33 |
qlist = qemu_malloc(sizeof(*qlist));
|
34 |
QTAILQ_INIT(&qlist->head); |
35 |
QOBJECT_INIT(qlist, &qlist_type); |
36 |
|
37 |
return qlist;
|
38 |
} |
39 |
|
40 |
/**
|
41 |
* qlist_append_obj(): Append an QObject into QList
|
42 |
*
|
43 |
* NOTE: ownership of 'value' is transferred to the QList
|
44 |
*/
|
45 |
void qlist_append_obj(QList *qlist, QObject *value)
|
46 |
{ |
47 |
QListEntry *entry; |
48 |
|
49 |
entry = qemu_malloc(sizeof(*entry));
|
50 |
entry->value = value; |
51 |
|
52 |
QTAILQ_INSERT_TAIL(&qlist->head, entry, next); |
53 |
} |
54 |
|
55 |
/**
|
56 |
* qlist_iter(): Iterate over all the list's stored values.
|
57 |
*
|
58 |
* This function allows the user to provide an iterator, which will be
|
59 |
* called for each stored value in the list.
|
60 |
*/
|
61 |
void qlist_iter(const QList *qlist, |
62 |
void (*iter)(QObject *obj, void *opaque), void *opaque) |
63 |
{ |
64 |
QListEntry *entry; |
65 |
|
66 |
QTAILQ_FOREACH(entry, &qlist->head, next) |
67 |
iter(entry->value, opaque); |
68 |
} |
69 |
|
70 |
/**
|
71 |
* qobject_to_qlist(): Convert a QObject into a QList
|
72 |
*/
|
73 |
QList *qobject_to_qlist(const QObject *obj)
|
74 |
{ |
75 |
if (qobject_type(obj) != QTYPE_QLIST) {
|
76 |
return NULL; |
77 |
} |
78 |
|
79 |
return container_of(obj, QList, base);
|
80 |
} |
81 |
|
82 |
/**
|
83 |
* qlist_destroy_obj(): Free all the memory allocated by a QList
|
84 |
*/
|
85 |
static void qlist_destroy_obj(QObject *obj) |
86 |
{ |
87 |
QList *qlist; |
88 |
QListEntry *entry, *next_entry; |
89 |
|
90 |
assert(obj != NULL);
|
91 |
qlist = qobject_to_qlist(obj); |
92 |
|
93 |
QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) { |
94 |
QTAILQ_REMOVE(&qlist->head, entry, next); |
95 |
qobject_decref(entry->value); |
96 |
qemu_free(entry); |
97 |
} |
98 |
|
99 |
qemu_free(qlist); |
100 |
} |