root / qdict.c @ df10ce6a
History | View | Annotate | Download (8.3 kB)
1 |
/*
|
---|---|
2 |
* QDict data type.
|
3 |
*
|
4 |
* Copyright (C) 2009 Red Hat Inc.
|
5 |
*
|
6 |
* Authors:
|
7 |
* Luiz Capitulino <lcapitulino@redhat.com>
|
8 |
*
|
9 |
* This work is licensed under the terms of the GNU GPL, version 2. See
|
10 |
* the COPYING file in the top-level directory.
|
11 |
*/
|
12 |
|
13 |
#include "qint.h" |
14 |
#include "qdict.h" |
15 |
#include "qbool.h" |
16 |
#include "qstring.h" |
17 |
#include "qobject.h" |
18 |
#include "qemu-queue.h" |
19 |
#include "qemu-common.h" |
20 |
|
21 |
static void qdict_destroy_obj(QObject *obj); |
22 |
|
23 |
static const QType qdict_type = { |
24 |
.code = QTYPE_QDICT, |
25 |
.destroy = qdict_destroy_obj, |
26 |
}; |
27 |
|
28 |
/**
|
29 |
* qdict_new(): Create a new QDict
|
30 |
*
|
31 |
* Return strong reference.
|
32 |
*/
|
33 |
QDict *qdict_new(void)
|
34 |
{ |
35 |
QDict *qdict; |
36 |
|
37 |
qdict = qemu_mallocz(sizeof(*qdict));
|
38 |
QOBJECT_INIT(qdict, &qdict_type); |
39 |
|
40 |
return qdict;
|
41 |
} |
42 |
|
43 |
/**
|
44 |
* qobject_to_qdict(): Convert a QObject into a QDict
|
45 |
*/
|
46 |
QDict *qobject_to_qdict(const QObject *obj)
|
47 |
{ |
48 |
if (qobject_type(obj) != QTYPE_QDICT)
|
49 |
return NULL; |
50 |
|
51 |
return container_of(obj, QDict, base);
|
52 |
} |
53 |
|
54 |
/**
|
55 |
* tdb_hash(): based on the hash agorithm from gdbm, via tdb
|
56 |
* (from module-init-tools)
|
57 |
*/
|
58 |
static unsigned int tdb_hash(const char *name) |
59 |
{ |
60 |
unsigned value; /* Used to compute the hash value. */ |
61 |
unsigned i; /* Used to cycle through random values. */ |
62 |
|
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))); |
66 |
|
67 |
return (1103515243 * value + 12345); |
68 |
} |
69 |
|
70 |
/**
|
71 |
* alloc_entry(): allocate a new QDictEntry
|
72 |
*/
|
73 |
static QDictEntry *alloc_entry(const char *key, QObject *value) |
74 |
{ |
75 |
QDictEntry *entry; |
76 |
|
77 |
entry = qemu_mallocz(sizeof(*entry));
|
78 |
entry->key = qemu_strdup(key); |
79 |
entry->value = value; |
80 |
|
81 |
return entry;
|
82 |
} |
83 |
|
84 |
/**
|
85 |
* qdict_find(): List lookup function
|
86 |
*/
|
87 |
static QDictEntry *qdict_find(const QDict *qdict, |
88 |
const char *key, unsigned int hash) |
89 |
{ |
90 |
QDictEntry *entry; |
91 |
|
92 |
QLIST_FOREACH(entry, &qdict->table[hash], next) |
93 |
if (!strcmp(entry->key, key))
|
94 |
return entry;
|
95 |
|
96 |
return NULL; |
97 |
} |
98 |
|
99 |
/**
|
100 |
* qdict_put_obj(): Put a new QObject into the dictionary
|
101 |
*
|
102 |
* Insert the pair 'key:value' into 'qdict', if 'key' already exists
|
103 |
* its 'value' will be replaced.
|
104 |
*
|
105 |
* This is done by freeing the reference to the stored QObject and
|
106 |
* storing the new one in the same entry.
|
107 |
*
|
108 |
* NOTE: ownership of 'value' is transferred to the QDict
|
109 |
*/
|
110 |
void qdict_put_obj(QDict *qdict, const char *key, QObject *value) |
111 |
{ |
112 |
unsigned int hash; |
113 |
QDictEntry *entry; |
114 |
|
115 |
hash = tdb_hash(key) % QDICT_HASH_SIZE; |
116 |
entry = qdict_find(qdict, key, hash); |
117 |
if (entry) {
|
118 |
/* replace key's value */
|
119 |
qobject_decref(entry->value); |
120 |
entry->value = value; |
121 |
} else {
|
122 |
/* allocate a new entry */
|
123 |
entry = alloc_entry(key, value); |
124 |
QLIST_INSERT_HEAD(&qdict->table[hash], entry, next); |
125 |
qdict->size++; |
126 |
} |
127 |
} |
128 |
|
129 |
/**
|
130 |
* qdict_get(): Lookup for a given 'key'
|
131 |
*
|
132 |
* Return a weak reference to the QObject associated with 'key' if
|
133 |
* 'key' is present in the dictionary, NULL otherwise.
|
134 |
*/
|
135 |
QObject *qdict_get(const QDict *qdict, const char *key) |
136 |
{ |
137 |
QDictEntry *entry; |
138 |
|
139 |
entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE); |
140 |
return (entry == NULL ? NULL : entry->value); |
141 |
} |
142 |
|
143 |
/**
|
144 |
* qdict_haskey(): Check if 'key' exists
|
145 |
*
|
146 |
* Return 1 if 'key' exists in the dict, 0 otherwise
|
147 |
*/
|
148 |
int qdict_haskey(const QDict *qdict, const char *key) |
149 |
{ |
150 |
unsigned int hash = tdb_hash(key) % QDICT_HASH_SIZE; |
151 |
return (qdict_find(qdict, key, hash) == NULL ? 0 : 1); |
152 |
} |
153 |
|
154 |
/**
|
155 |
* qdict_size(): Return the size of the dictionary
|
156 |
*/
|
157 |
size_t qdict_size(const QDict *qdict)
|
158 |
{ |
159 |
return qdict->size;
|
160 |
} |
161 |
|
162 |
/**
|
163 |
* qdict_get_obj(): Get a QObject of a specific type
|
164 |
*/
|
165 |
static QObject *qdict_get_obj(const QDict *qdict, const char *key, |
166 |
qtype_code type) |
167 |
{ |
168 |
QObject *obj; |
169 |
|
170 |
obj = qdict_get(qdict, key); |
171 |
assert(obj != NULL);
|
172 |
assert(qobject_type(obj) == type); |
173 |
|
174 |
return obj;
|
175 |
} |
176 |
|
177 |
/**
|
178 |
* qdict_get_int(): Get an integer mapped by 'key'
|
179 |
*
|
180 |
* This function assumes that 'key' exists and it stores a
|
181 |
* QInt object.
|
182 |
*
|
183 |
* Return integer mapped by 'key'.
|
184 |
*/
|
185 |
int64_t qdict_get_int(const QDict *qdict, const char *key) |
186 |
{ |
187 |
QObject *obj = qdict_get_obj(qdict, key, QTYPE_QINT); |
188 |
return qint_get_int(qobject_to_qint(obj));
|
189 |
} |
190 |
|
191 |
/**
|
192 |
* qdict_get_bool(): Get a bool mapped by 'key'
|
193 |
*
|
194 |
* This function assumes that 'key' exists and it stores a
|
195 |
* QBool object.
|
196 |
*
|
197 |
* Return bool mapped by 'key'.
|
198 |
*/
|
199 |
int qdict_get_bool(const QDict *qdict, const char *key) |
200 |
{ |
201 |
QObject *obj = qdict_get_obj(qdict, key, QTYPE_QBOOL); |
202 |
return qbool_get_int(qobject_to_qbool(obj));
|
203 |
} |
204 |
|
205 |
/**
|
206 |
* qdict_get_qlist(): Get the QList mapped by 'key'
|
207 |
*
|
208 |
* This function assumes that 'key' exists and it stores a
|
209 |
* QList object.
|
210 |
*
|
211 |
* Return QList mapped by 'key'.
|
212 |
*/
|
213 |
QList *qdict_get_qlist(const QDict *qdict, const char *key) |
214 |
{ |
215 |
return qobject_to_qlist(qdict_get_obj(qdict, key, QTYPE_QLIST));
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* qdict_get_qdict(): Get the QDict mapped by 'key'
|
220 |
*
|
221 |
* This function assumes that 'key' exists and it stores a
|
222 |
* QDict object.
|
223 |
*
|
224 |
* Return QDict mapped by 'key'.
|
225 |
*/
|
226 |
QDict *qdict_get_qdict(const QDict *qdict, const char *key) |
227 |
{ |
228 |
return qobject_to_qdict(qdict_get_obj(qdict, key, QTYPE_QDICT));
|
229 |
} |
230 |
|
231 |
/**
|
232 |
* qdict_get_str(): Get a pointer to the stored string mapped
|
233 |
* by 'key'
|
234 |
*
|
235 |
* This function assumes that 'key' exists and it stores a
|
236 |
* QString object.
|
237 |
*
|
238 |
* Return pointer to the string mapped by 'key'.
|
239 |
*/
|
240 |
const char *qdict_get_str(const QDict *qdict, const char *key) |
241 |
{ |
242 |
QObject *obj = qdict_get_obj(qdict, key, QTYPE_QSTRING); |
243 |
return qstring_get_str(qobject_to_qstring(obj));
|
244 |
} |
245 |
|
246 |
/**
|
247 |
* qdict_get_try_int(): Try to get integer mapped by 'key'
|
248 |
*
|
249 |
* Return integer mapped by 'key', if it is not present in
|
250 |
* the dictionary or if the stored object is not of QInt type
|
251 |
* 'err_value' will be returned.
|
252 |
*/
|
253 |
int64_t qdict_get_try_int(const QDict *qdict, const char *key, |
254 |
int64_t err_value) |
255 |
{ |
256 |
QObject *obj; |
257 |
|
258 |
obj = qdict_get(qdict, key); |
259 |
if (!obj || qobject_type(obj) != QTYPE_QINT)
|
260 |
return err_value;
|
261 |
|
262 |
return qint_get_int(qobject_to_qint(obj));
|
263 |
} |
264 |
|
265 |
/**
|
266 |
* qdict_get_try_str(): Try to get a pointer to the stored string
|
267 |
* mapped by 'key'
|
268 |
*
|
269 |
* Return a pointer to the string mapped by 'key', if it is not present
|
270 |
* in the dictionary or if the stored object is not of QString type
|
271 |
* NULL will be returned.
|
272 |
*/
|
273 |
const char *qdict_get_try_str(const QDict *qdict, const char *key) |
274 |
{ |
275 |
QObject *obj; |
276 |
|
277 |
obj = qdict_get(qdict, key); |
278 |
if (!obj || qobject_type(obj) != QTYPE_QSTRING)
|
279 |
return NULL; |
280 |
|
281 |
return qstring_get_str(qobject_to_qstring(obj));
|
282 |
} |
283 |
|
284 |
/**
|
285 |
* qdict_iter(): Iterate over all the dictionary's stored values.
|
286 |
*
|
287 |
* This function allows the user to provide an iterator, which will be
|
288 |
* called for each stored value in the dictionary.
|
289 |
*/
|
290 |
void qdict_iter(const QDict *qdict, |
291 |
void (*iter)(const char *key, QObject *obj, void *opaque), |
292 |
void *opaque)
|
293 |
{ |
294 |
int i;
|
295 |
QDictEntry *entry; |
296 |
|
297 |
for (i = 0; i < QDICT_HASH_SIZE; i++) { |
298 |
QLIST_FOREACH(entry, &qdict->table[i], next) |
299 |
iter(entry->key, entry->value, opaque); |
300 |
} |
301 |
} |
302 |
|
303 |
/**
|
304 |
* qentry_destroy(): Free all the memory allocated by a QDictEntry
|
305 |
*/
|
306 |
static void qentry_destroy(QDictEntry *e) |
307 |
{ |
308 |
assert(e != NULL);
|
309 |
assert(e->key != NULL);
|
310 |
assert(e->value != NULL);
|
311 |
|
312 |
qobject_decref(e->value); |
313 |
qemu_free(e->key); |
314 |
qemu_free(e); |
315 |
} |
316 |
|
317 |
/**
|
318 |
* qdict_del(): Delete a 'key:value' pair from the dictionary
|
319 |
*
|
320 |
* This will destroy all data allocated by this entry.
|
321 |
*/
|
322 |
void qdict_del(QDict *qdict, const char *key) |
323 |
{ |
324 |
QDictEntry *entry; |
325 |
|
326 |
entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE); |
327 |
if (entry) {
|
328 |
QLIST_REMOVE(entry, next); |
329 |
qentry_destroy(entry); |
330 |
qdict->size--; |
331 |
} |
332 |
} |
333 |
|
334 |
/**
|
335 |
* qdict_destroy_obj(): Free all the memory allocated by a QDict
|
336 |
*/
|
337 |
static void qdict_destroy_obj(QObject *obj) |
338 |
{ |
339 |
int i;
|
340 |
QDict *qdict; |
341 |
|
342 |
assert(obj != NULL);
|
343 |
qdict = qobject_to_qdict(obj); |
344 |
|
345 |
for (i = 0; i < QDICT_HASH_SIZE; i++) { |
346 |
QDictEntry *entry = QLIST_FIRST(&qdict->table[i]); |
347 |
while (entry) {
|
348 |
QDictEntry *tmp = QLIST_NEXT(entry, next); |
349 |
QLIST_REMOVE(entry, next); |
350 |
qentry_destroy(entry); |
351 |
entry = tmp; |
352 |
} |
353 |
} |
354 |
|
355 |
qemu_free(qdict); |
356 |
} |