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 "sys-queue.h"
18 #include "qemu-common.h"
20 static const QType qdict_type
;
23 * qdict_new(): Create a new QDict
25 * Return strong reference.
27 QDict
*qdict_new(void)
31 qdict
= qemu_mallocz(sizeof(*qdict
));
32 QOBJECT_INIT(qdict
, &qdict_type
);
38 * qobject_to_qdict(): Convert a QObject into a QDict
40 QDict
*qobject_to_qdict(const QObject
*obj
)
42 if (qobject_type(obj
) != QTYPE_QDICT
)
45 return container_of(obj
, QDict
, base
);
49 * tdb_hash(): based on the hash agorithm from gdbm, via tdb
50 * (from module-init-tools)
52 static unsigned int tdb_hash(const char *name
)
54 unsigned value
; /* Used to compute the hash value. */
55 unsigned i
; /* Used to cycle through random values. */
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)));
61 return (1103515243 * value
+ 12345);
65 * alloc_entry(): allocate a new QDictEntry
67 static QDictEntry
*alloc_entry(const char *key
, QObject
*value
)
71 entry
= qemu_mallocz(sizeof(*entry
));
72 entry
->key
= qemu_strdup(key
);
79 * qdict_find(): List lookup function
81 static QDictEntry
*qdict_find(const QDict
*qdict
,
82 const char *key
, unsigned int hash
)
86 LIST_FOREACH(entry
, &qdict
->table
[hash
], next
)
87 if (!strcmp(entry
->key
, key
))
94 * qdict_put_obj(): Put a new QObject into the dictionary
96 * Insert the pair 'key:value' into 'qdict', if 'key' already exists
97 * its 'value' will be replaced.
99 * This is done by freeing the reference to the stored QObject and
100 * storing the new one in the same entry.
102 * NOTE: ownership of 'value' is transferred to the QDict
104 void qdict_put_obj(QDict
*qdict
, const char *key
, QObject
*value
)
109 hash
= tdb_hash(key
) % QDICT_HASH_SIZE
;
110 entry
= qdict_find(qdict
, key
, hash
);
112 /* replace key's value */
113 qobject_decref(entry
->value
);
114 entry
->value
= value
;
116 /* allocate a new entry */
117 entry
= alloc_entry(key
, value
);
118 LIST_INSERT_HEAD(&qdict
->table
[hash
], entry
, next
);
125 * qdict_get(): Lookup for a given 'key'
127 * Return a weak reference to the QObject associated with 'key' if
128 * 'key' is present in the dictionary, NULL otherwise.
130 QObject
*qdict_get(const QDict
*qdict
, const char *key
)
134 entry
= qdict_find(qdict
, key
, tdb_hash(key
) % QDICT_HASH_SIZE
);
135 return (entry
== NULL
? NULL
: entry
->value
);
139 * qdict_haskey(): Check if 'key' exists
141 * Return 1 if 'key' exists in the dict, 0 otherwise
143 int qdict_haskey(const QDict
*qdict
, const char *key
)
145 unsigned int hash
= tdb_hash(key
) % QDICT_HASH_SIZE
;
146 return (qdict_find(qdict
, key
, hash
) == NULL
? 0 : 1);
150 * qdict_size(): Return the size of the dictionary
152 size_t qdict_size(const QDict
*qdict
)
158 * qdict_get_obj(): Get a QObject of a specific type
160 static QObject
*qdict_get_obj(const QDict
*qdict
, const char *key
,
165 obj
= qdict_get(qdict
, key
);
167 assert(qobject_type(obj
) == type
);
173 * qdict_get_int(): Get an integer mapped by 'key'
175 * This function assumes that 'key' exists and it stores a
178 * Return integer mapped by 'key'.
180 int64_t qdict_get_int(const QDict
*qdict
, const char *key
)
182 QObject
*obj
= qdict_get_obj(qdict
, key
, QTYPE_QINT
);
183 return qint_get_int(qobject_to_qint(obj
));
187 * qdict_get_str(): Get a pointer to the stored string mapped
190 * This function assumes that 'key' exists and it stores a
193 * Return pointer to the string mapped by 'key'.
195 const char *qdict_get_str(const QDict
*qdict
, const char *key
)
197 QObject
*obj
= qdict_get_obj(qdict
, key
, QTYPE_QSTRING
);
198 return qstring_get_str(qobject_to_qstring(obj
));
202 * qdict_get_try_int(): Try to get integer mapped by 'key'
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.
208 int64_t qdict_get_try_int(const QDict
*qdict
, const char *key
,
213 obj
= qdict_get(qdict
, key
);
214 if (!obj
|| qobject_type(obj
) != QTYPE_QINT
)
217 return qint_get_int(qobject_to_qint(obj
));
221 * qdict_get_try_str(): Try to get a pointer to the stored string
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.
228 const char *qdict_get_try_str(const QDict
*qdict
, const char *key
)
232 obj
= qdict_get(qdict
, key
);
233 if (!obj
|| qobject_type(obj
) != QTYPE_QSTRING
)
236 return qstring_get_str(qobject_to_qstring(obj
));
240 * qentry_destroy(): Free all the memory allocated by a QDictEntry
242 static void qentry_destroy(QDictEntry
*e
)
245 assert(e
->key
!= NULL
);
246 assert(e
->value
!= NULL
);
248 qobject_decref(e
->value
);
254 * qdict_del(): Delete a 'key:value' pair from the dictionary
256 * This will destroy all data allocated by this entry.
258 void qdict_del(QDict
*qdict
, const char *key
)
262 entry
= qdict_find(qdict
, key
, tdb_hash(key
) % QDICT_HASH_SIZE
);
264 LIST_REMOVE(entry
, next
);
265 qentry_destroy(entry
);
271 * qdict_destroy_obj(): Free all the memory allocated by a QDict
273 static void qdict_destroy_obj(QObject
*obj
)
279 qdict
= qobject_to_qdict(obj
);
281 for (i
= 0; i
< QDICT_HASH_SIZE
; i
++) {
282 QDictEntry
*entry
= LIST_FIRST(&qdict
->table
[i
]);
284 QDictEntry
*tmp
= LIST_NEXT(entry
, next
);
285 LIST_REMOVE(entry
, next
);
286 qentry_destroy(entry
);
294 static const QType qdict_type
= {
296 .destroy
= qdict_destroy_obj
,