docs/system/arm/cpu-features.rst: Format literals correctly
[qemu.git] / qobject / qdict.c
blob0216ca7ac1680b2ef28f2dddd6473311110ddd5e
1 /*
2 * QDict Module
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
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.
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 #include "qobject-internal.h"
21 /**
22 * qdict_new(): Create a new QDict
24 * Return strong reference.
26 QDict *qdict_new(void)
28 QDict *qdict;
30 qdict = g_malloc0(sizeof(*qdict));
31 qobject_init(QOBJECT(qdict), QTYPE_QDICT);
33 return qdict;
36 /**
37 * tdb_hash(): based on the hash algorithm from gdbm, via tdb
38 * (from module-init-tools)
40 static unsigned int tdb_hash(const char *name)
42 unsigned value; /* Used to compute the hash value. */
43 unsigned i; /* Used to cycle through random values. */
45 /* Set the initial value from the key size. */
46 for (value = 0x238F13AF * strlen(name), i = 0; name[i]; i++) {
47 value = (value + (((const unsigned char *)name)[i] << (i * 5 % 24)));
50 return (1103515243 * value + 12345);
53 /**
54 * alloc_entry(): allocate a new QDictEntry
56 static QDictEntry *alloc_entry(const char *key, QObject *value)
58 QDictEntry *entry;
60 entry = g_malloc0(sizeof(*entry));
61 entry->key = g_strdup(key);
62 entry->value = value;
64 return entry;
67 /**
68 * qdict_entry_value(): Return qdict entry value
70 * Return weak reference.
72 QObject *qdict_entry_value(const QDictEntry *entry)
74 return entry->value;
77 /**
78 * qdict_entry_key(): Return qdict entry key
80 * Return a *pointer* to the string, it has to be duplicated before being
81 * stored.
83 const char *qdict_entry_key(const QDictEntry *entry)
85 return entry->key;
88 /**
89 * qdict_find(): List lookup function
91 static QDictEntry *qdict_find(const QDict *qdict,
92 const char *key, unsigned int bucket)
94 QDictEntry *entry;
96 QLIST_FOREACH(entry, &qdict->table[bucket], next)
97 if (!strcmp(entry->key, key)) {
98 return entry;
101 return NULL;
105 * qdict_put_obj(): Put a new QObject into the dictionary
107 * Insert the pair 'key:value' into 'qdict', if 'key' already exists
108 * its 'value' will be replaced.
110 * This is done by freeing the reference to the stored QObject and
111 * storing the new one in the same entry.
113 * NOTE: ownership of 'value' is transferred to the QDict
115 void qdict_put_obj(QDict *qdict, const char *key, QObject *value)
117 unsigned int bucket;
118 QDictEntry *entry;
120 bucket = tdb_hash(key) % QDICT_BUCKET_MAX;
121 entry = qdict_find(qdict, key, bucket);
122 if (entry) {
123 /* replace key's value */
124 qobject_unref(entry->value);
125 entry->value = value;
126 } else {
127 /* allocate a new entry */
128 entry = alloc_entry(key, value);
129 QLIST_INSERT_HEAD(&qdict->table[bucket], entry, next);
130 qdict->size++;
134 void qdict_put_int(QDict *qdict, const char *key, int64_t value)
136 qdict_put(qdict, key, qnum_from_int(value));
139 void qdict_put_bool(QDict *qdict, const char *key, bool value)
141 qdict_put(qdict, key, qbool_from_bool(value));
144 void qdict_put_str(QDict *qdict, const char *key, const char *value)
146 qdict_put(qdict, key, qstring_from_str(value));
149 void qdict_put_null(QDict *qdict, const char *key)
151 qdict_put(qdict, key, qnull());
155 * qdict_get(): Lookup for a given 'key'
157 * Return a weak reference to the QObject associated with 'key' if
158 * 'key' is present in the dictionary, NULL otherwise.
160 QObject *qdict_get(const QDict *qdict, const char *key)
162 QDictEntry *entry;
164 entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX);
165 return (entry == NULL ? NULL : entry->value);
169 * qdict_haskey(): Check if 'key' exists
171 * Return 1 if 'key' exists in the dict, 0 otherwise
173 int qdict_haskey(const QDict *qdict, const char *key)
175 unsigned int bucket = tdb_hash(key) % QDICT_BUCKET_MAX;
176 return (qdict_find(qdict, key, bucket) == NULL ? 0 : 1);
180 * qdict_size(): Return the size of the dictionary
182 size_t qdict_size(const QDict *qdict)
184 return qdict->size;
188 * qdict_get_double(): Get an number mapped by 'key'
190 * This function assumes that 'key' exists and it stores a QNum.
192 * Return number mapped by 'key'.
194 double qdict_get_double(const QDict *qdict, const char *key)
196 return qnum_get_double(qobject_to(QNum, qdict_get(qdict, key)));
200 * qdict_get_int(): Get an integer mapped by 'key'
202 * This function assumes that 'key' exists and it stores a
203 * QNum representable as int.
205 * Return integer mapped by 'key'.
207 int64_t qdict_get_int(const QDict *qdict, const char *key)
209 return qnum_get_int(qobject_to(QNum, qdict_get(qdict, key)));
213 * qdict_get_bool(): Get a bool mapped by 'key'
215 * This function assumes that 'key' exists and it stores a
216 * QBool object.
218 * Return bool mapped by 'key'.
220 bool qdict_get_bool(const QDict *qdict, const char *key)
222 return qbool_get_bool(qobject_to(QBool, qdict_get(qdict, key)));
226 * qdict_get_qlist(): If @qdict maps @key to a QList, return it, else NULL.
228 QList *qdict_get_qlist(const QDict *qdict, const char *key)
230 return qobject_to(QList, qdict_get(qdict, key));
234 * qdict_get_qdict(): If @qdict maps @key to a QDict, return it, else NULL.
236 QDict *qdict_get_qdict(const QDict *qdict, const char *key)
238 return qobject_to(QDict, qdict_get(qdict, key));
242 * qdict_get_str(): Get a pointer to the stored string mapped
243 * by 'key'
245 * This function assumes that 'key' exists and it stores a
246 * QString object.
248 * Return pointer to the string mapped by 'key'.
250 const char *qdict_get_str(const QDict *qdict, const char *key)
252 return qstring_get_str(qobject_to(QString, qdict_get(qdict, key)));
256 * qdict_get_try_int(): Try to get integer mapped by 'key'
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.
262 int64_t qdict_get_try_int(const QDict *qdict, const char *key,
263 int64_t def_value)
265 QNum *qnum = qobject_to(QNum, qdict_get(qdict, key));
266 int64_t val;
268 if (!qnum || !qnum_get_try_int(qnum, &val)) {
269 return def_value;
272 return val;
276 * qdict_get_try_bool(): Try to get a bool mapped by 'key'
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.
282 bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value)
284 QBool *qbool = qobject_to(QBool, qdict_get(qdict, key));
286 return qbool ? qbool_get_bool(qbool) : def_value;
290 * qdict_get_try_str(): Try to get a pointer to the stored string
291 * mapped by 'key'
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.
297 const char *qdict_get_try_str(const QDict *qdict, const char *key)
299 QString *qstr = qobject_to(QString, qdict_get(qdict, key));
301 return qstr ? qstring_get_str(qstr) : NULL;
304 static QDictEntry *qdict_next_entry(const QDict *qdict, int first_bucket)
306 int i;
308 for (i = first_bucket; i < QDICT_BUCKET_MAX; i++) {
309 if (!QLIST_EMPTY(&qdict->table[i])) {
310 return QLIST_FIRST(&qdict->table[i]);
314 return NULL;
318 * qdict_first(): Return first qdict entry for iteration.
320 const QDictEntry *qdict_first(const QDict *qdict)
322 return qdict_next_entry(qdict, 0);
326 * qdict_next(): Return next qdict entry in an iteration.
328 const QDictEntry *qdict_next(const QDict *qdict, const QDictEntry *entry)
330 QDictEntry *ret;
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);
338 return ret;
342 * qdict_clone_shallow(): Clones a given QDict. Its entries are not copied, but
343 * another reference is added.
345 QDict *qdict_clone_shallow(const QDict *src)
347 QDict *dest;
348 QDictEntry *entry;
349 int i;
351 dest = qdict_new();
353 for (i = 0; i < QDICT_BUCKET_MAX; i++) {
354 QLIST_FOREACH(entry, &src->table[i], next) {
355 qdict_put_obj(dest, entry->key, qobject_ref(entry->value));
359 return dest;
363 * qentry_destroy(): Free all the memory allocated by a QDictEntry
365 static void qentry_destroy(QDictEntry *e)
367 assert(e != NULL);
368 assert(e->key != NULL);
369 assert(e->value != NULL);
371 qobject_unref(e->value);
372 g_free(e->key);
373 g_free(e);
377 * qdict_del(): Delete a 'key:value' pair from the dictionary
379 * This will destroy all data allocated by this entry.
381 void qdict_del(QDict *qdict, const char *key)
383 QDictEntry *entry;
385 entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX);
386 if (entry) {
387 QLIST_REMOVE(entry, next);
388 qentry_destroy(entry);
389 qdict->size--;
394 * qdict_is_equal(): Test whether the two QDicts are equal
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).
400 bool qdict_is_equal(const QObject *x, const QObject *y)
402 const QDict *dict_x = qobject_to(QDict, x);
403 const QDict *dict_y = qobject_to(QDict, y);
404 const QDictEntry *e;
406 if (qdict_size(dict_x) != qdict_size(dict_y)) {
407 return false;
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));
414 if (!qobject_is_equal(obj_x, obj_y)) {
415 return false;
419 return true;
423 * qdict_destroy_obj(): Free all the memory allocated by a QDict
425 void qdict_destroy_obj(QObject *obj)
427 int i;
428 QDict *qdict;
430 assert(obj != NULL);
431 qdict = qobject_to(QDict, obj);
433 for (i = 0; i < QDICT_BUCKET_MAX; i++) {
434 QDictEntry *entry = QLIST_FIRST(&qdict->table[i]);
435 while (entry) {
436 QDictEntry *tmp = QLIST_NEXT(entry, next);
437 QLIST_REMOVE(entry, next);
438 qentry_destroy(entry);
439 entry = tmp;
443 g_free(qdict);