xref: /openbmc/qemu/qobject/qdict.c (revision b917da4c)
1 /*
2  * QDict Module
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 LGPL, version 2.1 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/qmp/qint.h"
15 #include "qapi/qmp/qfloat.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qbool.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qapi/qmp/qobject.h"
20 #include "qemu/queue.h"
21 #include "qemu-common.h"
22 
23 /**
24  * qdict_new(): Create a new QDict
25  *
26  * Return strong reference.
27  */
28 QDict *qdict_new(void)
29 {
30     QDict *qdict;
31 
32     qdict = g_malloc0(sizeof(*qdict));
33     qobject_init(QOBJECT(qdict), QTYPE_QDICT);
34 
35     return qdict;
36 }
37 
38 /**
39  * qobject_to_qdict(): Convert a QObject into a QDict
40  */
41 QDict *qobject_to_qdict(const QObject *obj)
42 {
43     if (!obj || qobject_type(obj) != QTYPE_QDICT) {
44         return NULL;
45     }
46     return container_of(obj, QDict, base);
47 }
48 
49 /**
50  * tdb_hash(): based on the hash agorithm from gdbm, via tdb
51  * (from module-init-tools)
52  */
53 static unsigned int tdb_hash(const char *name)
54 {
55     unsigned value;	/* Used to compute the hash value.  */
56     unsigned   i;	/* Used to cycle through random values. */
57 
58     /* Set the initial value from the key size. */
59     for (value = 0x238F13AF * strlen(name), i=0; name[i]; i++)
60         value = (value + (((const unsigned char *)name)[i] << (i*5 % 24)));
61 
62     return (1103515243 * value + 12345);
63 }
64 
65 /**
66  * alloc_entry(): allocate a new QDictEntry
67  */
68 static QDictEntry *alloc_entry(const char *key, QObject *value)
69 {
70     QDictEntry *entry;
71 
72     entry = g_malloc0(sizeof(*entry));
73     entry->key = g_strdup(key);
74     entry->value = value;
75 
76     return entry;
77 }
78 
79 /**
80  * qdict_entry_value(): Return qdict entry value
81  *
82  * Return weak reference.
83  */
84 QObject *qdict_entry_value(const QDictEntry *entry)
85 {
86     return entry->value;
87 }
88 
89 /**
90  * qdict_entry_key(): Return qdict entry key
91  *
92  * Return a *pointer* to the string, it has to be duplicated before being
93  * stored.
94  */
95 const char *qdict_entry_key(const QDictEntry *entry)
96 {
97     return entry->key;
98 }
99 
100 /**
101  * qdict_find(): List lookup function
102  */
103 static QDictEntry *qdict_find(const QDict *qdict,
104                               const char *key, unsigned int bucket)
105 {
106     QDictEntry *entry;
107 
108     QLIST_FOREACH(entry, &qdict->table[bucket], next)
109         if (!strcmp(entry->key, key))
110             return entry;
111 
112     return NULL;
113 }
114 
115 /**
116  * qdict_put_obj(): Put a new QObject into the dictionary
117  *
118  * Insert the pair 'key:value' into 'qdict', if 'key' already exists
119  * its 'value' will be replaced.
120  *
121  * This is done by freeing the reference to the stored QObject and
122  * storing the new one in the same entry.
123  *
124  * NOTE: ownership of 'value' is transferred to the QDict
125  */
126 void qdict_put_obj(QDict *qdict, const char *key, QObject *value)
127 {
128     unsigned int bucket;
129     QDictEntry *entry;
130 
131     bucket = tdb_hash(key) % QDICT_BUCKET_MAX;
132     entry = qdict_find(qdict, key, bucket);
133     if (entry) {
134         /* replace key's value */
135         qobject_decref(entry->value);
136         entry->value = value;
137     } else {
138         /* allocate a new entry */
139         entry = alloc_entry(key, value);
140         QLIST_INSERT_HEAD(&qdict->table[bucket], entry, next);
141         qdict->size++;
142     }
143 }
144 
145 /**
146  * qdict_get(): Lookup for a given 'key'
147  *
148  * Return a weak reference to the QObject associated with 'key' if
149  * 'key' is present in the dictionary, NULL otherwise.
150  */
151 QObject *qdict_get(const QDict *qdict, const char *key)
152 {
153     QDictEntry *entry;
154 
155     entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX);
156     return (entry == NULL ? NULL : entry->value);
157 }
158 
159 /**
160  * qdict_haskey(): Check if 'key' exists
161  *
162  * Return 1 if 'key' exists in the dict, 0 otherwise
163  */
164 int qdict_haskey(const QDict *qdict, const char *key)
165 {
166     unsigned int bucket = tdb_hash(key) % QDICT_BUCKET_MAX;
167     return (qdict_find(qdict, key, bucket) == NULL ? 0 : 1);
168 }
169 
170 /**
171  * qdict_size(): Return the size of the dictionary
172  */
173 size_t qdict_size(const QDict *qdict)
174 {
175     return qdict->size;
176 }
177 
178 /**
179  * qdict_get_obj(): Get a QObject of a specific type
180  */
181 static QObject *qdict_get_obj(const QDict *qdict, const char *key, QType type)
182 {
183     QObject *obj;
184 
185     obj = qdict_get(qdict, key);
186     assert(obj != NULL);
187     assert(qobject_type(obj) == type);
188 
189     return obj;
190 }
191 
192 /**
193  * qdict_get_double(): Get an number mapped by 'key'
194  *
195  * This function assumes that 'key' exists and it stores a
196  * QFloat or QInt object.
197  *
198  * Return number mapped by 'key'.
199  */
200 double qdict_get_double(const QDict *qdict, const char *key)
201 {
202     QObject *obj = qdict_get(qdict, key);
203 
204     assert(obj);
205     switch (qobject_type(obj)) {
206     case QTYPE_QFLOAT:
207         return qfloat_get_double(qobject_to_qfloat(obj));
208     case QTYPE_QINT:
209         return qint_get_int(qobject_to_qint(obj));
210     default:
211         abort();
212     }
213 }
214 
215 /**
216  * qdict_get_int(): Get an integer mapped by 'key'
217  *
218  * This function assumes that 'key' exists and it stores a
219  * QInt object.
220  *
221  * Return integer mapped by 'key'.
222  */
223 int64_t qdict_get_int(const QDict *qdict, const char *key)
224 {
225     return qint_get_int(qobject_to_qint(qdict_get(qdict, key)));
226 }
227 
228 /**
229  * qdict_get_bool(): Get a bool mapped by 'key'
230  *
231  * This function assumes that 'key' exists and it stores a
232  * QBool object.
233  *
234  * Return bool mapped by 'key'.
235  */
236 bool qdict_get_bool(const QDict *qdict, const char *key)
237 {
238     return qbool_get_bool(qobject_to_qbool(qdict_get(qdict, key)));
239 }
240 
241 /**
242  * qdict_get_qlist(): Get the QList mapped by 'key'
243  *
244  * This function assumes that 'key' exists and it stores a
245  * QList object.
246  *
247  * Return QList mapped by 'key'.
248  */
249 QList *qdict_get_qlist(const QDict *qdict, const char *key)
250 {
251     return qobject_to_qlist(qdict_get_obj(qdict, key, QTYPE_QLIST));
252 }
253 
254 /**
255  * qdict_get_qdict(): Get the QDict mapped by 'key'
256  *
257  * This function assumes that 'key' exists and it stores a
258  * QDict object.
259  *
260  * Return QDict mapped by 'key'.
261  */
262 QDict *qdict_get_qdict(const QDict *qdict, const char *key)
263 {
264     return qobject_to_qdict(qdict_get(qdict, key));
265 }
266 
267 /**
268  * qdict_get_str(): Get a pointer to the stored string mapped
269  * by 'key'
270  *
271  * This function assumes that 'key' exists and it stores a
272  * QString object.
273  *
274  * Return pointer to the string mapped by 'key'.
275  */
276 const char *qdict_get_str(const QDict *qdict, const char *key)
277 {
278     return qstring_get_str(qobject_to_qstring(qdict_get(qdict, key)));
279 }
280 
281 /**
282  * qdict_get_try_int(): Try to get integer mapped by 'key'
283  *
284  * Return integer mapped by 'key', if it is not present in
285  * the dictionary or if the stored object is not of QInt type
286  * 'def_value' will be returned.
287  */
288 int64_t qdict_get_try_int(const QDict *qdict, const char *key,
289                           int64_t def_value)
290 {
291     QInt *qint = qobject_to_qint(qdict_get(qdict, key));
292 
293     return qint ? qint_get_int(qint) : def_value;
294 }
295 
296 /**
297  * qdict_get_try_bool(): Try to get a bool mapped by 'key'
298  *
299  * Return bool mapped by 'key', if it is not present in the
300  * dictionary or if the stored object is not of QBool type
301  * 'def_value' will be returned.
302  */
303 bool qdict_get_try_bool(const QDict *qdict, const char *key, bool def_value)
304 {
305     QBool *qbool = qobject_to_qbool(qdict_get(qdict, key));
306 
307     return qbool ? qbool_get_bool(qbool) : def_value;
308 }
309 
310 /**
311  * qdict_get_try_str(): Try to get a pointer to the stored string
312  * mapped by 'key'
313  *
314  * Return a pointer to the string mapped by 'key', if it is not present
315  * in the dictionary or if the stored object is not of QString type
316  * NULL will be returned.
317  */
318 const char *qdict_get_try_str(const QDict *qdict, const char *key)
319 {
320     QString *qstr = qobject_to_qstring(qdict_get(qdict, key));
321 
322     return qstr ? qstring_get_str(qstr) : NULL;
323 }
324 
325 /**
326  * qdict_iter(): Iterate over all the dictionary's stored values.
327  *
328  * This function allows the user to provide an iterator, which will be
329  * called for each stored value in the dictionary.
330  */
331 void qdict_iter(const QDict *qdict,
332                 void (*iter)(const char *key, QObject *obj, void *opaque),
333                 void *opaque)
334 {
335     int i;
336     QDictEntry *entry;
337 
338     for (i = 0; i < QDICT_BUCKET_MAX; i++) {
339         QLIST_FOREACH(entry, &qdict->table[i], next)
340             iter(entry->key, entry->value, opaque);
341     }
342 }
343 
344 static QDictEntry *qdict_next_entry(const QDict *qdict, int first_bucket)
345 {
346     int i;
347 
348     for (i = first_bucket; i < QDICT_BUCKET_MAX; i++) {
349         if (!QLIST_EMPTY(&qdict->table[i])) {
350             return QLIST_FIRST(&qdict->table[i]);
351         }
352     }
353 
354     return NULL;
355 }
356 
357 /**
358  * qdict_first(): Return first qdict entry for iteration.
359  */
360 const QDictEntry *qdict_first(const QDict *qdict)
361 {
362     return qdict_next_entry(qdict, 0);
363 }
364 
365 /**
366  * qdict_next(): Return next qdict entry in an iteration.
367  */
368 const QDictEntry *qdict_next(const QDict *qdict, const QDictEntry *entry)
369 {
370     QDictEntry *ret;
371 
372     ret = QLIST_NEXT(entry, next);
373     if (!ret) {
374         unsigned int bucket = tdb_hash(entry->key) % QDICT_BUCKET_MAX;
375         ret = qdict_next_entry(qdict, bucket + 1);
376     }
377 
378     return ret;
379 }
380 
381 /**
382  * qdict_clone_shallow(): Clones a given QDict. Its entries are not copied, but
383  * another reference is added.
384  */
385 QDict *qdict_clone_shallow(const QDict *src)
386 {
387     QDict *dest;
388     QDictEntry *entry;
389     int i;
390 
391     dest = qdict_new();
392 
393     for (i = 0; i < QDICT_BUCKET_MAX; i++) {
394         QLIST_FOREACH(entry, &src->table[i], next) {
395             qobject_incref(entry->value);
396             qdict_put_obj(dest, entry->key, entry->value);
397         }
398     }
399 
400     return dest;
401 }
402 
403 /**
404  * qentry_destroy(): Free all the memory allocated by a QDictEntry
405  */
406 static void qentry_destroy(QDictEntry *e)
407 {
408     assert(e != NULL);
409     assert(e->key != NULL);
410     assert(e->value != NULL);
411 
412     qobject_decref(e->value);
413     g_free(e->key);
414     g_free(e);
415 }
416 
417 /**
418  * qdict_del(): Delete a 'key:value' pair from the dictionary
419  *
420  * This will destroy all data allocated by this entry.
421  */
422 void qdict_del(QDict *qdict, const char *key)
423 {
424     QDictEntry *entry;
425 
426     entry = qdict_find(qdict, key, tdb_hash(key) % QDICT_BUCKET_MAX);
427     if (entry) {
428         QLIST_REMOVE(entry, next);
429         qentry_destroy(entry);
430         qdict->size--;
431     }
432 }
433 
434 /**
435  * qdict_destroy_obj(): Free all the memory allocated by a QDict
436  */
437 void qdict_destroy_obj(QObject *obj)
438 {
439     int i;
440     QDict *qdict;
441 
442     assert(obj != NULL);
443     qdict = qobject_to_qdict(obj);
444 
445     for (i = 0; i < QDICT_BUCKET_MAX; i++) {
446         QDictEntry *entry = QLIST_FIRST(&qdict->table[i]);
447         while (entry) {
448             QDictEntry *tmp = QLIST_NEXT(entry, next);
449             QLIST_REMOVE(entry, next);
450             qentry_destroy(entry);
451             entry = tmp;
452         }
453     }
454 
455     g_free(qdict);
456 }
457 
458 /**
459  * qdict_copy_default(): If no entry mapped by 'key' exists in 'dst' yet, the
460  * value of 'key' in 'src' is copied there (and the refcount increased
461  * accordingly).
462  */
463 void qdict_copy_default(QDict *dst, QDict *src, const char *key)
464 {
465     QObject *val;
466 
467     if (qdict_haskey(dst, key)) {
468         return;
469     }
470 
471     val = qdict_get(src, key);
472     if (val) {
473         qobject_incref(val);
474         qdict_put_obj(dst, key, val);
475     }
476 }
477 
478 /**
479  * qdict_set_default_str(): If no entry mapped by 'key' exists in 'dst' yet, a
480  * new QString initialised by 'val' is put there.
481  */
482 void qdict_set_default_str(QDict *dst, const char *key, const char *val)
483 {
484     if (qdict_haskey(dst, key)) {
485         return;
486     }
487 
488     qdict_put(dst, key, qstring_from_str(val));
489 }
490 
491 static void qdict_flatten_qdict(QDict *qdict, QDict *target,
492                                 const char *prefix);
493 
494 static void qdict_flatten_qlist(QList *qlist, QDict *target, const char *prefix)
495 {
496     QObject *value;
497     const QListEntry *entry;
498     char *new_key;
499     int i;
500 
501     /* This function is never called with prefix == NULL, i.e., it is always
502      * called from within qdict_flatten_q(list|dict)(). Therefore, it does not
503      * need to remove list entries during the iteration (the whole list will be
504      * deleted eventually anyway from qdict_flatten_qdict()). */
505     assert(prefix);
506 
507     entry = qlist_first(qlist);
508 
509     for (i = 0; entry; entry = qlist_next(entry), i++) {
510         value = qlist_entry_obj(entry);
511         new_key = g_strdup_printf("%s.%i", prefix, i);
512 
513         if (qobject_type(value) == QTYPE_QDICT) {
514             qdict_flatten_qdict(qobject_to_qdict(value), target, new_key);
515         } else if (qobject_type(value) == QTYPE_QLIST) {
516             qdict_flatten_qlist(qobject_to_qlist(value), target, new_key);
517         } else {
518             /* All other types are moved to the target unchanged. */
519             qobject_incref(value);
520             qdict_put_obj(target, new_key, value);
521         }
522 
523         g_free(new_key);
524     }
525 }
526 
527 static void qdict_flatten_qdict(QDict *qdict, QDict *target, const char *prefix)
528 {
529     QObject *value;
530     const QDictEntry *entry, *next;
531     char *new_key;
532     bool delete;
533 
534     entry = qdict_first(qdict);
535 
536     while (entry != NULL) {
537 
538         next = qdict_next(qdict, entry);
539         value = qdict_entry_value(entry);
540         new_key = NULL;
541         delete = false;
542 
543         if (prefix) {
544             new_key = g_strdup_printf("%s.%s", prefix, entry->key);
545         }
546 
547         if (qobject_type(value) == QTYPE_QDICT) {
548             /* Entries of QDicts are processed recursively, the QDict object
549              * itself disappears. */
550             qdict_flatten_qdict(qobject_to_qdict(value), target,
551                                 new_key ? new_key : entry->key);
552             delete = true;
553         } else if (qobject_type(value) == QTYPE_QLIST) {
554             qdict_flatten_qlist(qobject_to_qlist(value), target,
555                                 new_key ? new_key : entry->key);
556             delete = true;
557         } else if (prefix) {
558             /* All other objects are moved to the target unchanged. */
559             qobject_incref(value);
560             qdict_put_obj(target, new_key, value);
561             delete = true;
562         }
563 
564         g_free(new_key);
565 
566         if (delete) {
567             qdict_del(qdict, entry->key);
568 
569             /* Restart loop after modifying the iterated QDict */
570             entry = qdict_first(qdict);
571             continue;
572         }
573 
574         entry = next;
575     }
576 }
577 
578 /**
579  * qdict_flatten(): For each nested QDict with key x, all fields with key y
580  * are moved to this QDict and their key is renamed to "x.y". For each nested
581  * QList with key x, the field at index y is moved to this QDict with the key
582  * "x.y" (i.e., the reverse of what qdict_array_split() does).
583  * This operation is applied recursively for nested QDicts and QLists.
584  */
585 void qdict_flatten(QDict *qdict)
586 {
587     qdict_flatten_qdict(qdict, qdict, NULL);
588 }
589 
590 /* extract all the src QDict entries starting by start into dst */
591 void qdict_extract_subqdict(QDict *src, QDict **dst, const char *start)
592 
593 {
594     const QDictEntry *entry, *next;
595     const char *p;
596 
597     *dst = qdict_new();
598     entry = qdict_first(src);
599 
600     while (entry != NULL) {
601         next = qdict_next(src, entry);
602         if (strstart(entry->key, start, &p)) {
603             qobject_incref(entry->value);
604             qdict_put_obj(*dst, p, entry->value);
605             qdict_del(src, entry->key);
606         }
607         entry = next;
608     }
609 }
610 
611 static int qdict_count_prefixed_entries(const QDict *src, const char *start)
612 {
613     const QDictEntry *entry;
614     int count = 0;
615 
616     for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
617         if (strstart(entry->key, start, NULL)) {
618             if (count == INT_MAX) {
619                 return -ERANGE;
620             }
621             count++;
622         }
623     }
624 
625     return count;
626 }
627 
628 /**
629  * qdict_array_split(): This function moves array-like elements of a QDict into
630  * a new QList. Every entry in the original QDict with a key "%u" or one
631  * prefixed "%u.", where %u designates an unsigned integer starting at 0 and
632  * incrementally counting up, will be moved to a new QDict at index %u in the
633  * output QList with the key prefix removed, if that prefix is "%u.". If the
634  * whole key is just "%u", the whole QObject will be moved unchanged without
635  * creating a new QDict. The function terminates when there is no entry in the
636  * QDict with a prefix directly (incrementally) following the last one; it also
637  * returns if there are both entries with "%u" and "%u." for the same index %u.
638  * Example: {"0.a": 42, "0.b": 23, "1.x": 0, "4.y": 1, "o.o": 7, "2": 66}
639  *      (or {"1.x": 0, "4.y": 1, "0.a": 42, "o.o": 7, "0.b": 23, "2": 66})
640  *       => [{"a": 42, "b": 23}, {"x": 0}, 66]
641  *      and {"4.y": 1, "o.o": 7} (remainder of the old QDict)
642  */
643 void qdict_array_split(QDict *src, QList **dst)
644 {
645     unsigned i;
646 
647     *dst = qlist_new();
648 
649     for (i = 0; i < UINT_MAX; i++) {
650         QObject *subqobj;
651         bool is_subqdict;
652         QDict *subqdict;
653         char indexstr[32], prefix[32];
654         size_t snprintf_ret;
655 
656         snprintf_ret = snprintf(indexstr, 32, "%u", i);
657         assert(snprintf_ret < 32);
658 
659         subqobj = qdict_get(src, indexstr);
660 
661         snprintf_ret = snprintf(prefix, 32, "%u.", i);
662         assert(snprintf_ret < 32);
663 
664         /* Overflow is the same as positive non-zero results */
665         is_subqdict = qdict_count_prefixed_entries(src, prefix);
666 
667         // There may be either a single subordinate object (named "%u") or
668         // multiple objects (each with a key prefixed "%u."), but not both.
669         if (!subqobj == !is_subqdict) {
670             break;
671         }
672 
673         if (is_subqdict) {
674             qdict_extract_subqdict(src, &subqdict, prefix);
675             assert(qdict_size(subqdict) > 0);
676         } else {
677             qobject_incref(subqobj);
678             qdict_del(src, indexstr);
679         }
680 
681         qlist_append_obj(*dst, subqobj ?: QOBJECT(subqdict));
682     }
683 }
684 
685 /**
686  * qdict_array_entries(): Returns the number of direct array entries if the
687  * sub-QDict of src specified by the prefix in subqdict (or src itself for
688  * prefix == "") is valid as an array, i.e. the length of the created list if
689  * the sub-QDict would become empty after calling qdict_array_split() on it. If
690  * the array is not valid, -EINVAL is returned.
691  */
692 int qdict_array_entries(QDict *src, const char *subqdict)
693 {
694     const QDictEntry *entry;
695     unsigned i;
696     unsigned entries = 0;
697     size_t subqdict_len = strlen(subqdict);
698 
699     assert(!subqdict_len || subqdict[subqdict_len - 1] == '.');
700 
701     /* qdict_array_split() loops until UINT_MAX, but as we want to return
702      * negative errors, we only have a signed return value here. Any additional
703      * entries will lead to -EINVAL. */
704     for (i = 0; i < INT_MAX; i++) {
705         QObject *subqobj;
706         int subqdict_entries;
707         size_t slen = 32 + subqdict_len;
708         char indexstr[slen], prefix[slen];
709         size_t snprintf_ret;
710 
711         snprintf_ret = snprintf(indexstr, slen, "%s%u", subqdict, i);
712         assert(snprintf_ret < slen);
713 
714         subqobj = qdict_get(src, indexstr);
715 
716         snprintf_ret = snprintf(prefix, slen, "%s%u.", subqdict, i);
717         assert(snprintf_ret < slen);
718 
719         subqdict_entries = qdict_count_prefixed_entries(src, prefix);
720         if (subqdict_entries < 0) {
721             return subqdict_entries;
722         }
723 
724         /* There may be either a single subordinate object (named "%u") or
725          * multiple objects (each with a key prefixed "%u."), but not both. */
726         if (subqobj && subqdict_entries) {
727             return -EINVAL;
728         } else if (!subqobj && !subqdict_entries) {
729             break;
730         }
731 
732         entries += subqdict_entries ? subqdict_entries : 1;
733     }
734 
735     /* Consider everything handled that isn't part of the given sub-QDict */
736     for (entry = qdict_first(src); entry; entry = qdict_next(src, entry)) {
737         if (!strstart(qdict_entry_key(entry), subqdict, NULL)) {
738             entries++;
739         }
740     }
741 
742     /* Anything left in the sub-QDict that wasn't handled? */
743     if (qdict_size(src) != entries) {
744         return -EINVAL;
745     }
746 
747     return i;
748 }
749 
750 /**
751  * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all
752  * elements from src to dest.
753  *
754  * If an element from src has a key already present in dest, it will not be
755  * moved unless overwrite is true.
756  *
757  * If overwrite is true, the conflicting values in dest will be discarded and
758  * replaced by the corresponding values from src.
759  *
760  * Therefore, with overwrite being true, the src QDict will always be empty when
761  * this function returns. If overwrite is false, the src QDict will be empty
762  * iff there were no conflicts.
763  */
764 void qdict_join(QDict *dest, QDict *src, bool overwrite)
765 {
766     const QDictEntry *entry, *next;
767 
768     entry = qdict_first(src);
769     while (entry) {
770         next = qdict_next(src, entry);
771 
772         if (overwrite || !qdict_haskey(dest, entry->key)) {
773             qobject_incref(entry->value);
774             qdict_put_obj(dest, entry->key, entry->value);
775             qdict_del(src, entry->key);
776         }
777 
778         entry = next;
779     }
780 }
781