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