1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "qapi/qmp/qnum.h"
15#include "qapi/qmp/qdict.h"
16#include "qapi/qmp/qbool.h"
17#include "qapi/qmp/qnull.h"
18#include "qapi/qmp/qstring.h"
19
20
21
22
23
24
25QDict *qdict_new(void)
26{
27 QDict *qdict;
28
29 qdict = g_malloc0(sizeof(*qdict));
30 qobject_init(QOBJECT(qdict), QTYPE_QDICT);
31
32 return qdict;
33}
34
35
36
37
38
39static unsigned int tdb_hash(const char *name)
40{
41 unsigned value;
42 unsigned i;
43
44
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
53
54static QDictEntry *alloc_entry(const char *key, QObject *value)
55{
56 QDictEntry *entry;
57
58 entry = g_malloc0(sizeof(*entry));
59 entry->key = g_strdup(key);
60 entry->value = value;
61
62 return entry;
63}
64
65
66
67
68
69
70QObject *qdict_entry_value(const QDictEntry *entry)
71{
72 return entry->value;
73}
74
75
76
77
78
79
80
81const char *qdict_entry_key(const QDictEntry *entry)
82{
83 return entry->key;
84}
85
86
87
88
89static QDictEntry *qdict_find(const QDict *qdict,
90 const char *key, unsigned int bucket)
91{
92 QDictEntry *entry;
93
94 QLIST_FOREACH(entry, &qdict->table[bucket], next)
95 if (!strcmp(entry->key, key))
96 return entry;
97
98 return NULL;
99}
100
101
102
103
104
105
106
107
108
109
110
111
112void qdict_put_obj(QDict *qdict, const char *key, QObject *value)
113{
114 unsigned int bucket;
115 QDictEntry *entry;
116
117 bucket = tdb_hash(key) % QDICT_BUCKET_MAX;
118 entry = qdict_find(qdict, key, bucket);
119 if (entry) {
120
121 qobject_unref(entry->value);
122 entry->value = value;
123 } else {
124
125 entry = alloc_entry(key, value);
126 QLIST_INSERT_HEAD(&qdict->table[bucket], entry, next);
127 qdict->size++;
128 }
129}
130
131void qdict_put_int(QDict *qdict, const char *key, int64_t value)
132{
133 qdict_put(qdict, key, qnum_from_int(value));
134}
135
136void qdict_put_bool(QDict *qdict, const char *key, bool value)
137{
138 qdict_put(qdict, key, qbool_from_bool(value));
139}
140
141void qdict_put_str(QDict *qdict, const char *key, const char *value)
142{
143 qdict_put(qdict, key, qstring_from_str(value));
144}
145
146void qdict_put_null(QDict *qdict, const char *key)
147{
148 qdict_put(qdict, key, qnull());
149}
150
151
152
153
154
155
156
157QObject *qdict_get(const QDict *qdict, const char *key)
158{
159 QDictEntry *entry;
160
161 entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX);
162 return (entry == NULL ? NULL : entry->value);
163}
164
165
166
167
168
169
170int qdict_haskey(const QDict *qdict, const char *key)
171{
172 unsigned int bucket = tdb_hash(key) % QDICT_BUCKET_MAX;
173 return (qdict_find(qdict, key, bucket) == NULL ? 0 : 1);
174}
175
176
177
178
179size_t qdict_size(const QDict *qdict)
180{
181 return qdict->size;
182}
183
184
185
186
187
188
189
190
191double qdict_get_double(const QDict *qdict, const char *key)
192{
193 return qnum_get_double(qobject_to(QNum, qdict_get(qdict, key)));
194}
195
196
197
198
199
200
201
202
203
204int64_t qdict_get_int(const QDict *qdict, const char *key)
205{
206 return qnum_get_int(qobject_to(QNum, qdict_get(qdict, key)));
207}
208
209
210
211
212
213
214
215
216
217bool qdict_get_bool(const QDict *qdict, const char *key)
218{
219 return qbool_get_bool(qobject_to(QBool, qdict_get(qdict, key)));
220}
221
222
223
224
225QList *qdict_get_qlist(const QDict *qdict, const char *key)
226{
227 return qobject_to(QList, qdict_get(qdict, key));
228}
229
230
231
232
233QDict *qdict_get_qdict(const QDict *qdict, const char *key)
234{
235 return qobject_to(QDict, qdict_get(qdict, key));
236}
237
238
239
240
241
242
243
244
245
246
247const char *qdict_get_str(const QDict *qdict, const char *key)
248{
249 return qstring_get_str(qobject_to(QString, qdict_get(qdict, key)));
250}
251
252
253
254
255
256
257
258
259int64_t qdict_get_try_int(const QDict *qdict, const char *key,
260 int64_t def_value)
261{
262 QNum *qnum = qobject_to(QNum, qdict_get(qdict, key));
263 int64_t val;
264
265 if (!qnum || !qnum_get_try_int(qnum, &val)) {
266 return def_value;
267 }
268
269 return val;
270}
271
272
273
274
275
276
277
278
279bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value)
280{
281 QBool *qbool = qobject_to(QBool, qdict_get(qdict, key));
282
283 return qbool ? qbool_get_bool(qbool) : def_value;
284}
285
286
287
288
289
290
291
292
293
294const char *qdict_get_try_str(const QDict *qdict, const char *key)
295{
296 QString *qstr = qobject_to(QString, qdict_get(qdict, key));
297
298 return qstr ? qstring_get_str(qstr) : NULL;
299}
300
301
302
303
304
305
306
307void 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
314 for (i = 0; i < QDICT_BUCKET_MAX; i++) {
315 QLIST_FOREACH(entry, &qdict->table[i], next)
316 iter(entry->key, entry->value, opaque);
317 }
318}
319
320static 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
335
336const QDictEntry *qdict_first(const QDict *qdict)
337{
338 return qdict_next_entry(qdict, 0);
339}
340
341
342
343
344const 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
357
358
359
360
361QDict *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) {
371 qdict_put_obj(dest, entry->key, qobject_ref(entry->value));
372 }
373 }
374
375 return dest;
376}
377
378
379
380
381static void qentry_destroy(QDictEntry *e)
382{
383 assert(e != NULL);
384 assert(e->key != NULL);
385 assert(e->value != NULL);
386
387 qobject_unref(e->value);
388 g_free(e->key);
389 g_free(e);
390}
391
392
393
394
395
396
397void qdict_del(QDict *qdict, const char *key)
398{
399 QDictEntry *entry;
400
401 entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX);
402 if (entry) {
403 QLIST_REMOVE(entry, next);
404 qentry_destroy(entry);
405 qdict->size--;
406 }
407}
408
409
410
411
412
413
414
415
416bool qdict_is_equal(const QObject *x, const QObject *y)
417{
418 const QDict *dict_x = qobject_to(QDict, x);
419 const QDict *dict_y = qobject_to(QDict, y);
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
438
439
440
441void qdict_destroy_obj(QObject *obj)
442{
443 int i;
444 QDict *qdict;
445
446 assert(obj != NULL);
447 qdict = qobject_to(QDict, obj);
448
449 for (i = 0; i < QDICT_BUCKET_MAX; i++) {
450 QDictEntry *entry = QLIST_FIRST(&qdict->table[i]);
451 while (entry) {
452 QDictEntry *tmp = QLIST_NEXT(entry, next);
453 QLIST_REMOVE(entry, next);
454 qentry_destroy(entry);
455 entry = tmp;
456 }
457 }
458
459 g_free(qdict);
460}
461