]>
Commit | Line | Data |
---|---|---|
fb08dde0 LC |
1 | /* |
2 | * QDict data type. | |
3 | * | |
4 | * Copyright (C) 2009 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Luiz Capitulino <[email protected]> | |
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" | |
cd4dde36 | 15 | #include "qbool.h" |
fb08dde0 LC |
16 | #include "qstring.h" |
17 | #include "qobject.h" | |
72cf2d4f | 18 | #include "qemu-queue.h" |
fb08dde0 LC |
19 | #include "qemu-common.h" |
20 | ||
aa43d9cc BS |
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 | }; | |
fb08dde0 LC |
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 | ||
72cf2d4f | 92 | QLIST_FOREACH(entry, &qdict->table[hash], next) |
fb08dde0 LC |
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); | |
72cf2d4f | 124 | QLIST_INSERT_HEAD(&qdict->table[hash], entry, next); |
fb08dde0 LC |
125 | } |
126 | ||
127 | qdict->size++; | |
128 | } | |
129 | ||
130 | /** | |
131 | * qdict_get(): Lookup for a given 'key' | |
132 | * | |
133 | * Return a weak reference to the QObject associated with 'key' if | |
134 | * 'key' is present in the dictionary, NULL otherwise. | |
135 | */ | |
136 | QObject *qdict_get(const QDict *qdict, const char *key) | |
137 | { | |
138 | QDictEntry *entry; | |
139 | ||
140 | entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE); | |
141 | return (entry == NULL ? NULL : entry->value); | |
142 | } | |
143 | ||
144 | /** | |
145 | * qdict_haskey(): Check if 'key' exists | |
146 | * | |
147 | * Return 1 if 'key' exists in the dict, 0 otherwise | |
148 | */ | |
149 | int qdict_haskey(const QDict *qdict, const char *key) | |
150 | { | |
151 | unsigned int hash = tdb_hash(key) % QDICT_HASH_SIZE; | |
152 | return (qdict_find(qdict, key, hash) == NULL ? 0 : 1); | |
153 | } | |
154 | ||
155 | /** | |
156 | * qdict_size(): Return the size of the dictionary | |
157 | */ | |
158 | size_t qdict_size(const QDict *qdict) | |
159 | { | |
160 | return qdict->size; | |
161 | } | |
162 | ||
163 | /** | |
164 | * qdict_get_obj(): Get a QObject of a specific type | |
165 | */ | |
166 | static QObject *qdict_get_obj(const QDict *qdict, const char *key, | |
167 | qtype_code type) | |
168 | { | |
169 | QObject *obj; | |
170 | ||
171 | obj = qdict_get(qdict, key); | |
172 | assert(obj != NULL); | |
173 | assert(qobject_type(obj) == type); | |
174 | ||
175 | return obj; | |
176 | } | |
177 | ||
178 | /** | |
179 | * qdict_get_int(): Get an integer mapped by 'key' | |
180 | * | |
181 | * This function assumes that 'key' exists and it stores a | |
182 | * QInt object. | |
183 | * | |
184 | * Return integer mapped by 'key'. | |
185 | */ | |
186 | int64_t qdict_get_int(const QDict *qdict, const char *key) | |
187 | { | |
188 | QObject *obj = qdict_get_obj(qdict, key, QTYPE_QINT); | |
189 | return qint_get_int(qobject_to_qint(obj)); | |
190 | } | |
191 | ||
cd4dde36 LC |
192 | /** |
193 | * qdict_get_bool(): Get a bool mapped by 'key' | |
194 | * | |
195 | * This function assumes that 'key' exists and it stores a | |
196 | * QBool object. | |
197 | * | |
198 | * Return bool mapped by 'key'. | |
199 | */ | |
200 | int qdict_get_bool(const QDict *qdict, const char *key) | |
201 | { | |
202 | QObject *obj = qdict_get_obj(qdict, key, QTYPE_QBOOL); | |
203 | return qbool_get_int(qobject_to_qbool(obj)); | |
204 | } | |
205 | ||
fb08dde0 LC |
206 | /** |
207 | * qdict_get_str(): Get a pointer to the stored string mapped | |
208 | * by 'key' | |
209 | * | |
210 | * This function assumes that 'key' exists and it stores a | |
211 | * QString object. | |
212 | * | |
213 | * Return pointer to the string mapped by 'key'. | |
214 | */ | |
215 | const char *qdict_get_str(const QDict *qdict, const char *key) | |
216 | { | |
217 | QObject *obj = qdict_get_obj(qdict, key, QTYPE_QSTRING); | |
218 | return qstring_get_str(qobject_to_qstring(obj)); | |
219 | } | |
220 | ||
221 | /** | |
222 | * qdict_get_try_int(): Try to get integer mapped by 'key' | |
223 | * | |
224 | * Return integer mapped by 'key', if it is not present in | |
225 | * the dictionary or if the stored object is not of QInt type | |
226 | * 'err_value' will be returned. | |
227 | */ | |
228 | int64_t qdict_get_try_int(const QDict *qdict, const char *key, | |
229 | int64_t err_value) | |
230 | { | |
231 | QObject *obj; | |
232 | ||
233 | obj = qdict_get(qdict, key); | |
234 | if (!obj || qobject_type(obj) != QTYPE_QINT) | |
235 | return err_value; | |
236 | ||
237 | return qint_get_int(qobject_to_qint(obj)); | |
238 | } | |
239 | ||
240 | /** | |
241 | * qdict_get_try_str(): Try to get a pointer to the stored string | |
242 | * mapped by 'key' | |
243 | * | |
244 | * Return a pointer to the string mapped by 'key', if it is not present | |
245 | * in the dictionary or if the stored object is not of QString type | |
246 | * NULL will be returned. | |
247 | */ | |
248 | const char *qdict_get_try_str(const QDict *qdict, const char *key) | |
249 | { | |
250 | QObject *obj; | |
251 | ||
252 | obj = qdict_get(qdict, key); | |
253 | if (!obj || qobject_type(obj) != QTYPE_QSTRING) | |
254 | return NULL; | |
255 | ||
256 | return qstring_get_str(qobject_to_qstring(obj)); | |
257 | } | |
258 | ||
21f800d3 LC |
259 | /** |
260 | * qdict_iter(): Iterate over all the dictionary's stored values. | |
261 | * | |
262 | * This function allows the user to provide an iterator, which will be | |
263 | * called for each stored value in the dictionary. | |
264 | */ | |
265 | void qdict_iter(const QDict *qdict, | |
266 | void (*iter)(const char *key, QObject *obj, void *opaque), | |
267 | void *opaque) | |
268 | { | |
269 | int i; | |
270 | QDictEntry *entry; | |
271 | ||
272 | for (i = 0; i < QDICT_HASH_SIZE; i++) { | |
273 | QLIST_FOREACH(entry, &qdict->table[i], next) | |
274 | iter(entry->key, entry->value, opaque); | |
275 | } | |
276 | } | |
277 | ||
fb08dde0 LC |
278 | /** |
279 | * qentry_destroy(): Free all the memory allocated by a QDictEntry | |
280 | */ | |
281 | static void qentry_destroy(QDictEntry *e) | |
282 | { | |
283 | assert(e != NULL); | |
284 | assert(e->key != NULL); | |
285 | assert(e->value != NULL); | |
286 | ||
287 | qobject_decref(e->value); | |
288 | qemu_free(e->key); | |
289 | qemu_free(e); | |
290 | } | |
291 | ||
292 | /** | |
293 | * qdict_del(): Delete a 'key:value' pair from the dictionary | |
294 | * | |
295 | * This will destroy all data allocated by this entry. | |
296 | */ | |
297 | void qdict_del(QDict *qdict, const char *key) | |
298 | { | |
299 | QDictEntry *entry; | |
300 | ||
301 | entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE); | |
302 | if (entry) { | |
72cf2d4f | 303 | QLIST_REMOVE(entry, next); |
fb08dde0 LC |
304 | qentry_destroy(entry); |
305 | qdict->size--; | |
306 | } | |
307 | } | |
308 | ||
309 | /** | |
310 | * qdict_destroy_obj(): Free all the memory allocated by a QDict | |
311 | */ | |
312 | static void qdict_destroy_obj(QObject *obj) | |
313 | { | |
314 | int i; | |
315 | QDict *qdict; | |
316 | ||
317 | assert(obj != NULL); | |
318 | qdict = qobject_to_qdict(obj); | |
319 | ||
320 | for (i = 0; i < QDICT_HASH_SIZE; i++) { | |
72cf2d4f | 321 | QDictEntry *entry = QLIST_FIRST(&qdict->table[i]); |
fb08dde0 | 322 | while (entry) { |
72cf2d4f BS |
323 | QDictEntry *tmp = QLIST_NEXT(entry, next); |
324 | QLIST_REMOVE(entry, next); | |
fb08dde0 LC |
325 | qentry_destroy(entry); |
326 | entry = tmp; | |
327 | } | |
328 | } | |
329 | ||
330 | qemu_free(qdict); | |
331 | } |