Introduce QDict
[qemu] / qdict.c
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 "qstring.h"
16 #include "qobject.h"
17 #include "sys-queue.h"
18 #include "qemu-common.h"
19
20 static const QType qdict_type;
21
22 /**
23  * qdict_new(): Create a new QDict
24  *
25  * Return strong reference.
26  */
27 QDict *qdict_new(void)
28 {
29     QDict *qdict;
30
31     qdict = qemu_mallocz(sizeof(*qdict));
32     QOBJECT_INIT(qdict, &qdict_type);
33
34     return qdict;
35 }
36
37 /**
38  * qobject_to_qdict(): Convert a QObject into a QDict
39  */
40 QDict *qobject_to_qdict(const QObject *obj)
41 {
42     if (qobject_type(obj) != QTYPE_QDICT)
43         return NULL;
44
45     return container_of(obj, QDict, base);
46 }
47
48 /**
49  * tdb_hash(): based on the hash agorithm from gdbm, via tdb
50  * (from module-init-tools)
51  */
52 static unsigned int tdb_hash(const char *name)
53 {
54     unsigned value;     /* Used to compute the hash value.  */
55     unsigned   i;       /* Used to cycle through random values. */
56
57     /* Set the initial value from the key size. */
58     for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
59         value = (value + (((const unsigned char *)name)[i] << (i*5 % 24)));
60
61     return (1103515243 * value + 12345);
62 }
63
64 /**
65  * alloc_entry(): allocate a new QDictEntry
66  */
67 static QDictEntry *alloc_entry(const char *key, QObject *value)
68 {
69     QDictEntry *entry;
70
71     entry = qemu_mallocz(sizeof(*entry));
72     entry->key = qemu_strdup(key);
73     entry->value = value;
74
75     return entry;
76 }
77
78 /**
79  * qdict_find(): List lookup function
80  */
81 static QDictEntry *qdict_find(const QDict *qdict,
82                               const char *key, unsigned int hash)
83 {
84     QDictEntry *entry;
85
86     LIST_FOREACH(entry, &qdict->table[hash], next)
87         if (!strcmp(entry->key, key))
88             return entry;
89
90     return NULL;
91 }
92
93 /**
94  * qdict_put_obj(): Put a new QObject into the dictionary
95  *
96  * Insert the pair 'key:value' into 'qdict', if 'key' already exists
97  * its 'value' will be replaced.
98  *
99  * This is done by freeing the reference to the stored QObject and
100  * storing the new one in the same entry.
101  *
102  * NOTE: ownership of 'value' is transferred to the QDict
103  */
104 void qdict_put_obj(QDict *qdict, const char *key, QObject *value)
105 {
106     unsigned int hash;
107     QDictEntry *entry;
108
109     hash = tdb_hash(key) % QDICT_HASH_SIZE;
110     entry = qdict_find(qdict, key, hash);
111     if (entry) {
112         /* replace key's value */
113         qobject_decref(entry->value);
114         entry->value = value;
115     } else {
116         /* allocate a new entry */
117         entry = alloc_entry(key, value);
118         LIST_INSERT_HEAD(&qdict->table[hash], entry, next);
119     }
120
121     qdict->size++;
122 }
123
124 /**
125  * qdict_get(): Lookup for a given 'key'
126  *
127  * Return a weak reference to the QObject associated with 'key' if
128  * 'key' is present in the dictionary, NULL otherwise.
129  */
130 QObject *qdict_get(const QDict *qdict, const char *key)
131 {
132     QDictEntry *entry;
133
134     entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE);
135     return (entry == NULL ? NULL : entry->value);
136 }
137
138 /**
139  * qdict_haskey(): Check if 'key' exists
140  *
141  * Return 1 if 'key' exists in the dict, 0 otherwise
142  */
143 int qdict_haskey(const QDict *qdict, const char *key)
144 {
145     unsigned int hash = tdb_hash(key) % QDICT_HASH_SIZE;
146     return (qdict_find(qdict, key, hash) == NULL ? 0 : 1);
147 }
148
149 /**
150  * qdict_size(): Return the size of the dictionary
151  */
152 size_t qdict_size(const QDict *qdict)
153 {
154     return qdict->size;
155 }
156
157 /**
158  * qdict_get_obj(): Get a QObject of a specific type
159  */
160 static QObject *qdict_get_obj(const QDict *qdict, const char *key,
161                               qtype_code type)
162 {
163     QObject *obj;
164
165     obj = qdict_get(qdict, key);
166     assert(obj != NULL);
167     assert(qobject_type(obj) == type);
168
169     return obj;
170 }
171
172 /**
173  * qdict_get_int(): Get an integer mapped by 'key'
174  *
175  * This function assumes that 'key' exists and it stores a
176  * QInt object.
177  *
178  * Return integer mapped by 'key'.
179  */
180 int64_t qdict_get_int(const QDict *qdict, const char *key)
181 {
182     QObject *obj = qdict_get_obj(qdict, key, QTYPE_QINT);
183     return qint_get_int(qobject_to_qint(obj));
184 }
185
186 /**
187  * qdict_get_str(): Get a pointer to the stored string mapped
188  * by 'key'
189  *
190  * This function assumes that 'key' exists and it stores a
191  * QString object.
192  *
193  * Return pointer to the string mapped by 'key'.
194  */
195 const char *qdict_get_str(const QDict *qdict, const char *key)
196 {
197     QObject *obj = qdict_get_obj(qdict, key, QTYPE_QSTRING);
198     return qstring_get_str(qobject_to_qstring(obj));
199 }
200
201 /**
202  * qdict_get_try_int(): Try to get integer mapped by 'key'
203  *
204  * Return integer mapped by 'key', if it is not present in
205  * the dictionary or if the stored object is not of QInt type
206  * 'err_value' will be returned.
207  */
208 int64_t qdict_get_try_int(const QDict *qdict, const char *key,
209                           int64_t err_value)
210 {
211     QObject *obj;
212
213     obj = qdict_get(qdict, key);
214     if (!obj || qobject_type(obj) != QTYPE_QINT)
215         return err_value;
216
217     return qint_get_int(qobject_to_qint(obj));
218 }
219
220 /**
221  * qdict_get_try_str(): Try to get a pointer to the stored string
222  * mapped by 'key'
223  *
224  * Return a pointer to the string mapped by 'key', if it is not present
225  * in the dictionary or if the stored object is not of QString type
226  * NULL will be returned.
227  */
228 const char *qdict_get_try_str(const QDict *qdict, const char *key)
229 {
230     QObject *obj;
231
232     obj = qdict_get(qdict, key);
233     if (!obj || qobject_type(obj) != QTYPE_QSTRING)
234         return NULL;
235
236     return qstring_get_str(qobject_to_qstring(obj));
237 }
238
239 /**
240  * qentry_destroy(): Free all the memory allocated by a QDictEntry
241  */
242 static void qentry_destroy(QDictEntry *e)
243 {
244     assert(e != NULL);
245     assert(e->key != NULL);
246     assert(e->value != NULL);
247
248     qobject_decref(e->value);
249     qemu_free(e->key);
250     qemu_free(e);
251 }
252
253 /**
254  * qdict_del(): Delete a 'key:value' pair from the dictionary
255  *
256  * This will destroy all data allocated by this entry.
257  */
258 void qdict_del(QDict *qdict, const char *key)
259 {
260     QDictEntry *entry;
261
262     entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE);
263     if (entry) {
264         LIST_REMOVE(entry, next);
265         qentry_destroy(entry);
266         qdict->size--;
267     }
268 }
269
270 /**
271  * qdict_destroy_obj(): Free all the memory allocated by a QDict
272  */
273 static void qdict_destroy_obj(QObject *obj)
274 {
275     int i;
276     QDict *qdict;
277
278     assert(obj != NULL);
279     qdict = qobject_to_qdict(obj);
280
281     for (i = 0; i < QDICT_HASH_SIZE; i++) {
282         QDictEntry *entry = LIST_FIRST(&qdict->table[i]);
283         while (entry) {
284             QDictEntry *tmp = LIST_NEXT(entry, next);
285             LIST_REMOVE(entry, next);
286             qentry_destroy(entry);
287             entry = tmp;
288         }
289     }
290
291     qemu_free(qdict);
292 }
293
294 static const QType qdict_type = {
295     .code = QTYPE_QDICT,
296     .destroy = qdict_destroy_obj,
297 };