Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 1 | /* |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 2 | * QList Module |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Luiz Capitulino <lcapitulino@redhat.com> |
| 8 | * |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 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. |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 11 | */ |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 12 | |
Peter Maydell | f2ad72b | 2016-01-29 17:50:01 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 14 | #include "qapi/qmp/qbool.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 15 | #include "qapi/qmp/qlist.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 16 | #include "qapi/qmp/qnull.h" |
| 17 | #include "qapi/qmp/qnum.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 18 | #include "qapi/qmp/qstring.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 19 | #include "qemu/queue.h" |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 20 | |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 21 | /** |
| 22 | * qlist_new(): Create a new QList |
| 23 | * |
| 24 | * Return strong reference. |
| 25 | */ |
| 26 | QList *qlist_new(void) |
| 27 | { |
| 28 | QList *qlist; |
| 29 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 30 | qlist = g_malloc(sizeof(*qlist)); |
Eric Blake | 55e1819 | 2015-12-01 22:20:45 -0700 | [diff] [blame] | 31 | qobject_init(QOBJECT(qlist), QTYPE_QLIST); |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 32 | QTAILQ_INIT(&qlist->head); |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 33 | |
| 34 | return qlist; |
| 35 | } |
| 36 | |
Anthony Liguori | 033815f | 2009-11-11 10:48:51 -0600 | [diff] [blame] | 37 | static void qlist_copy_elem(QObject *obj, void *opaque) |
| 38 | { |
| 39 | QList *dst = opaque; |
| 40 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 41 | qobject_ref(obj); |
Anthony Liguori | 033815f | 2009-11-11 10:48:51 -0600 | [diff] [blame] | 42 | qlist_append_obj(dst, obj); |
| 43 | } |
| 44 | |
| 45 | QList *qlist_copy(QList *src) |
| 46 | { |
| 47 | QList *dst = qlist_new(); |
| 48 | |
| 49 | qlist_iter(src, qlist_copy_elem, dst); |
| 50 | |
| 51 | return dst; |
| 52 | } |
| 53 | |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 54 | /** |
| 55 | * qlist_append_obj(): Append an QObject into QList |
| 56 | * |
| 57 | * NOTE: ownership of 'value' is transferred to the QList |
| 58 | */ |
| 59 | void qlist_append_obj(QList *qlist, QObject *value) |
| 60 | { |
| 61 | QListEntry *entry; |
| 62 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 63 | entry = g_malloc(sizeof(*entry)); |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 64 | entry->value = value; |
| 65 | |
| 66 | QTAILQ_INSERT_TAIL(&qlist->head, entry, next); |
| 67 | } |
| 68 | |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 69 | void qlist_append_int(QList *qlist, int64_t value) |
| 70 | { |
| 71 | qlist_append(qlist, qnum_from_int(value)); |
| 72 | } |
| 73 | |
| 74 | void qlist_append_bool(QList *qlist, bool value) |
| 75 | { |
| 76 | qlist_append(qlist, qbool_from_bool(value)); |
| 77 | } |
| 78 | |
| 79 | void qlist_append_str(QList *qlist, const char *value) |
| 80 | { |
| 81 | qlist_append(qlist, qstring_from_str(value)); |
| 82 | } |
| 83 | |
| 84 | void qlist_append_null(QList *qlist) |
| 85 | { |
| 86 | qlist_append(qlist, qnull()); |
| 87 | } |
| 88 | |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 89 | /** |
| 90 | * qlist_iter(): Iterate over all the list's stored values. |
| 91 | * |
| 92 | * This function allows the user to provide an iterator, which will be |
| 93 | * called for each stored value in the list. |
| 94 | */ |
| 95 | void qlist_iter(const QList *qlist, |
| 96 | void (*iter)(QObject *obj, void *opaque), void *opaque) |
| 97 | { |
| 98 | QListEntry *entry; |
| 99 | |
| 100 | QTAILQ_FOREACH(entry, &qlist->head, next) |
| 101 | iter(entry->value, opaque); |
| 102 | } |
| 103 | |
Anthony Liguori | 033815f | 2009-11-11 10:48:51 -0600 | [diff] [blame] | 104 | QObject *qlist_pop(QList *qlist) |
| 105 | { |
| 106 | QListEntry *entry; |
| 107 | QObject *ret; |
| 108 | |
| 109 | if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) { |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | entry = QTAILQ_FIRST(&qlist->head); |
| 114 | QTAILQ_REMOVE(&qlist->head, entry, next); |
| 115 | |
| 116 | ret = entry->value; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 117 | g_free(entry); |
Anthony Liguori | 033815f | 2009-11-11 10:48:51 -0600 | [diff] [blame] | 118 | |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | QObject *qlist_peek(QList *qlist) |
| 123 | { |
| 124 | QListEntry *entry; |
Anthony Liguori | 033815f | 2009-11-11 10:48:51 -0600 | [diff] [blame] | 125 | |
| 126 | if (qlist == NULL || QTAILQ_EMPTY(&qlist->head)) { |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | entry = QTAILQ_FIRST(&qlist->head); |
| 131 | |
Eduardo Habkost | 9be3859 | 2016-06-13 18:57:58 -0300 | [diff] [blame] | 132 | return entry->value; |
Anthony Liguori | 033815f | 2009-11-11 10:48:51 -0600 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | int qlist_empty(const QList *qlist) |
| 136 | { |
| 137 | return QTAILQ_EMPTY(&qlist->head); |
| 138 | } |
| 139 | |
Michael Roth | a86a4c2 | 2012-08-15 13:45:42 -0500 | [diff] [blame] | 140 | static void qlist_size_iter(QObject *obj, void *opaque) |
| 141 | { |
| 142 | size_t *count = opaque; |
| 143 | (*count)++; |
| 144 | } |
| 145 | |
| 146 | size_t qlist_size(const QList *qlist) |
| 147 | { |
| 148 | size_t count = 0; |
| 149 | qlist_iter(qlist, qlist_size_iter, &count); |
| 150 | return count; |
| 151 | } |
| 152 | |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 153 | /** |
Max Reitz | b38dd67 | 2017-11-14 19:01:25 +0100 | [diff] [blame] | 154 | * qlist_is_equal(): Test whether the two QLists are equal |
| 155 | * |
| 156 | * In order to be considered equal, the respective two objects at each |
| 157 | * index of the two lists have to compare equal (regarding |
| 158 | * qobject_is_equal()), and both lists have to have the same number of |
| 159 | * elements. |
| 160 | * That means both lists have to contain equal objects in equal order. |
| 161 | */ |
| 162 | bool qlist_is_equal(const QObject *x, const QObject *y) |
| 163 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 164 | const QList *list_x = qobject_to(QList, x); |
| 165 | const QList *list_y = qobject_to(QList, y); |
Max Reitz | b38dd67 | 2017-11-14 19:01:25 +0100 | [diff] [blame] | 166 | const QListEntry *entry_x, *entry_y; |
| 167 | |
| 168 | entry_x = qlist_first(list_x); |
| 169 | entry_y = qlist_first(list_y); |
| 170 | |
| 171 | while (entry_x && entry_y) { |
| 172 | if (!qobject_is_equal(qlist_entry_obj(entry_x), |
| 173 | qlist_entry_obj(entry_y))) |
| 174 | { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | entry_x = qlist_next(entry_x); |
| 179 | entry_y = qlist_next(entry_y); |
| 180 | } |
| 181 | |
| 182 | return !entry_x && !entry_y; |
| 183 | } |
| 184 | |
| 185 | /** |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 186 | * qlist_destroy_obj(): Free all the memory allocated by a QList |
| 187 | */ |
Eric Blake | 55e1819 | 2015-12-01 22:20:45 -0700 | [diff] [blame] | 188 | void qlist_destroy_obj(QObject *obj) |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 189 | { |
| 190 | QList *qlist; |
| 191 | QListEntry *entry, *next_entry; |
| 192 | |
| 193 | assert(obj != NULL); |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 194 | qlist = qobject_to(QList, obj); |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 195 | |
| 196 | QTAILQ_FOREACH_SAFE(entry, &qlist->head, next, next_entry) { |
| 197 | QTAILQ_REMOVE(&qlist->head, entry, next); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 198 | qobject_unref(entry->value); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 199 | g_free(entry); |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 200 | } |
| 201 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 202 | g_free(qlist); |
Luiz Capitulino | a6fd08e | 2009-10-07 13:41:48 -0300 | [diff] [blame] | 203 | } |