4 * Copyright (C) 2009 Red Hat Inc.
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"
21 * qdict_new(): Create a new QDict
23 * Return strong reference.
25 QDict
*qdict_new(void)
29 qdict
= g_malloc0(sizeof(*qdict
));
30 qobject_init(QOBJECT(qdict
), QTYPE_QDICT
);
36 * tdb_hash(): based on the hash agorithm from gdbm, via tdb
37 * (from module-init-tools)
39 static unsigned int tdb_hash(const char *name
)
41 unsigned value
; /* Used to compute the hash value. */
42 unsigned i
; /* Used to cycle through random values. */
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)));
48 return (1103515243 * value
+ 12345);
52 * alloc_entry(): allocate a new QDictEntry
54 static QDictEntry
*alloc_entry(const char *key
, QObject
*value
)
58 entry
= g_malloc0(sizeof(*entry
));
59 entry
->key
= g_strdup(key
);
66 * qdict_entry_value(): Return qdict entry value
68 * Return weak reference.
70 QObject
*qdict_entry_value(const QDictEntry
*entry
)
76 * qdict_entry_key(): Return qdict entry key
78 * Return a *pointer* to the string, it has to be duplicated before being
81 const char *qdict_entry_key(const QDictEntry
*entry
)
87 * qdict_find(): List lookup function
89 static QDictEntry
*qdict_find(const QDict
*qdict
,
90 const char *key
, unsigned int bucket
)
94 QLIST_FOREACH(entry
, &qdict
->table
[bucket
], next
)
95 if (!strcmp(entry
->key
, key
))
102 * qdict_put_obj(): Put a new QObject into the dictionary
104 * Insert the pair 'key:value' into 'qdict', if 'key' already exists
105 * its 'value' will be replaced.
107 * This is done by freeing the reference to the stored QObject and
108 * storing the new one in the same entry.
110 * NOTE: ownership of 'value' is transferred to the QDict
112 void qdict_put_obj(QDict
*qdict
, const char *key
, QObject
*value
)
117 bucket
= tdb_hash(key
) % QDICT_BUCKET_MAX
;
118 entry
= qdict_find(qdict
, key
, bucket
);
120 /* replace key's value */
121 qobject_unref(entry
->value
);
122 entry
->value
= value
;
124 /* allocate a new entry */
125 entry
= alloc_entry(key
, value
);
126 QLIST_INSERT_HEAD(&qdict
->table
[bucket
], entry
, next
);
131 void qdict_put_int(QDict
*qdict
, const char *key
, int64_t value
)
133 qdict_put(qdict
, key
, qnum_from_int(value
));
136 void qdict_put_bool(QDict
*qdict
, const char *key
, bool value
)
138 qdict_put(qdict
, key
, qbool_from_bool(value
));
141 void qdict_put_str(QDict
*qdict
, const char *key
, const char *value
)
143 qdict_put(qdict
, key
, qstring_from_str(value
));
146 void qdict_put_null(QDict
*qdict
, const char *key
)
148 qdict_put(qdict
, key
, qnull());
152 * qdict_get(): Lookup for a given 'key'
154 * Return a weak reference to the QObject associated with 'key' if
155 * 'key' is present in the dictionary, NULL otherwise.
157 QObject
*qdict_get(const QDict
*qdict
, const char *key
)
161 entry
= qdict_find(qdict
, key
, tdb_hash(key
) % QDICT_BUCKET_MAX
);
162 return (entry
== NULL
? NULL
: entry
->value
);
166 * qdict_haskey(): Check if 'key' exists
168 * Return 1 if 'key' exists in the dict, 0 otherwise
170 int qdict_haskey(const QDict
*qdict
, const char *key
)
172 unsigned int bucket
= tdb_hash(key
) % QDICT_BUCKET_MAX
;
173 return (qdict_find(qdict
, key
, bucket
) == NULL
? 0 : 1);
177 * qdict_size(): Return the size of the dictionary
179 size_t qdict_size(const QDict
*qdict
)
185 * qdict_get_double(): Get an number mapped by 'key'
187 * This function assumes that 'key' exists and it stores a QNum.
189 * Return number mapped by 'key'.
191 double qdict_get_double(const QDict
*qdict
, const char *key
)
193 return qnum_get_double(qobject_to(QNum
, qdict_get(qdict
, key
)));
197 * qdict_get_int(): Get an integer mapped by 'key'
199 * This function assumes that 'key' exists and it stores a
200 * QNum representable as int.
202 * Return integer mapped by 'key'.
204 int64_t qdict_get_int(const QDict
*qdict
, const char *key
)
206 return qnum_get_int(qobject_to(QNum
, qdict_get(qdict
, key
)));
210 * qdict_get_bool(): Get a bool mapped by 'key'
212 * This function assumes that 'key' exists and it stores a
215 * Return bool mapped by 'key'.
217 bool qdict_get_bool(const QDict
*qdict
, const char *key
)
219 return qbool_get_bool(qobject_to(QBool
, qdict_get(qdict
, key
)));
223 * qdict_get_qlist(): If @qdict maps @key to a QList, return it, else NULL.
225 QList
*qdict_get_qlist(const QDict
*qdict
, const char *key
)
227 return qobject_to(QList
, qdict_get(qdict
, key
));
231 * qdict_get_qdict(): If @qdict maps @key to a QDict, return it, else NULL.
233 QDict
*qdict_get_qdict(const QDict
*qdict
, const char *key
)
235 return qobject_to(QDict
, qdict_get(qdict
, key
));
239 * qdict_get_str(): Get a pointer to the stored string mapped
242 * This function assumes that 'key' exists and it stores a
245 * Return pointer to the string mapped by 'key'.
247 const char *qdict_get_str(const QDict
*qdict
, const char *key
)
249 return qstring_get_str(qobject_to(QString
, qdict_get(qdict
, key
)));
253 * qdict_get_try_int(): Try to get integer mapped by 'key'
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.
259 int64_t qdict_get_try_int(const QDict
*qdict
, const char *key
,
262 QNum
*qnum
= qobject_to(QNum
, qdict_get(qdict
, key
));
265 if (!qnum
|| !qnum_get_try_int(qnum
, &val
)) {
273 * qdict_get_try_bool(): Try to get a bool mapped by 'key'
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.
279 bool qdict_get_try_bool(const QDict
*qdict
, const char *key
, bool def_value
)
281 QBool
*qbool
= qobject_to(QBool
, qdict_get(qdict
, key
));
283 return qbool
? qbool_get_bool(qbool
) : def_value
;
287 * qdict_get_try_str(): Try to get a pointer to the stored string
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.
294 const char *qdict_get_try_str(const QDict
*qdict
, const char *key
)
296 QString
*qstr
= qobject_to(QString
, qdict_get(qdict
, key
));
298 return qstr
? qstring_get_str(qstr
) : NULL
;
301 static QDictEntry
*qdict_next_entry(const QDict
*qdict
, int first_bucket
)
305 for (i
= first_bucket
; i
< QDICT_BUCKET_MAX
; i
++) {
306 if (!QLIST_EMPTY(&qdict
->table
[i
])) {
307 return QLIST_FIRST(&qdict
->table
[i
]);
315 * qdict_first(): Return first qdict entry for iteration.
317 const QDictEntry
*qdict_first(const QDict
*qdict
)
319 return qdict_next_entry(qdict
, 0);
323 * qdict_next(): Return next qdict entry in an iteration.
325 const QDictEntry
*qdict_next(const QDict
*qdict
, const QDictEntry
*entry
)
329 ret
= QLIST_NEXT(entry
, next
);
331 unsigned int bucket
= tdb_hash(entry
->key
) % QDICT_BUCKET_MAX
;
332 ret
= qdict_next_entry(qdict
, bucket
+ 1);
339 * qdict_clone_shallow(): Clones a given QDict. Its entries are not copied, but
340 * another reference is added.
342 QDict
*qdict_clone_shallow(const QDict
*src
)
350 for (i
= 0; i
< QDICT_BUCKET_MAX
; i
++) {
351 QLIST_FOREACH(entry
, &src
->table
[i
], next
) {
352 qdict_put_obj(dest
, entry
->key
, qobject_ref(entry
->value
));
360 * qentry_destroy(): Free all the memory allocated by a QDictEntry
362 static void qentry_destroy(QDictEntry
*e
)
365 assert(e
->key
!= NULL
);
366 assert(e
->value
!= NULL
);
368 qobject_unref(e
->value
);
374 * qdict_del(): Delete a 'key:value' pair from the dictionary
376 * This will destroy all data allocated by this entry.
378 void qdict_del(QDict
*qdict
, const char *key
)
382 entry
= qdict_find(qdict
, key
, tdb_hash(key
) % QDICT_BUCKET_MAX
);
384 QLIST_REMOVE(entry
, next
);
385 qentry_destroy(entry
);
391 * qdict_is_equal(): Test whether the two QDicts are equal
393 * Here, equality means whether they contain the same keys and whether
394 * the respective values are in turn equal (i.e. invoking
395 * qobject_is_equal() on them yields true).
397 bool qdict_is_equal(const QObject
*x
, const QObject
*y
)
399 const QDict
*dict_x
= qobject_to(QDict
, x
);
400 const QDict
*dict_y
= qobject_to(QDict
, y
);
403 if (qdict_size(dict_x
) != qdict_size(dict_y
)) {
407 for (e
= qdict_first(dict_x
); e
; e
= qdict_next(dict_x
, e
)) {
408 const QObject
*obj_x
= qdict_entry_value(e
);
409 const QObject
*obj_y
= qdict_get(dict_y
, qdict_entry_key(e
));
411 if (!qobject_is_equal(obj_x
, obj_y
)) {
420 * qdict_destroy_obj(): Free all the memory allocated by a QDict
422 void qdict_destroy_obj(QObject
*obj
)
428 qdict
= qobject_to(QDict
, obj
);
430 for (i
= 0; i
< QDICT_BUCKET_MAX
; i
++) {
431 QDictEntry
*entry
= QLIST_FIRST(&qdict
->table
[i
]);
433 QDictEntry
*tmp
= QLIST_NEXT(entry
, next
);
434 QLIST_REMOVE(entry
, next
);
435 qentry_destroy(entry
);