4 * Copyright (C) 2009 Red Hat Inc.
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.
18 #include "qemu-queue.h"
19 #include "qemu-common.h"
21 static void qdict_destroy_obj(QObject
*obj
);
23 static const QType qdict_type
= {
25 .destroy
= qdict_destroy_obj
,
29 * qdict_new(): Create a new QDict
31 * Return strong reference.
33 QDict
*qdict_new(void)
37 qdict
= qemu_mallocz(sizeof(*qdict
));
38 QOBJECT_INIT(qdict
, &qdict_type
);
44 * qobject_to_qdict(): Convert a QObject into a QDict
46 QDict
*qobject_to_qdict(const QObject
*obj
)
48 if (qobject_type(obj
) != QTYPE_QDICT
)
51 return container_of(obj
, QDict
, base
);
55 * tdb_hash(): based on the hash agorithm from gdbm, via tdb
56 * (from module-init-tools)
58 static unsigned int tdb_hash(const char *name
)
60 unsigned value
; /* Used to compute the hash value. */
61 unsigned i
; /* Used to cycle through random values. */
63 /* Set the initial value from the key size. */
64 for (value
= 0x238F13AF * strlen(name
), i
=0; name
[i
]; i
++)
65 value
= (value
+ (((const unsigned char *)name
)[i
] << (i
*5 % 24)));
67 return (1103515243 * value
+ 12345);
71 * alloc_entry(): allocate a new QDictEntry
73 static QDictEntry
*alloc_entry(const char *key
, QObject
*value
)
77 entry
= qemu_mallocz(sizeof(*entry
));
78 entry
->key
= qemu_strdup(key
);
85 * qdict_find(): List lookup function
87 static QDictEntry
*qdict_find(const QDict
*qdict
,
88 const char *key
, unsigned int hash
)
92 QLIST_FOREACH(entry
, &qdict
->table
[hash
], next
)
93 if (!strcmp(entry
->key
, key
))
100 * qdict_put_obj(): Put a new QObject into the dictionary
102 * Insert the pair 'key:value' into 'qdict', if 'key' already exists
103 * its 'value' will be replaced.
105 * This is done by freeing the reference to the stored QObject and
106 * storing the new one in the same entry.
108 * NOTE: ownership of 'value' is transferred to the QDict
110 void qdict_put_obj(QDict
*qdict
, const char *key
, QObject
*value
)
115 hash
= tdb_hash(key
) % QDICT_HASH_SIZE
;
116 entry
= qdict_find(qdict
, key
, hash
);
118 /* replace key's value */
119 qobject_decref(entry
->value
);
120 entry
->value
= value
;
122 /* allocate a new entry */
123 entry
= alloc_entry(key
, value
);
124 QLIST_INSERT_HEAD(&qdict
->table
[hash
], entry
, next
);
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
)
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
)
164 * qdict_get_obj(): Get a QObject of a specific type
166 static QObject
*qdict_get_obj(const QDict
*qdict
, const char *key
,
171 obj
= qdict_get(qdict
, key
);
173 assert(qobject_type(obj
) == type
);
179 * qdict_get_int(): Get an integer mapped by 'key'
181 * This function assumes that 'key' exists and it stores a
184 * Return integer mapped by 'key'.
186 int64_t qdict_get_int(const QDict
*qdict
, const char *key
)
188 QObject
*obj
= qdict_get_obj(qdict
, key
, QTYPE_QINT
);
189 return qint_get_int(qobject_to_qint(obj
));
193 * qdict_get_bool(): Get a bool mapped by 'key'
195 * This function assumes that 'key' exists and it stores a
198 * Return bool mapped by 'key'.
200 int qdict_get_bool(const QDict
*qdict
, const char *key
)
202 QObject
*obj
= qdict_get_obj(qdict
, key
, QTYPE_QBOOL
);
203 return qbool_get_int(qobject_to_qbool(obj
));
207 * qdict_get_qlist(): Get the QList mapped by 'key'
209 * This function assumes that 'key' exists and it stores a
212 * Return QList mapped by 'key'.
214 QList
*qdict_get_qlist(const QDict
*qdict
, const char *key
)
216 return qobject_to_qlist(qdict_get_obj(qdict
, key
, QTYPE_QLIST
));
220 * qdict_get_str(): Get a pointer to the stored string mapped
223 * This function assumes that 'key' exists and it stores a
226 * Return pointer to the string mapped by 'key'.
228 const char *qdict_get_str(const QDict
*qdict
, const char *key
)
230 QObject
*obj
= qdict_get_obj(qdict
, key
, QTYPE_QSTRING
);
231 return qstring_get_str(qobject_to_qstring(obj
));
235 * qdict_get_try_int(): Try to get integer mapped by 'key'
237 * Return integer mapped by 'key', if it is not present in
238 * the dictionary or if the stored object is not of QInt type
239 * 'err_value' will be returned.
241 int64_t qdict_get_try_int(const QDict
*qdict
, const char *key
,
246 obj
= qdict_get(qdict
, key
);
247 if (!obj
|| qobject_type(obj
) != QTYPE_QINT
)
250 return qint_get_int(qobject_to_qint(obj
));
254 * qdict_get_try_str(): Try to get a pointer to the stored string
257 * Return a pointer to the string mapped by 'key', if it is not present
258 * in the dictionary or if the stored object is not of QString type
259 * NULL will be returned.
261 const char *qdict_get_try_str(const QDict
*qdict
, const char *key
)
265 obj
= qdict_get(qdict
, key
);
266 if (!obj
|| qobject_type(obj
) != QTYPE_QSTRING
)
269 return qstring_get_str(qobject_to_qstring(obj
));
273 * qdict_iter(): Iterate over all the dictionary's stored values.
275 * This function allows the user to provide an iterator, which will be
276 * called for each stored value in the dictionary.
278 void qdict_iter(const QDict
*qdict
,
279 void (*iter
)(const char *key
, QObject
*obj
, void *opaque
),
285 for (i
= 0; i
< QDICT_HASH_SIZE
; i
++) {
286 QLIST_FOREACH(entry
, &qdict
->table
[i
], next
)
287 iter(entry
->key
, entry
->value
, opaque
);
292 * qentry_destroy(): Free all the memory allocated by a QDictEntry
294 static void qentry_destroy(QDictEntry
*e
)
297 assert(e
->key
!= NULL
);
298 assert(e
->value
!= NULL
);
300 qobject_decref(e
->value
);
306 * qdict_del(): Delete a 'key:value' pair from the dictionary
308 * This will destroy all data allocated by this entry.
310 void qdict_del(QDict
*qdict
, const char *key
)
314 entry
= qdict_find(qdict
, key
, tdb_hash(key
) % QDICT_HASH_SIZE
);
316 QLIST_REMOVE(entry
, next
);
317 qentry_destroy(entry
);
323 * qdict_destroy_obj(): Free all the memory allocated by a QDict
325 static void qdict_destroy_obj(QObject
*obj
)
331 qdict
= qobject_to_qdict(obj
);
333 for (i
= 0; i
< QDICT_HASH_SIZE
; i
++) {
334 QDictEntry
*entry
= QLIST_FIRST(&qdict
->table
[i
]);
336 QDictEntry
*tmp
= QLIST_NEXT(entry
, next
);
337 QLIST_REMOVE(entry
, next
);
338 qentry_destroy(entry
);