Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 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" |
Markus Armbruster | acc3b03 | 2010-01-27 17:16:38 +0100 | [diff] [blame] | 14 | #include "qfloat.h" |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 15 | #include "qdict.h" |
Luiz Capitulino | cd4dde3 | 2009-12-10 17:15:53 -0200 | [diff] [blame] | 16 | #include "qbool.h" |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 17 | #include "qstring.h" |
| 18 | #include "qobject.h" |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 19 | #include "qemu-queue.h" |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 20 | #include "qemu-common.h" |
| 21 | |
Blue Swirl | aa43d9c | 2009-09-04 17:43:37 +0000 | [diff] [blame] | 22 | static void qdict_destroy_obj(QObject *obj); |
| 23 | |
| 24 | static const QType qdict_type = { |
| 25 | .code = QTYPE_QDICT, |
| 26 | .destroy = qdict_destroy_obj, |
| 27 | }; |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * qdict_new(): Create a new QDict |
| 31 | * |
| 32 | * Return strong reference. |
| 33 | */ |
| 34 | QDict *qdict_new(void) |
| 35 | { |
| 36 | QDict *qdict; |
| 37 | |
| 38 | qdict = qemu_mallocz(sizeof(*qdict)); |
| 39 | QOBJECT_INIT(qdict, &qdict_type); |
| 40 | |
| 41 | return qdict; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * qobject_to_qdict(): Convert a QObject into a QDict |
| 46 | */ |
| 47 | QDict *qobject_to_qdict(const QObject *obj) |
| 48 | { |
| 49 | if (qobject_type(obj) != QTYPE_QDICT) |
| 50 | return NULL; |
| 51 | |
| 52 | return container_of(obj, QDict, base); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * tdb_hash(): based on the hash agorithm from gdbm, via tdb |
| 57 | * (from module-init-tools) |
| 58 | */ |
| 59 | static unsigned int tdb_hash(const char *name) |
| 60 | { |
| 61 | unsigned value; /* Used to compute the hash value. */ |
| 62 | unsigned i; /* Used to cycle through random values. */ |
| 63 | |
| 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))); |
| 67 | |
| 68 | return (1103515243 * value + 12345); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * alloc_entry(): allocate a new QDictEntry |
| 73 | */ |
| 74 | static QDictEntry *alloc_entry(const char *key, QObject *value) |
| 75 | { |
| 76 | QDictEntry *entry; |
| 77 | |
| 78 | entry = qemu_mallocz(sizeof(*entry)); |
| 79 | entry->key = qemu_strdup(key); |
| 80 | entry->value = value; |
| 81 | |
| 82 | return entry; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * qdict_find(): List lookup function |
| 87 | */ |
| 88 | static QDictEntry *qdict_find(const QDict *qdict, |
| 89 | const char *key, unsigned int hash) |
| 90 | { |
| 91 | QDictEntry *entry; |
| 92 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 93 | QLIST_FOREACH(entry, &qdict->table[hash], next) |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 94 | if (!strcmp(entry->key, key)) |
| 95 | return entry; |
| 96 | |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * qdict_put_obj(): Put a new QObject into the dictionary |
| 102 | * |
| 103 | * Insert the pair 'key:value' into 'qdict', if 'key' already exists |
| 104 | * its 'value' will be replaced. |
| 105 | * |
| 106 | * This is done by freeing the reference to the stored QObject and |
| 107 | * storing the new one in the same entry. |
| 108 | * |
| 109 | * NOTE: ownership of 'value' is transferred to the QDict |
| 110 | */ |
| 111 | void qdict_put_obj(QDict *qdict, const char *key, QObject *value) |
| 112 | { |
| 113 | unsigned int hash; |
| 114 | QDictEntry *entry; |
| 115 | |
| 116 | hash = tdb_hash(key) % QDICT_HASH_SIZE; |
| 117 | entry = qdict_find(qdict, key, hash); |
| 118 | if (entry) { |
| 119 | /* replace key's value */ |
| 120 | qobject_decref(entry->value); |
| 121 | entry->value = value; |
| 122 | } else { |
| 123 | /* allocate a new entry */ |
| 124 | entry = alloc_entry(key, value); |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 125 | QLIST_INSERT_HEAD(&qdict->table[hash], entry, next); |
Luiz Capitulino | 29ec315 | 2009-12-14 18:53:20 -0200 | [diff] [blame] | 126 | qdict->size++; |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 127 | } |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 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 | /** |
Markus Armbruster | acc3b03 | 2010-01-27 17:16:38 +0100 | [diff] [blame] | 179 | * qdict_get_double(): Get an number mapped by 'key' |
| 180 | * |
| 181 | * This function assumes that 'key' exists and it stores a |
| 182 | * QFloat or QInt object. |
| 183 | * |
| 184 | * Return number mapped by 'key'. |
| 185 | */ |
| 186 | double qdict_get_double(const QDict *qdict, const char *key) |
| 187 | { |
| 188 | QObject *obj = qdict_get(qdict, key); |
| 189 | |
| 190 | assert(obj); |
| 191 | switch (qobject_type(obj)) { |
| 192 | case QTYPE_QFLOAT: |
| 193 | return qfloat_get_double(qobject_to_qfloat(obj)); |
| 194 | case QTYPE_QINT: |
| 195 | return qint_get_int(qobject_to_qint(obj)); |
| 196 | default: |
| 197 | assert(0); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 202 | * qdict_get_int(): Get an integer mapped by 'key' |
| 203 | * |
| 204 | * This function assumes that 'key' exists and it stores a |
| 205 | * QInt object. |
| 206 | * |
| 207 | * Return integer mapped by 'key'. |
| 208 | */ |
| 209 | int64_t qdict_get_int(const QDict *qdict, const char *key) |
| 210 | { |
| 211 | QObject *obj = qdict_get_obj(qdict, key, QTYPE_QINT); |
| 212 | return qint_get_int(qobject_to_qint(obj)); |
| 213 | } |
| 214 | |
| 215 | /** |
Luiz Capitulino | cd4dde3 | 2009-12-10 17:15:53 -0200 | [diff] [blame] | 216 | * qdict_get_bool(): Get a bool mapped by 'key' |
| 217 | * |
| 218 | * This function assumes that 'key' exists and it stores a |
| 219 | * QBool object. |
| 220 | * |
| 221 | * Return bool mapped by 'key'. |
| 222 | */ |
| 223 | int qdict_get_bool(const QDict *qdict, const char *key) |
| 224 | { |
| 225 | QObject *obj = qdict_get_obj(qdict, key, QTYPE_QBOOL); |
| 226 | return qbool_get_int(qobject_to_qbool(obj)); |
| 227 | } |
| 228 | |
| 229 | /** |
Luiz Capitulino | f2e1750 | 2009-12-10 17:15:54 -0200 | [diff] [blame] | 230 | * qdict_get_qlist(): Get the QList mapped by 'key' |
| 231 | * |
| 232 | * This function assumes that 'key' exists and it stores a |
| 233 | * QList object. |
| 234 | * |
| 235 | * Return QList mapped by 'key'. |
| 236 | */ |
| 237 | QList *qdict_get_qlist(const QDict *qdict, const char *key) |
| 238 | { |
| 239 | return qobject_to_qlist(qdict_get_obj(qdict, key, QTYPE_QLIST)); |
| 240 | } |
| 241 | |
| 242 | /** |
Luiz Capitulino | df10ce6 | 2010-01-21 19:15:39 -0200 | [diff] [blame] | 243 | * qdict_get_qdict(): Get the QDict mapped by 'key' |
| 244 | * |
| 245 | * This function assumes that 'key' exists and it stores a |
| 246 | * QDict object. |
| 247 | * |
| 248 | * Return QDict mapped by 'key'. |
| 249 | */ |
| 250 | QDict *qdict_get_qdict(const QDict *qdict, const char *key) |
| 251 | { |
| 252 | return qobject_to_qdict(qdict_get_obj(qdict, key, QTYPE_QDICT)); |
| 253 | } |
| 254 | |
| 255 | /** |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 256 | * qdict_get_str(): Get a pointer to the stored string mapped |
| 257 | * by 'key' |
| 258 | * |
| 259 | * This function assumes that 'key' exists and it stores a |
| 260 | * QString object. |
| 261 | * |
| 262 | * Return pointer to the string mapped by 'key'. |
| 263 | */ |
| 264 | const char *qdict_get_str(const QDict *qdict, const char *key) |
| 265 | { |
| 266 | QObject *obj = qdict_get_obj(qdict, key, QTYPE_QSTRING); |
| 267 | return qstring_get_str(qobject_to_qstring(obj)); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * qdict_get_try_int(): Try to get integer mapped by 'key' |
| 272 | * |
| 273 | * Return integer mapped by 'key', if it is not present in |
| 274 | * the dictionary or if the stored object is not of QInt type |
| 275 | * 'err_value' will be returned. |
| 276 | */ |
| 277 | int64_t qdict_get_try_int(const QDict *qdict, const char *key, |
| 278 | int64_t err_value) |
| 279 | { |
| 280 | QObject *obj; |
| 281 | |
| 282 | obj = qdict_get(qdict, key); |
| 283 | if (!obj || qobject_type(obj) != QTYPE_QINT) |
| 284 | return err_value; |
| 285 | |
| 286 | return qint_get_int(qobject_to_qint(obj)); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * qdict_get_try_str(): Try to get a pointer to the stored string |
| 291 | * mapped by 'key' |
| 292 | * |
| 293 | * Return a pointer to the string mapped by 'key', if it is not present |
| 294 | * in the dictionary or if the stored object is not of QString type |
| 295 | * NULL will be returned. |
| 296 | */ |
| 297 | const char *qdict_get_try_str(const QDict *qdict, const char *key) |
| 298 | { |
| 299 | QObject *obj; |
| 300 | |
| 301 | obj = qdict_get(qdict, key); |
| 302 | if (!obj || qobject_type(obj) != QTYPE_QSTRING) |
| 303 | return NULL; |
| 304 | |
| 305 | return qstring_get_str(qobject_to_qstring(obj)); |
| 306 | } |
| 307 | |
| 308 | /** |
Luiz Capitulino | 21f800d | 2009-10-13 13:56:58 -0300 | [diff] [blame] | 309 | * qdict_iter(): Iterate over all the dictionary's stored values. |
| 310 | * |
| 311 | * This function allows the user to provide an iterator, which will be |
| 312 | * called for each stored value in the dictionary. |
| 313 | */ |
| 314 | void qdict_iter(const QDict *qdict, |
| 315 | void (*iter)(const char *key, QObject *obj, void *opaque), |
| 316 | void *opaque) |
| 317 | { |
| 318 | int i; |
| 319 | QDictEntry *entry; |
| 320 | |
| 321 | for (i = 0; i < QDICT_HASH_SIZE; i++) { |
| 322 | QLIST_FOREACH(entry, &qdict->table[i], next) |
| 323 | iter(entry->key, entry->value, opaque); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 328 | * qentry_destroy(): Free all the memory allocated by a QDictEntry |
| 329 | */ |
| 330 | static void qentry_destroy(QDictEntry *e) |
| 331 | { |
| 332 | assert(e != NULL); |
| 333 | assert(e->key != NULL); |
| 334 | assert(e->value != NULL); |
| 335 | |
| 336 | qobject_decref(e->value); |
| 337 | qemu_free(e->key); |
| 338 | qemu_free(e); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * qdict_del(): Delete a 'key:value' pair from the dictionary |
| 343 | * |
| 344 | * This will destroy all data allocated by this entry. |
| 345 | */ |
| 346 | void qdict_del(QDict *qdict, const char *key) |
| 347 | { |
| 348 | QDictEntry *entry; |
| 349 | |
| 350 | entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_HASH_SIZE); |
| 351 | if (entry) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 352 | QLIST_REMOVE(entry, next); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 353 | qentry_destroy(entry); |
| 354 | qdict->size--; |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * qdict_destroy_obj(): Free all the memory allocated by a QDict |
| 360 | */ |
| 361 | static void qdict_destroy_obj(QObject *obj) |
| 362 | { |
| 363 | int i; |
| 364 | QDict *qdict; |
| 365 | |
| 366 | assert(obj != NULL); |
| 367 | qdict = qobject_to_qdict(obj); |
| 368 | |
| 369 | for (i = 0; i < QDICT_HASH_SIZE; i++) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 370 | QDictEntry *entry = QLIST_FIRST(&qdict->table[i]); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 371 | while (entry) { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 372 | QDictEntry *tmp = QLIST_NEXT(entry, next); |
| 373 | QLIST_REMOVE(entry, next); |
Luiz Capitulino | fb08dde | 2009-08-28 15:27:07 -0300 | [diff] [blame] | 374 | qentry_destroy(entry); |
| 375 | entry = tmp; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | qemu_free(qdict); |
| 380 | } |