disentangle tcg and deadline calculation
[qemu-kvm.git] / qdict.c
blob7fb425a0cac830197cdf33050caaa6b1ab159c49
1 /*
2 * QDict data type.
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 GPL, version 2. See
10 * the COPYING file in the top-level directory.
13 #include "qint.h"
14 #include "qfloat.h"
15 #include "qdict.h"
16 #include "qbool.h"
17 #include "qstring.h"
18 #include "qobject.h"
19 #include "qemu-queue.h"
20 #include "qemu-common.h"
22 static void qdict_destroy_obj(QObject *obj);
24 static const QType qdict_type = {
25 .code = QTYPE_QDICT,
26 .destroy = qdict_destroy_obj,
29 /**
30 * qdict_new(): Create a new QDict
32 * Return strong reference.
34 QDict *qdict_new(void)
36 QDict *qdict;
38 qdict = qemu_mallocz(sizeof(*qdict));
39 QOBJECT_INIT(qdict, &qdict_type);
41 return qdict;
44 /**
45 * qobject_to_qdict(): Convert a QObject into a QDict
47 QDict *qobject_to_qdict(const QObject *obj)
49 if (qobject_type(obj) != QTYPE_QDICT)
50 return NULL;
52 return container_of(obj, QDict, base);
55 /**
56 * tdb_hash(): based on the hash agorithm from gdbm, via tdb
57 * (from module-init-tools)
59 static unsigned int tdb_hash(const char *name)
61 unsigned value; /* Used to compute the hash value. */
62 unsigned i; /* Used to cycle through random values. */
64 /* Set the initial value from the key size. */
65 for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
66 value = (value + (((const unsigned char *)name)[i] << (i*5 % 24)));
68 return (1103515243 * value + 12345);
71 /**
72 * alloc_entry(): allocate a new QDictEntry
74 static QDictEntry *alloc_entry(const char *key, QObject *value)
76 QDictEntry *entry;
78 entry = qemu_mallocz(sizeof(*entry));
79 entry->key = qemu_strdup(key);
80 entry->value = value;
82 return entry;
85 /**
86 * qdict_find(): List lookup function
88 static QDictEntry *qdict_find(const QDict *qdict,
89 const char *key, unsigned int hash)
91 QDictEntry *entry;
93 QLIST_FOREACH(entry, &qdict->table[hash], next)
94 if (!strcmp(entry->key, key))
95 return entry;
97 return NULL;
101 * qdict_put_obj(): Put a new QObject into the dictionary
103 * Insert the pair 'key:value' into 'qdict', if 'key' already exists
104 * its 'value' will be replaced.
106 * This is done by freeing the reference to the stored QObject and
107 * storing the new one in the same entry.
109 * NOTE: ownership of 'value' is transferred to the QDict
111 void qdict_put_obj(QDict *qdict, const char *key, QObject *value)
113 unsigned int hash;
114 QDictEntry *entry;
116 hash = tdb_hash(key) % QDICT_HASH_SIZE;
117 entry = qdict_find(qdict, key, hash);
118 if (entry) {
119 /* replace key's value */
120 qobject_decref(entry->value);
121 entry->value = value;
122 } else {
123 /* allocate a new entry */
124 entry = alloc_entry(key, value);
125 QLIST_INSERT_HEAD(&qdict->table[hash], entry, next);
126 qdict->size++;
131 * qdict_get(): Lookup for a given 'key'
133 * Return a weak reference to the QObject associated with 'key' if
134 * 'key' is present in the dictionary, NULL otherwise.
136 QObject *qdict_get(const QDict *qdict, const char *key)
138 QDictEntry *entry;
140 entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE);
141 return (entry == NULL ? NULL : entry->value);
145 * qdict_haskey(): Check if 'key' exists
147 * Return 1 if 'key' exists in the dict, 0 otherwise
149 int qdict_haskey(const QDict *qdict, const char *key)
151 unsigned int hash = tdb_hash(key) % QDICT_HASH_SIZE;
152 return (qdict_find(qdict, key, hash) == NULL ? 0 : 1);
156 * qdict_size(): Return the size of the dictionary
158 size_t qdict_size(const QDict *qdict)
160 return qdict->size;
164 * qdict_get_obj(): Get a QObject of a specific type
166 static QObject *qdict_get_obj(const QDict *qdict, const char *key,
167 qtype_code type)
169 QObject *obj;
171 obj = qdict_get(qdict, key);
172 assert(obj != NULL);
173 assert(qobject_type(obj) == type);
175 return obj;
179 * qdict_get_double(): Get an number mapped by 'key'
181 * This function assumes that 'key' exists and it stores a
182 * QFloat or QInt object.
184 * Return number mapped by 'key'.
186 double qdict_get_double(const QDict *qdict, const char *key)
188 QObject *obj = qdict_get(qdict, key);
190 assert(obj);
191 switch (qobject_type(obj)) {
192 case QTYPE_QFLOAT:
193 return qfloat_get_double(qobject_to_qfloat(obj));
194 case QTYPE_QINT:
195 return qint_get_int(qobject_to_qint(obj));
196 default:
197 assert(0);
198 return 0.0;
203 * qdict_get_int(): Get an integer mapped by 'key'
205 * This function assumes that 'key' exists and it stores a
206 * QInt object.
208 * Return integer mapped by 'key'.
210 int64_t qdict_get_int(const QDict *qdict, const char *key)
212 QObject *obj = qdict_get_obj(qdict, key, QTYPE_QINT);
213 return qint_get_int(qobject_to_qint(obj));
217 * qdict_get_bool(): Get a bool mapped by 'key'
219 * This function assumes that 'key' exists and it stores a
220 * QBool object.
222 * Return bool mapped by 'key'.
224 int qdict_get_bool(const QDict *qdict, const char *key)
226 QObject *obj = qdict_get_obj(qdict, key, QTYPE_QBOOL);
227 return qbool_get_int(qobject_to_qbool(obj));
231 * qdict_get_qlist(): Get the QList mapped by 'key'
233 * This function assumes that 'key' exists and it stores a
234 * QList object.
236 * Return QList mapped by 'key'.
238 QList *qdict_get_qlist(const QDict *qdict, const char *key)
240 return qobject_to_qlist(qdict_get_obj(qdict, key, QTYPE_QLIST));
244 * qdict_get_qdict(): Get the QDict mapped by 'key'
246 * This function assumes that 'key' exists and it stores a
247 * QDict object.
249 * Return QDict mapped by 'key'.
251 QDict *qdict_get_qdict(const QDict *qdict, const char *key)
253 return qobject_to_qdict(qdict_get_obj(qdict, key, QTYPE_QDICT));
257 * qdict_get_str(): Get a pointer to the stored string mapped
258 * by 'key'
260 * This function assumes that 'key' exists and it stores a
261 * QString object.
263 * Return pointer to the string mapped by 'key'.
265 const char *qdict_get_str(const QDict *qdict, const char *key)
267 QObject *obj = qdict_get_obj(qdict, key, QTYPE_QSTRING);
268 return qstring_get_str(qobject_to_qstring(obj));
272 * qdict_get_try_int(): Try to get integer mapped by 'key'
274 * Return integer mapped by 'key', if it is not present in
275 * the dictionary or if the stored object is not of QInt type
276 * 'err_value' will be returned.
278 int64_t qdict_get_try_int(const QDict *qdict, const char *key,
279 int64_t err_value)
281 QObject *obj;
283 obj = qdict_get(qdict, key);
284 if (!obj || qobject_type(obj) != QTYPE_QINT)
285 return err_value;
287 return qint_get_int(qobject_to_qint(obj));
291 * qdict_get_try_str(): Try to get a pointer to the stored string
292 * mapped by 'key'
294 * Return a pointer to the string mapped by 'key', if it is not present
295 * in the dictionary or if the stored object is not of QString type
296 * NULL will be returned.
298 const char *qdict_get_try_str(const QDict *qdict, const char *key)
300 QObject *obj;
302 obj = qdict_get(qdict, key);
303 if (!obj || qobject_type(obj) != QTYPE_QSTRING)
304 return NULL;
306 return qstring_get_str(qobject_to_qstring(obj));
310 * qdict_iter(): Iterate over all the dictionary's stored values.
312 * This function allows the user to provide an iterator, which will be
313 * called for each stored value in the dictionary.
315 void qdict_iter(const QDict *qdict,
316 void (*iter)(const char *key, QObject *obj, void *opaque),
317 void *opaque)
319 int i;
320 QDictEntry *entry;
322 for (i = 0; i < QDICT_HASH_SIZE; i++) {
323 QLIST_FOREACH(entry, &qdict->table[i], next)
324 iter(entry->key, entry->value, opaque);
329 * qentry_destroy(): Free all the memory allocated by a QDictEntry
331 static void qentry_destroy(QDictEntry *e)
333 assert(e != NULL);
334 assert(e->key != NULL);
335 assert(e->value != NULL);
337 qobject_decref(e->value);
338 qemu_free(e->key);
339 qemu_free(e);
343 * qdict_del(): Delete a 'key:value' pair from the dictionary
345 * This will destroy all data allocated by this entry.
347 void qdict_del(QDict *qdict, const char *key)
349 QDictEntry *entry;
351 entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE);
352 if (entry) {
353 QLIST_REMOVE(entry, next);
354 qentry_destroy(entry);
355 qdict->size--;
360 * qdict_destroy_obj(): Free all the memory allocated by a QDict
362 static void qdict_destroy_obj(QObject *obj)
364 int i;
365 QDict *qdict;
367 assert(obj != NULL);
368 qdict = qobject_to_qdict(obj);
370 for (i = 0; i < QDICT_HASH_SIZE; i++) {
371 QDictEntry *entry = QLIST_FIRST(&qdict->table[i]);
372 while (entry) {
373 QDictEntry *tmp = QLIST_NEXT(entry, next);
374 QLIST_REMOVE(entry, next);
375 qentry_destroy(entry);
376 entry = tmp;
380 qemu_free(qdict);