]>
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" | |
15 | #include "qstring.h" | |
16 | #include "qobject.h" | |
72cf2d4f | 17 | #include "qemu-queue.h" |
fb08dde0 LC |
18 | #include "qemu-common.h" |
19 | ||
aa43d9cc BS |
20 | static void qdict_destroy_obj(QObject *obj); |
21 | ||
22 | static const QType qdict_type = { | |
23 | .code = QTYPE_QDICT, | |
24 | .destroy = qdict_destroy_obj, | |
25 | }; | |
fb08dde0 LC |
26 | |
27 | /** | |
28 | * qdict_new(): Create a new QDict | |
29 | * | |
30 | * Return strong reference. | |
31 | */ | |
32 | QDict *qdict_new(void) | |
33 | { | |
34 | QDict *qdict; | |
35 | ||
36 | qdict = qemu_mallocz(sizeof(*qdict)); | |
37 | QOBJECT_INIT(qdict, &qdict_type); | |
38 | ||
39 | return qdict; | |
40 | } | |
41 | ||
42 | /** | |
43 | * qobject_to_qdict(): Convert a QObject into a QDict | |
44 | */ | |
45 | QDict *qobject_to_qdict(const QObject *obj) | |
46 | { | |
47 | if (qobject_type(obj) != QTYPE_QDICT) | |
48 | return NULL; | |
49 | ||
50 | return container_of(obj, QDict, base); | |
51 | } | |
52 | ||
53 | /** | |
54 | * tdb_hash(): based on the hash agorithm from gdbm, via tdb | |
55 | * (from module-init-tools) | |
56 | */ | |
57 | static unsigned int tdb_hash(const char *name) | |
58 | { | |
59 | unsigned value; /* Used to compute the hash value. */ | |
60 | unsigned i; /* Used to cycle through random values. */ | |
61 | ||
62 | /* Set the initial value from the key size. */ | |
63 | for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++) | |
64 | value = (value + (((const unsigned char *)name)[i] << (i*5 % 24))); | |
65 | ||
66 | return (1103515243 * value + 12345); | |
67 | } | |
68 | ||
69 | /** | |
70 | * alloc_entry(): allocate a new QDictEntry | |
71 | */ | |
72 | static QDictEntry *alloc_entry(const char *key, QObject *value) | |
73 | { | |
74 | QDictEntry *entry; | |
75 | ||
76 | entry = qemu_mallocz(sizeof(*entry)); | |
77 | entry->key = qemu_strdup(key); | |
78 | entry->value = value; | |
79 | ||
80 | return entry; | |
81 | } | |
82 | ||
83 | /** | |
84 | * qdict_find(): List lookup function | |
85 | */ | |
86 | static QDictEntry *qdict_find(const QDict *qdict, | |
87 | const char *key, unsigned int hash) | |
88 | { | |
89 | QDictEntry *entry; | |
90 | ||
72cf2d4f | 91 | QLIST_FOREACH(entry, &qdict->table[hash], next) |
fb08dde0 LC |
92 | if (!strcmp(entry->key, key)) |
93 | return entry; | |
94 | ||
95 | return NULL; | |
96 | } | |
97 | ||
98 | /** | |
99 | * qdict_put_obj(): Put a new QObject into the dictionary | |
100 | * | |
101 | * Insert the pair 'key:value' into 'qdict', if 'key' already exists | |
102 | * its 'value' will be replaced. | |
103 | * | |
104 | * This is done by freeing the reference to the stored QObject and | |
105 | * storing the new one in the same entry. | |
106 | * | |
107 | * NOTE: ownership of 'value' is transferred to the QDict | |
108 | */ | |
109 | void qdict_put_obj(QDict *qdict, const char *key, QObject *value) | |
110 | { | |
111 | unsigned int hash; | |
112 | QDictEntry *entry; | |
113 | ||
114 | hash = tdb_hash(key) % QDICT_HASH_SIZE; | |
115 | entry = qdict_find(qdict, key, hash); | |
116 | if (entry) { | |
117 | /* replace key's value */ | |
118 | qobject_decref(entry->value); | |
119 | entry->value = value; | |
120 | } else { | |
121 | /* allocate a new entry */ | |
122 | entry = alloc_entry(key, value); | |
72cf2d4f | 123 | QLIST_INSERT_HEAD(&qdict->table[hash], entry, next); |
fb08dde0 LC |
124 | } |
125 | ||
126 | qdict->size++; | |
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_str(): Get a pointer to the stored string mapped | |
193 | * by 'key' | |
194 | * | |
195 | * This function assumes that 'key' exists and it stores a | |
196 | * QString object. | |
197 | * | |
198 | * Return pointer to the string mapped by 'key'. | |
199 | */ | |
200 | const char *qdict_get_str(const QDict *qdict, const char *key) | |
201 | { | |
202 | QObject *obj = qdict_get_obj(qdict, key, QTYPE_QSTRING); | |
203 | return qstring_get_str(qobject_to_qstring(obj)); | |
204 | } | |
205 | ||
206 | /** | |
207 | * qdict_get_try_int(): Try to get integer mapped by 'key' | |
208 | * | |
209 | * Return integer mapped by 'key', if it is not present in | |
210 | * the dictionary or if the stored object is not of QInt type | |
211 | * 'err_value' will be returned. | |
212 | */ | |
213 | int64_t qdict_get_try_int(const QDict *qdict, const char *key, | |
214 | int64_t err_value) | |
215 | { | |
216 | QObject *obj; | |
217 | ||
218 | obj = qdict_get(qdict, key); | |
219 | if (!obj || qobject_type(obj) != QTYPE_QINT) | |
220 | return err_value; | |
221 | ||
222 | return qint_get_int(qobject_to_qint(obj)); | |
223 | } | |
224 | ||
225 | /** | |
226 | * qdict_get_try_str(): Try to get a pointer to the stored string | |
227 | * mapped by 'key' | |
228 | * | |
229 | * Return a pointer to the string mapped by 'key', if it is not present | |
230 | * in the dictionary or if the stored object is not of QString type | |
231 | * NULL will be returned. | |
232 | */ | |
233 | const char *qdict_get_try_str(const QDict *qdict, const char *key) | |
234 | { | |
235 | QObject *obj; | |
236 | ||
237 | obj = qdict_get(qdict, key); | |
238 | if (!obj || qobject_type(obj) != QTYPE_QSTRING) | |
239 | return NULL; | |
240 | ||
241 | return qstring_get_str(qobject_to_qstring(obj)); | |
242 | } | |
243 | ||
21f800d3 LC |
244 | /** |
245 | * qdict_iter(): Iterate over all the dictionary's stored values. | |
246 | * | |
247 | * This function allows the user to provide an iterator, which will be | |
248 | * called for each stored value in the dictionary. | |
249 | */ | |
250 | void qdict_iter(const QDict *qdict, | |
251 | void (*iter)(const char *key, QObject *obj, void *opaque), | |
252 | void *opaque) | |
253 | { | |
254 | int i; | |
255 | QDictEntry *entry; | |
256 | ||
257 | for (i = 0; i < QDICT_HASH_SIZE; i++) { | |
258 | QLIST_FOREACH(entry, &qdict->table[i], next) | |
259 | iter(entry->key, entry->value, opaque); | |
260 | } | |
261 | } | |
262 | ||
fb08dde0 LC |
263 | /** |
264 | * qentry_destroy(): Free all the memory allocated by a QDictEntry | |
265 | */ | |
266 | static void qentry_destroy(QDictEntry *e) | |
267 | { | |
268 | assert(e != NULL); | |
269 | assert(e->key != NULL); | |
270 | assert(e->value != NULL); | |
271 | ||
272 | qobject_decref(e->value); | |
273 | qemu_free(e->key); | |
274 | qemu_free(e); | |
275 | } | |
276 | ||
277 | /** | |
278 | * qdict_del(): Delete a 'key:value' pair from the dictionary | |
279 | * | |
280 | * This will destroy all data allocated by this entry. | |
281 | */ | |
282 | void qdict_del(QDict *qdict, const char *key) | |
283 | { | |
284 | QDictEntry *entry; | |
285 | ||
286 | entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE); | |
287 | if (entry) { | |
72cf2d4f | 288 | QLIST_REMOVE(entry, next); |
fb08dde0 LC |
289 | qentry_destroy(entry); |
290 | qdict->size--; | |
291 | } | |
292 | } | |
293 | ||
294 | /** | |
295 | * qdict_destroy_obj(): Free all the memory allocated by a QDict | |
296 | */ | |
297 | static void qdict_destroy_obj(QObject *obj) | |
298 | { | |
299 | int i; | |
300 | QDict *qdict; | |
301 | ||
302 | assert(obj != NULL); | |
303 | qdict = qobject_to_qdict(obj); | |
304 | ||
305 | for (i = 0; i < QDICT_HASH_SIZE; i++) { | |
72cf2d4f | 306 | QDictEntry *entry = QLIST_FIRST(&qdict->table[i]); |
fb08dde0 | 307 | while (entry) { |
72cf2d4f BS |
308 | QDictEntry *tmp = QLIST_NEXT(entry, next); |
309 | QLIST_REMOVE(entry, next); | |
fb08dde0 LC |
310 | qentry_destroy(entry); |
311 | entry = tmp; | |
312 | } | |
313 | } | |
314 | ||
315 | qemu_free(qdict); | |
316 | } |