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