Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 1 | /* |
Luiz Capitulino | 41836a9 | 2010-05-12 16:34:42 -0300 | [diff] [blame] | 2 | * QDict Module |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -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 | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 11 | */ |
| 12 | |
Peter Maydell | f2ad72b | 2016-01-29 17:50:01 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 14 | #include "qapi/qmp/qnum.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 15 | #include "qapi/qmp/qdict.h" |
| 16 | #include "qapi/qmp/qbool.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 17 | #include "qapi/qmp/qnull.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 18 | #include "qapi/qmp/qstring.h" |
Markus Armbruster | 80d7112 | 2020-12-11 18:11:40 +0100 | [diff] [blame] | 19 | #include "qobject-internal.h" |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 20 | |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 21 | /** |
| 22 | * qdict_new(): Create a new QDict |
| 23 | * |
| 24 | * Return strong reference. |
| 25 | */ |
| 26 | QDict *qdict_new(void) |
| 27 | { |
| 28 | QDict *qdict; |
| 29 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 30 | qdict = g_malloc0(sizeof(*qdict)); |
Eric Blake | 55e1819 | 2015-12-01 22:20:45 -0700 | [diff] [blame] | 31 | qobject_init(QOBJECT(qdict), QTYPE_QDICT); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 32 | |
| 33 | return qdict; |
| 34 | } |
| 35 | |
| 36 | /** |
zhaolichang | e3a6e0d | 2020-09-17 15:50:20 +0800 | [diff] [blame] | 37 | * tdb_hash(): based on the hash algorithm from gdbm, via tdb |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 38 | * (from module-init-tools) |
| 39 | */ |
| 40 | static unsigned int tdb_hash(const char *name) |
| 41 | { |
Zhang Han | be08fb1 | 2020-12-28 15:11:27 +0800 | [diff] [blame] | 42 | unsigned value; /* Used to compute the hash value. */ |
| 43 | unsigned i; /* Used to cycle through random values. */ |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 44 | |
| 45 | /* Set the initial value from the key size. */ |
Zhang Han | 1841f01 | 2020-12-28 15:11:29 +0800 | [diff] [blame] | 46 | for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++) { |
Zhang Han | f3d71c6 | 2020-12-28 15:11:28 +0800 | [diff] [blame] | 47 | value = (value + (((const unsigned char *)name)[i] << (i * 5 % 24))); |
Zhang Han | 1841f01 | 2020-12-28 15:11:29 +0800 | [diff] [blame] | 48 | } |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 49 | |
| 50 | return (1103515243 * value + 12345); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * alloc_entry(): allocate a new QDictEntry |
| 55 | */ |
| 56 | static QDictEntry *alloc_entry(const char *key, QObject *value) |
| 57 | { |
| 58 | QDictEntry *entry; |
| 59 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 60 | entry = g_malloc0(sizeof(*entry)); |
| 61 | entry->key = g_strdup(key); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 62 | entry->value = value; |
| 63 | |
| 64 | return entry; |
| 65 | } |
| 66 | |
| 67 | /** |
Luiz Capitulino | 0d078b2 | 2010-06-07 16:53:51 -0300 | [diff] [blame] | 68 | * qdict_entry_value(): Return qdict entry value |
| 69 | * |
| 70 | * Return weak reference. |
| 71 | */ |
| 72 | QObject *qdict_entry_value(const QDictEntry *entry) |
| 73 | { |
| 74 | return entry->value; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * qdict_entry_key(): Return qdict entry key |
| 79 | * |
| 80 | * Return a *pointer* to the string, it has to be duplicated before being |
| 81 | * stored. |
| 82 | */ |
| 83 | const char *qdict_entry_key(const QDictEntry *entry) |
| 84 | { |
| 85 | return entry->key; |
| 86 | } |
| 87 | |
| 88 | /** |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 89 | * qdict_find(): List lookup function |
| 90 | */ |
| 91 | static QDictEntry *qdict_find(const QDict *qdict, |
Luiz Capitulino | c8bc3cd | 2010-06-07 15:45:22 -0300 | [diff] [blame] | 92 | const char *key, unsigned int bucket) |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 93 | { |
| 94 | QDictEntry *entry; |
| 95 | |
Luiz Capitulino | c8bc3cd | 2010-06-07 15:45:22 -0300 | [diff] [blame] | 96 | QLIST_FOREACH(entry, &qdict->table[bucket], next) |
Zhang Han | 1841f01 | 2020-12-28 15:11:29 +0800 | [diff] [blame] | 97 | if (!strcmp(entry->key, key)) { |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 98 | return entry; |
Zhang Han | 1841f01 | 2020-12-28 15:11:29 +0800 | [diff] [blame] | 99 | } |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 100 | |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * qdict_put_obj(): Put a new QObject into the dictionary |
| 106 | * |
| 107 | * Insert the pair 'key:value' into 'qdict', if 'key' already exists |
| 108 | * its 'value' will be replaced. |
| 109 | * |
| 110 | * This is done by freeing the reference to the stored QObject and |
| 111 | * storing the new one in the same entry. |
| 112 | * |
| 113 | * NOTE: ownership of 'value' is transferred to the QDict |
| 114 | */ |
| 115 | void qdict_put_obj(QDict *qdict, const char *key, QObject *value) |
| 116 | { |
Luiz Capitulino | c8bc3cd | 2010-06-07 15:45:22 -0300 | [diff] [blame] | 117 | unsigned int bucket; |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 118 | QDictEntry *entry; |
| 119 | |
Luiz Capitulino | c8bc3cd | 2010-06-07 15:45:22 -0300 | [diff] [blame] | 120 | bucket = tdb_hash(key) % QDICT_BUCKET_MAX; |
| 121 | entry = qdict_find(qdict, key, bucket); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 122 | if (entry) { |
| 123 | /* replace key's value */ |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 124 | qobject_unref(entry->value); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 125 | entry->value = value; |
| 126 | } else { |
| 127 | /* allocate a new entry */ |
| 128 | entry = alloc_entry(key, value); |
Luiz Capitulino | c8bc3cd | 2010-06-07 15:45:22 -0300 | [diff] [blame] | 129 | QLIST_INSERT_HEAD(&qdict->table[bucket], entry, next); |
Luiz Capitulino | 29ec315 | 2009-12-14 18:53:20 -0200 | [diff] [blame] | 130 | qdict->size++; |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 131 | } |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 132 | } |
| 133 | |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 134 | void qdict_put_int(QDict *qdict, const char *key, int64_t value) |
| 135 | { |
| 136 | qdict_put(qdict, key, qnum_from_int(value)); |
| 137 | } |
| 138 | |
| 139 | void qdict_put_bool(QDict *qdict, const char *key, bool value) |
| 140 | { |
| 141 | qdict_put(qdict, key, qbool_from_bool(value)); |
| 142 | } |
| 143 | |
| 144 | void qdict_put_str(QDict *qdict, const char *key, const char *value) |
| 145 | { |
| 146 | qdict_put(qdict, key, qstring_from_str(value)); |
| 147 | } |
| 148 | |
| 149 | void qdict_put_null(QDict *qdict, const char *key) |
| 150 | { |
| 151 | qdict_put(qdict, key, qnull()); |
| 152 | } |
| 153 | |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 154 | /** |
| 155 | * qdict_get(): Lookup for a given 'key' |
| 156 | * |
| 157 | * Return a weak reference to the QObject associated with 'key' if |
| 158 | * 'key' is present in the dictionary, NULL otherwise. |
| 159 | */ |
| 160 | QObject *qdict_get(const QDict *qdict, const char *key) |
| 161 | { |
| 162 | QDictEntry *entry; |
| 163 | |
Luiz Capitulino | c8bc3cd | 2010-06-07 15:45:22 -0300 | [diff] [blame] | 164 | entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 165 | return (entry == NULL ? NULL : entry->value); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * qdict_haskey(): Check if 'key' exists |
| 170 | * |
| 171 | * Return 1 if 'key' exists in the dict, 0 otherwise |
| 172 | */ |
| 173 | int qdict_haskey(const QDict *qdict, const char *key) |
| 174 | { |
Luiz Capitulino | c8bc3cd | 2010-06-07 15:45:22 -0300 | [diff] [blame] | 175 | unsigned int bucket = tdb_hash(key) % QDICT_BUCKET_MAX; |
| 176 | return (qdict_find(qdict, key, bucket) == NULL ? 0 : 1); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * qdict_size(): Return the size of the dictionary |
| 181 | */ |
| 182 | size_t qdict_size(const QDict *qdict) |
| 183 | { |
| 184 | return qdict->size; |
| 185 | } |
| 186 | |
| 187 | /** |
Markus Armbruster | acc3b03 | 2010-01-27 17:16:38 +0100 | [diff] [blame] | 188 | * qdict_get_double(): Get an number mapped by 'key' |
| 189 | * |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 190 | * This function assumes that 'key' exists and it stores a QNum. |
Markus Armbruster | acc3b03 | 2010-01-27 17:16:38 +0100 | [diff] [blame] | 191 | * |
| 192 | * Return number mapped by 'key'. |
| 193 | */ |
| 194 | double qdict_get_double(const QDict *qdict, const char *key) |
| 195 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 196 | return qnum_get_double(qobject_to(QNum, qdict_get(qdict, key))); |
Markus Armbruster | acc3b03 | 2010-01-27 17:16:38 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 200 | * qdict_get_int(): Get an integer mapped by 'key' |
| 201 | * |
| 202 | * This function assumes that 'key' exists and it stores a |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 203 | * QNum representable as int. |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 204 | * |
| 205 | * Return integer mapped by 'key'. |
| 206 | */ |
| 207 | int64_t qdict_get_int(const QDict *qdict, const char *key) |
| 208 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 209 | return qnum_get_int(qobject_to(QNum, qdict_get(qdict, key))); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
Luiz Capitulino | cd4dde3 | 2009-12-10 17:15:53 -0200 | [diff] [blame] | 213 | * qdict_get_bool(): Get a bool mapped by 'key' |
| 214 | * |
| 215 | * This function assumes that 'key' exists and it stores a |
| 216 | * QBool object. |
| 217 | * |
| 218 | * Return bool mapped by 'key'. |
| 219 | */ |
Eric Blake | 34acbc9 | 2015-05-15 16:25:00 -0600 | [diff] [blame] | 220 | bool qdict_get_bool(const QDict *qdict, const char *key) |
Luiz Capitulino | cd4dde3 | 2009-12-10 17:15:53 -0200 | [diff] [blame] | 221 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 222 | return qbool_get_bool(qobject_to(QBool, qdict_get(qdict, key))); |
Luiz Capitulino | cd4dde3 | 2009-12-10 17:15:53 -0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /** |
Markus Armbruster | b25f23e | 2017-02-17 21:38:12 +0100 | [diff] [blame] | 226 | * qdict_get_qlist(): If @qdict maps @key to a QList, return it, else NULL. |
Luiz Capitulino | f2e1750 | 2009-12-10 17:15:54 -0200 | [diff] [blame] | 227 | */ |
| 228 | QList *qdict_get_qlist(const QDict *qdict, const char *key) |
| 229 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 230 | return qobject_to(QList, qdict_get(qdict, key)); |
Luiz Capitulino | f2e1750 | 2009-12-10 17:15:54 -0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
Markus Armbruster | b25f23e | 2017-02-17 21:38:12 +0100 | [diff] [blame] | 234 | * qdict_get_qdict(): If @qdict maps @key to a QDict, return it, else NULL. |
Luiz Capitulino | df10ce6 | 2010-01-21 19:15:39 -0200 | [diff] [blame] | 235 | */ |
| 236 | QDict *qdict_get_qdict(const QDict *qdict, const char *key) |
| 237 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 238 | return qobject_to(QDict, qdict_get(qdict, key)); |
Luiz Capitulino | df10ce6 | 2010-01-21 19:15:39 -0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 242 | * qdict_get_str(): Get a pointer to the stored string mapped |
| 243 | * by 'key' |
| 244 | * |
| 245 | * This function assumes that 'key' exists and it stores a |
| 246 | * QString object. |
| 247 | * |
| 248 | * Return pointer to the string mapped by 'key'. |
| 249 | */ |
| 250 | const char *qdict_get_str(const QDict *qdict, const char *key) |
| 251 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 252 | return qstring_get_str(qobject_to(QString, qdict_get(qdict, key))); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /** |
| 256 | * qdict_get_try_int(): Try to get integer mapped by 'key' |
| 257 | * |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 258 | * Return integer mapped by 'key', if it is not present in the |
| 259 | * dictionary or if the stored object is not a QNum representing an |
| 260 | * integer, 'def_value' will be returned. |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 261 | */ |
| 262 | int64_t qdict_get_try_int(const QDict *qdict, const char *key, |
Luiz Capitulino | 83aba69 | 2010-06-04 19:20:54 -0300 | [diff] [blame] | 263 | int64_t def_value) |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 264 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 265 | QNum *qnum = qobject_to(QNum, qdict_get(qdict, key)); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 266 | int64_t val; |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 267 | |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 268 | if (!qnum || !qnum_get_try_int(qnum, &val)) { |
| 269 | return def_value; |
| 270 | } |
| 271 | |
| 272 | return val; |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
Luiz Capitulino | 35006ac | 2010-06-07 17:25:04 -0300 | [diff] [blame] | 276 | * qdict_get_try_bool(): Try to get a bool mapped by 'key' |
| 277 | * |
| 278 | * Return bool mapped by 'key', if it is not present in the |
| 279 | * dictionary or if the stored object is not of QBool type |
| 280 | * 'def_value' will be returned. |
| 281 | */ |
Eric Blake | 34acbc9 | 2015-05-15 16:25:00 -0600 | [diff] [blame] | 282 | bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value) |
Luiz Capitulino | 35006ac | 2010-06-07 17:25:04 -0300 | [diff] [blame] | 283 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 284 | QBool *qbool = qobject_to(QBool, qdict_get(qdict, key)); |
Luiz Capitulino | 35006ac | 2010-06-07 17:25:04 -0300 | [diff] [blame] | 285 | |
Markus Armbruster | 14b6160 | 2015-10-15 16:15:33 +0200 | [diff] [blame] | 286 | return qbool ? qbool_get_bool(qbool) : def_value; |
Luiz Capitulino | 35006ac | 2010-06-07 17:25:04 -0300 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 290 | * qdict_get_try_str(): Try to get a pointer to the stored string |
| 291 | * mapped by 'key' |
| 292 | * |
| 293 | * Return a pointer to the string mapped by 'key', if it is not present |
| 294 | * in the dictionary or if the stored object is not of QString type |
| 295 | * NULL will be returned. |
| 296 | */ |
| 297 | const char *qdict_get_try_str(const QDict *qdict, const char *key) |
| 298 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 299 | QString *qstr = qobject_to(QString, qdict_get(qdict, key)); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 300 | |
Markus Armbruster | 7f02784 | 2015-10-15 16:15:37 +0200 | [diff] [blame] | 301 | return qstr ? qstring_get_str(qstr) : NULL; |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 302 | } |
| 303 | |
Luiz Capitulino | f2b07f3 | 2010-06-07 16:07:29 -0300 | [diff] [blame] | 304 | static QDictEntry *qdict_next_entry(const QDict *qdict, int first_bucket) |
| 305 | { |
| 306 | int i; |
| 307 | |
| 308 | for (i = first_bucket; i < QDICT_BUCKET_MAX; i++) { |
| 309 | if (!QLIST_EMPTY(&qdict->table[i])) { |
| 310 | return QLIST_FIRST(&qdict->table[i]); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * qdict_first(): Return first qdict entry for iteration. |
| 319 | */ |
| 320 | const QDictEntry *qdict_first(const QDict *qdict) |
| 321 | { |
| 322 | return qdict_next_entry(qdict, 0); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * qdict_next(): Return next qdict entry in an iteration. |
| 327 | */ |
| 328 | const QDictEntry *qdict_next(const QDict *qdict, const QDictEntry *entry) |
| 329 | { |
| 330 | QDictEntry *ret; |
| 331 | |
| 332 | ret = QLIST_NEXT(entry, next); |
| 333 | if (!ret) { |
| 334 | unsigned int bucket = tdb_hash(entry->key) % QDICT_BUCKET_MAX; |
| 335 | ret = qdict_next_entry(qdict, bucket + 1); |
| 336 | } |
| 337 | |
| 338 | return ret; |
| 339 | } |
| 340 | |
Luiz Capitulino | 21f800d | 2009-10-13 13:56:58 -0300 | [diff] [blame] | 341 | /** |
Kevin Wolf | b382bc9 | 2013-03-15 10:35:03 +0100 | [diff] [blame] | 342 | * qdict_clone_shallow(): Clones a given QDict. Its entries are not copied, but |
| 343 | * another reference is added. |
| 344 | */ |
| 345 | QDict *qdict_clone_shallow(const QDict *src) |
| 346 | { |
| 347 | QDict *dest; |
| 348 | QDictEntry *entry; |
| 349 | int i; |
| 350 | |
| 351 | dest = qdict_new(); |
| 352 | |
| 353 | for (i = 0; i < QDICT_BUCKET_MAX; i++) { |
| 354 | QLIST_FOREACH(entry, &src->table[i], next) { |
Marc-André Lureau | f5a74a5 | 2018-04-19 17:01:44 +0200 | [diff] [blame] | 355 | qdict_put_obj(dest, entry->key, qobject_ref(entry->value)); |
Kevin Wolf | b382bc9 | 2013-03-15 10:35:03 +0100 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | return dest; |
| 360 | } |
| 361 | |
| 362 | /** |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 363 | * qentry_destroy(): Free all the memory allocated by a QDictEntry |
| 364 | */ |
| 365 | static void qentry_destroy(QDictEntry *e) |
| 366 | { |
| 367 | assert(e != NULL); |
| 368 | assert(e->key != NULL); |
| 369 | assert(e->value != NULL); |
| 370 | |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 371 | qobject_unref(e->value); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 372 | g_free(e->key); |
| 373 | g_free(e); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
| 377 | * qdict_del(): Delete a 'key:value' pair from the dictionary |
| 378 | * |
| 379 | * This will destroy all data allocated by this entry. |
| 380 | */ |
| 381 | void qdict_del(QDict *qdict, const char *key) |
| 382 | { |
| 383 | QDictEntry *entry; |
| 384 | |
Luiz Capitulino | c8bc3cd | 2010-06-07 15:45:22 -0300 | [diff] [blame] | 385 | entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 386 | if (entry) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 387 | QLIST_REMOVE(entry, next); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 388 | qentry_destroy(entry); |
| 389 | qdict->size--; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | /** |
Max Reitz | b38dd67 | 2017-11-14 19:01:25 +0100 | [diff] [blame] | 394 | * qdict_is_equal(): Test whether the two QDicts are equal |
| 395 | * |
| 396 | * Here, equality means whether they contain the same keys and whether |
| 397 | * the respective values are in turn equal (i.e. invoking |
| 398 | * qobject_is_equal() on them yields true). |
| 399 | */ |
| 400 | bool qdict_is_equal(const QObject *x, const QObject *y) |
| 401 | { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 402 | const QDict *dict_x = qobject_to(QDict, x); |
| 403 | const QDict *dict_y = qobject_to(QDict, y); |
Max Reitz | b38dd67 | 2017-11-14 19:01:25 +0100 | [diff] [blame] | 404 | const QDictEntry *e; |
| 405 | |
| 406 | if (qdict_size(dict_x) != qdict_size(dict_y)) { |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | for (e = qdict_first(dict_x); e; e = qdict_next(dict_x, e)) { |
| 411 | const QObject *obj_x = qdict_entry_value(e); |
| 412 | const QObject *obj_y = qdict_get(dict_y, qdict_entry_key(e)); |
| 413 | |
| 414 | if (!qobject_is_equal(obj_x, obj_y)) { |
| 415 | return false; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | /** |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 423 | * qdict_destroy_obj(): Free all the memory allocated by a QDict |
| 424 | */ |
Eric Blake | 55e1819 | 2015-12-01 22:20:45 -0700 | [diff] [blame] | 425 | void qdict_destroy_obj(QObject *obj) |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 426 | { |
| 427 | int i; |
| 428 | QDict *qdict; |
| 429 | |
| 430 | assert(obj != NULL); |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 431 | qdict = qobject_to(QDict, obj); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 432 | |
Luiz Capitulino | c8bc3cd | 2010-06-07 15:45:22 -0300 | [diff] [blame] | 433 | for (i = 0; i < QDICT_BUCKET_MAX; i++) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 434 | QDictEntry *entry = QLIST_FIRST(&qdict->table[i]); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 435 | while (entry) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 436 | QDictEntry *tmp = QLIST_NEXT(entry, next); |
| 437 | QLIST_REMOVE(entry, next); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 438 | qentry_destroy(entry); |
| 439 | entry = tmp; |
| 440 | } |
| 441 | } |
| 442 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 443 | g_free(qdict); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 444 | } |
Marc-André Lureau | d709bbf | 2022-03-23 19:57:20 +0400 | [diff] [blame] | 445 | |
| 446 | void qdict_unref(QDict *q) |
| 447 | { |
| 448 | qobject_unref(q); |
| 449 | } |