05bba0
From 08ab287975f409e420863a8d39e3b3add8ca6088 Mon Sep 17 00:00:00 2001
05bba0
From: Richard Jones <rjones@redhat.com>
05bba0
Date: Tue, 2 Jun 2015 09:46:36 +0200
05bba0
Subject: [PATCH 2/4] qdict: Add qdict_join()
05bba0
05bba0
Message-id: <1433238397-2500-2-git-send-email-rjones@redhat.com>
05bba0
Patchwork-id: 65278
05bba0
O-Subject: [RHEL-7.2 qemu-kvm PATCH 1/2] qdict: Add qdict_join()
05bba0
Bugzilla: 1226697
05bba0
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
05bba0
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
05bba0
RH-Acked-by: Max Reitz <mreitz@redhat.com>
05bba0
05bba0
From: Max Reitz <mreitz@redhat.com>
05bba0
05bba0
This function joins two QDicts by absorbing one into the other.
05bba0
05bba0
Signed-off-by: Max Reitz <mreitz@redhat.com>
05bba0
Reviewed-by: Benoit Canet <benoit@irqsave.net>
05bba0
Reviewed-by: Eric Blake <eblake@redhat.com>
05bba0
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
05bba0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
05bba0
---
05bba0
 include/qapi/qmp/qdict.h |  3 +++
05bba0
 qobject/qdict.c          | 32 ++++++++++++++++++++++++++++++++
05bba0
 2 files changed, 35 insertions(+)
05bba0
05bba0
diff --git a/include/qapi/qmp/qdict.h b/include/qapi/qmp/qdict.h
05bba0
index d6855d1..fc6e2f2 100644
05bba0
--- a/include/qapi/qmp/qdict.h
05bba0
+++ b/include/qapi/qmp/qdict.h
05bba0
@@ -16,6 +16,7 @@
05bba0
 #include "qapi/qmp/qobject.h"
05bba0
 #include "qapi/qmp/qlist.h"
05bba0
 #include "qemu/queue.h"
05bba0
+#include <stdbool.h>
05bba0
 #include <stdint.h>
05bba0
 
05bba0
 #define QDICT_BUCKET_MAX 512
05bba0
@@ -67,4 +68,6 @@ const char *qdict_get_try_str(const QDict *qdict, const char *key);
05bba0
 QDict *qdict_clone_shallow(const QDict *src);
05bba0
 void qdict_flatten(QDict *qdict);
05bba0
 
05bba0
+void qdict_join(QDict *dest, QDict *src, bool overwrite);
05bba0
+
05bba0
 #endif /* QDICT_H */
05bba0
diff --git a/qobject/qdict.c b/qobject/qdict.c
05bba0
index 472f106..c7e35ae 100644
05bba0
--- a/qobject/qdict.c
05bba0
+++ b/qobject/qdict.c
05bba0
@@ -527,3 +527,35 @@ void qdict_flatten(QDict *qdict)
05bba0
 {
05bba0
     qdict_do_flatten(qdict, qdict, NULL);
05bba0
 }
05bba0
+
05bba0
+/**
05bba0
+ * qdict_join(): Absorb the src QDict into the dest QDict, that is, move all
05bba0
+ * elements from src to dest.
05bba0
+ *
05bba0
+ * If an element from src has a key already present in dest, it will not be
05bba0
+ * moved unless overwrite is true.
05bba0
+ *
05bba0
+ * If overwrite is true, the conflicting values in dest will be discarded and
05bba0
+ * replaced by the corresponding values from src.
05bba0
+ *
05bba0
+ * Therefore, with overwrite being true, the src QDict will always be empty when
05bba0
+ * this function returns. If overwrite is false, the src QDict will be empty
05bba0
+ * iff there were no conflicts.
05bba0
+ */
05bba0
+void qdict_join(QDict *dest, QDict *src, bool overwrite)
05bba0
+{
05bba0
+    const QDictEntry *entry, *next;
05bba0
+
05bba0
+    entry = qdict_first(src);
05bba0
+    while (entry) {
05bba0
+        next = qdict_next(src, entry);
05bba0
+
05bba0
+        if (overwrite || !qdict_haskey(dest, entry->key)) {
05bba0
+            qobject_incref(entry->value);
05bba0
+            qdict_put_obj(dest, entry->key, entry->value);
05bba0
+            qdict_del(src, entry->key);
05bba0
+        }
05bba0
+
05bba0
+        entry = next;
05bba0
+    }
05bba0
+}
05bba0
-- 
05bba0
1.8.3.1
05bba0