4 * Copyright (C) 2009 Red Hat Inc.
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
17 #include "qemu-queue.h"
18 #include "qemu-common.h"
20 static void qdict_destroy_obj(QObject
*obj
);
22 static const QType qdict_type
= {
24 .destroy
= qdict_destroy_obj
,
28 * qdict_new(): Create a new QDict
30 * Return strong reference.
32 QDict
*qdict_new(void)
36 qdict
= qemu_mallocz(sizeof(*qdict
));
37 QOBJECT_INIT(qdict
, &qdict_type
);
43 * qobject_to_qdict(): Convert a QObject into a QDict
45 QDict
*qobject_to_qdict(const QObject
*obj
)
47 if (qobject_type(obj
) != QTYPE_QDICT
)
50 return container_of(obj
, QDict
, base
);
54 * tdb_hash(): based on the hash agorithm from gdbm, via tdb
55 * (from module-init-tools)
57 static unsigned int tdb_hash(const char *name
)
59 unsigned value
; /* Used to compute the hash value. */
60 unsigned i
; /* Used to cycle through random values. */
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)));
66 return (1103515243 * value
+ 12345);
70 * alloc_entry(): allocate a new QDictEntry
72 static QDictEntry
*alloc_entry(const char *key
, QObject
*value
)
76 entry
= qemu_mallocz(sizeof(*entry
));
77 entry
->key
= qemu_strdup(key
);
84 * qdict_find(): List lookup function
86 static QDictEntry
*qdict_find(const QDict
*qdict
,
87 const char *key
, unsigned int hash
)
91 QLIST_FOREACH(entry
, &qdict
->table
[hash
], next
)
92 if (!strcmp(entry
->key
, key
))
99 * qdict_put_obj(): Put a new QObject into the dictionary
101 * Insert the pair 'key:value' into 'qdict', if 'key' already exists
102 * its 'value' will be replaced.
104 * This is done by freeing the reference to the stored QObject and
105 * storing the new one in the same entry.
107 * NOTE: ownership of 'value' is transferred to the QDict
109 void qdict_put_obj(QDict
*qdict
, const char *key
, QObject
*value
)
114 hash
= tdb_hash(key
) % QDICT_HASH_SIZE
;
115 entry
= qdict_find(qdict
, key
, hash
);
117 /* replace key's value */
118 qobject_decref(entry
->value
);
119 entry
->value
= value
;
121 /* allocate a new entry */
122 entry
= alloc_entry(key
, value
);
123 QLIST_INSERT_HEAD(&qdict
->table
[hash
], entry
, next
);
130 * qdict_get(): Lookup for a given 'key'
132 * Return a weak reference to the QObject associated with 'key' if
133 * 'key' is present in the dictionary, NULL otherwise.
135 QObject
*qdict_get(const QDict
*qdict
, const char *key
)
139 entry
= qdict_find(qdict
, key
, tdb_hash(key
) % QDICT_HASH_SIZE
);
140 return (entry
== NULL
? NULL
: entry
->value
);
144 * qdict_haskey(): Check if 'key' exists
146 * Return 1 if 'key' exists in the dict, 0 otherwise
148 int qdict_haskey(const QDict
*qdict
, const char *key
)
150 unsigned int hash
= tdb_hash(key
) % QDICT_HASH_SIZE
;
151 return (qdict_find(qdict
, key
, hash
) == NULL
? 0 : 1);
155 * qdict_size(): Return the size of the dictionary
157 size_t qdict_size(const QDict
*qdict
)
163 * qdict_get_obj(): Get a QObject of a specific type
165 static QObject
*qdict_get_obj(const QDict
*qdict
, const char *key
,
170 obj
= qdict_get(qdict
, key
);
172 assert(qobject_type(obj
) == type
);
178 * qdict_get_int(): Get an integer mapped by 'key'
180 * This function assumes that 'key' exists and it stores a
183 * Return integer mapped by 'key'.
185 int64_t qdict_get_int(const QDict
*qdict
, const char *key
)
187 QObject
*obj
= qdict_get_obj(qdict
, key
, QTYPE_QINT
);
188 return qint_get_int(qobject_to_qint(obj
));
192 * qdict_get_str(): Get a pointer to the stored string mapped
195 * This function assumes that 'key' exists and it stores a
198 * Return pointer to the string mapped by 'key'.
200 const char *qdict_get_str(const QDict
*qdict
, const char *key
)
202 QObject
*obj
= qdict_get_obj(qdict
, key
, QTYPE_QSTRING
);
203 return qstring_get_str(qobject_to_qstring(obj
));
207 * qdict_get_try_int(): Try to get integer mapped by 'key'
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.
213 int64_t qdict_get_try_int(const QDict
*qdict
, const char *key
,
218 obj
= qdict_get(qdict
, key
);
219 if (!obj
|| qobject_type(obj
) != QTYPE_QINT
)
222 return qint_get_int(qobject_to_qint(obj
));
226 * qdict_get_try_str(): Try to get a pointer to the stored string
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.
233 const char *qdict_get_try_str(const QDict
*qdict
, const char *key
)
237 obj
= qdict_get(qdict
, key
);
238 if (!obj
|| qobject_type(obj
) != QTYPE_QSTRING
)
241 return qstring_get_str(qobject_to_qstring(obj
));
245 * qentry_destroy(): Free all the memory allocated by a QDictEntry
247 static void qentry_destroy(QDictEntry
*e
)
250 assert(e
->key
!= NULL
);
251 assert(e
->value
!= NULL
);
253 qobject_decref(e
->value
);
259 * qdict_del(): Delete a 'key:value' pair from the dictionary
261 * This will destroy all data allocated by this entry.
263 void qdict_del(QDict
*qdict
, const char *key
)
267 entry
= qdict_find(qdict
, key
, tdb_hash(key
) % QDICT_HASH_SIZE
);
269 QLIST_REMOVE(entry
, next
);
270 qentry_destroy(entry
);
276 * qdict_destroy_obj(): Free all the memory allocated by a QDict
278 static void qdict_destroy_obj(QObject
*obj
)
284 qdict
= qobject_to_qdict(obj
);
286 for (i
= 0; i
< QDICT_HASH_SIZE
; i
++) {
287 QDictEntry
*entry
= QLIST_FIRST(&qdict
->table
[i
]);
289 QDictEntry
*tmp
= QLIST_NEXT(entry
, next
);
290 QLIST_REMOVE(entry
, next
);
291 qentry_destroy(entry
);