]>
git.proxmox.com Git - mirror_qemu.git/blob - qdict.c
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.
19 #include "qemu-queue.h"
20 #include "qemu-common.h"
22 static void qdict_destroy_obj(QObject
*obj
);
24 static const QType qdict_type
= {
26 .destroy
= qdict_destroy_obj
,
30 * qdict_new(): Create a new QDict
32 * Return strong reference.
34 QDict
*qdict_new(void)
38 qdict
= g_malloc0(sizeof(*qdict
));
39 QOBJECT_INIT(qdict
, &qdict_type
);
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
)
52 return container_of(obj
, QDict
, base
);
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);
72 * alloc_entry(): allocate a new QDictEntry
74 static QDictEntry
*alloc_entry(const char *key
, QObject
*value
)
78 entry
= g_malloc0(sizeof(*entry
));
79 entry
->key
= g_strdup(key
);
86 * qdict_entry_value(): Return qdict entry value
88 * Return weak reference.
90 QObject
*qdict_entry_value(const QDictEntry
*entry
)
96 * qdict_entry_key(): Return qdict entry key
98 * Return a *pointer* to the string, it has to be duplicated before being
101 const char *qdict_entry_key(const QDictEntry
*entry
)
107 * qdict_find(): List lookup function
109 static QDictEntry
*qdict_find(const QDict
*qdict
,
110 const char *key
, unsigned int bucket
)
114 QLIST_FOREACH(entry
, &qdict
->table
[bucket
], next
)
115 if (!strcmp(entry
->key
, key
))
122 * qdict_put_obj(): Put a new QObject into the dictionary
124 * Insert the pair 'key:value' into 'qdict', if 'key' already exists
125 * its 'value' will be replaced.
127 * This is done by freeing the reference to the stored QObject and
128 * storing the new one in the same entry.
130 * NOTE: ownership of 'value' is transferred to the QDict
132 void qdict_put_obj(QDict
*qdict
, const char *key
, QObject
*value
)
137 bucket
= tdb_hash(key
) % QDICT_BUCKET_MAX
;
138 entry
= qdict_find(qdict
, key
, bucket
);
140 /* replace key's value */
141 qobject_decref(entry
->value
);
142 entry
->value
= value
;
144 /* allocate a new entry */
145 entry
= alloc_entry(key
, value
);
146 QLIST_INSERT_HEAD(&qdict
->table
[bucket
], entry
, next
);
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_obj(): Get a QObject of a specific type
187 static QObject
*qdict_get_obj(const QDict
*qdict
, const char *key
,
192 obj
= qdict_get(qdict
, key
);
194 assert(qobject_type(obj
) == type
);
200 * qdict_get_double(): Get an number mapped by 'key'
202 * This function assumes that 'key' exists and it stores a
203 * QFloat or QInt object.
205 * Return number mapped by 'key'.
207 double qdict_get_double(const QDict
*qdict
, const char *key
)
209 QObject
*obj
= qdict_get(qdict
, key
);
212 switch (qobject_type(obj
)) {
214 return qfloat_get_double(qobject_to_qfloat(obj
));
216 return qint_get_int(qobject_to_qint(obj
));
223 * qdict_get_int(): Get an integer mapped by 'key'
225 * This function assumes that 'key' exists and it stores a
228 * Return integer mapped by 'key'.
230 int64_t qdict_get_int(const QDict
*qdict
, const char *key
)
232 QObject
*obj
= qdict_get_obj(qdict
, key
, QTYPE_QINT
);
233 return qint_get_int(qobject_to_qint(obj
));
237 * qdict_get_bool(): Get a bool mapped by 'key'
239 * This function assumes that 'key' exists and it stores a
242 * Return bool mapped by 'key'.
244 int qdict_get_bool(const QDict
*qdict
, const char *key
)
246 QObject
*obj
= qdict_get_obj(qdict
, key
, QTYPE_QBOOL
);
247 return qbool_get_int(qobject_to_qbool(obj
));
251 * qdict_get_qlist(): Get the QList mapped by 'key'
253 * This function assumes that 'key' exists and it stores a
256 * Return QList mapped by 'key'.
258 QList
*qdict_get_qlist(const QDict
*qdict
, const char *key
)
260 return qobject_to_qlist(qdict_get_obj(qdict
, key
, QTYPE_QLIST
));
264 * qdict_get_qdict(): Get the QDict mapped by 'key'
266 * This function assumes that 'key' exists and it stores a
269 * Return QDict mapped by 'key'.
271 QDict
*qdict_get_qdict(const QDict
*qdict
, const char *key
)
273 return qobject_to_qdict(qdict_get_obj(qdict
, key
, QTYPE_QDICT
));
277 * qdict_get_str(): Get a pointer to the stored string mapped
280 * This function assumes that 'key' exists and it stores a
283 * Return pointer to the string mapped by 'key'.
285 const char *qdict_get_str(const QDict
*qdict
, const char *key
)
287 QObject
*obj
= qdict_get_obj(qdict
, key
, QTYPE_QSTRING
);
288 return qstring_get_str(qobject_to_qstring(obj
));
292 * qdict_get_try_int(): Try to get integer mapped by 'key'
294 * Return integer mapped by 'key', if it is not present in
295 * the dictionary or if the stored object is not of QInt type
296 * 'def_value' will be returned.
298 int64_t qdict_get_try_int(const QDict
*qdict
, const char *key
,
303 obj
= qdict_get(qdict
, key
);
304 if (!obj
|| qobject_type(obj
) != QTYPE_QINT
)
307 return qint_get_int(qobject_to_qint(obj
));
311 * qdict_get_try_bool(): Try to get a bool mapped by 'key'
313 * Return bool mapped by 'key', if it is not present in the
314 * dictionary or if the stored object is not of QBool type
315 * 'def_value' will be returned.
317 int qdict_get_try_bool(const QDict
*qdict
, const char *key
, int def_value
)
321 obj
= qdict_get(qdict
, key
);
322 if (!obj
|| qobject_type(obj
) != QTYPE_QBOOL
)
325 return qbool_get_int(qobject_to_qbool(obj
));
329 * qdict_get_try_str(): Try to get a pointer to the stored string
332 * Return a pointer to the string mapped by 'key', if it is not present
333 * in the dictionary or if the stored object is not of QString type
334 * NULL will be returned.
336 const char *qdict_get_try_str(const QDict
*qdict
, const char *key
)
340 obj
= qdict_get(qdict
, key
);
341 if (!obj
|| qobject_type(obj
) != QTYPE_QSTRING
)
344 return qstring_get_str(qobject_to_qstring(obj
));
348 * qdict_iter(): Iterate over all the dictionary's stored values.
350 * This function allows the user to provide an iterator, which will be
351 * called for each stored value in the dictionary.
353 void qdict_iter(const QDict
*qdict
,
354 void (*iter
)(const char *key
, QObject
*obj
, void *opaque
),
360 for (i
= 0; i
< QDICT_BUCKET_MAX
; i
++) {
361 QLIST_FOREACH(entry
, &qdict
->table
[i
], next
)
362 iter(entry
->key
, entry
->value
, opaque
);
366 static QDictEntry
*qdict_next_entry(const QDict
*qdict
, int first_bucket
)
370 for (i
= first_bucket
; i
< QDICT_BUCKET_MAX
; i
++) {
371 if (!QLIST_EMPTY(&qdict
->table
[i
])) {
372 return QLIST_FIRST(&qdict
->table
[i
]);
380 * qdict_first(): Return first qdict entry for iteration.
382 const QDictEntry
*qdict_first(const QDict
*qdict
)
384 return qdict_next_entry(qdict
, 0);
388 * qdict_next(): Return next qdict entry in an iteration.
390 const QDictEntry
*qdict_next(const QDict
*qdict
, const QDictEntry
*entry
)
394 ret
= QLIST_NEXT(entry
, next
);
396 unsigned int bucket
= tdb_hash(entry
->key
) % QDICT_BUCKET_MAX
;
397 ret
= qdict_next_entry(qdict
, bucket
+ 1);
404 * qentry_destroy(): Free all the memory allocated by a QDictEntry
406 static void qentry_destroy(QDictEntry
*e
)
409 assert(e
->key
!= NULL
);
410 assert(e
->value
!= NULL
);
412 qobject_decref(e
->value
);
418 * qdict_del(): Delete a 'key:value' pair from the dictionary
420 * This will destroy all data allocated by this entry.
422 void qdict_del(QDict
*qdict
, const char *key
)
426 entry
= qdict_find(qdict
, key
, tdb_hash(key
) % QDICT_BUCKET_MAX
);
428 QLIST_REMOVE(entry
, next
);
429 qentry_destroy(entry
);
435 * qdict_destroy_obj(): Free all the memory allocated by a QDict
437 static void qdict_destroy_obj(QObject
*obj
)
443 qdict
= qobject_to_qdict(obj
);
445 for (i
= 0; i
< QDICT_BUCKET_MAX
; i
++) {
446 QDictEntry
*entry
= QLIST_FIRST(&qdict
->table
[i
]);
448 QDictEntry
*tmp
= QLIST_NEXT(entry
, next
);
449 QLIST_REMOVE(entry
, next
);
450 qentry_destroy(entry
);