From: Luiz Capitulino <lcapitulino@redhat.com>
To: qemu-devel@nongnu.org
Cc: aliguori@us.ibm.com, quintela@redhat.com
Subject: [Qemu-devel] [PATCH 3/6] QDict: Introduce qdict_copy()
Date: Fri, 10 Feb 2012 17:31:03 -0200 [thread overview]
Message-ID: <1328902266-25308-4-git-send-email-lcapitulino@redhat.com> (raw)
In-Reply-To: <1328902266-25308-1-git-send-email-lcapitulino@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
check-qdict.c | 29 +++++++++++++++++++++++++++++
qdict.c | 18 ++++++++++++++++++
qdict.h | 1 +
3 files changed, 48 insertions(+), 0 deletions(-)
diff --git a/check-qdict.c b/check-qdict.c
index fc0d276..12c73ab 100644
--- a/check-qdict.c
+++ b/check-qdict.c
@@ -227,6 +227,34 @@ static void qdict_iterapi_test(void)
QDECREF(tests_dict);
}
+static void qdict_copy_test(void)
+{
+ QDict *tests_dict = qdict_new();
+ const QDictEntry *ent;
+ QDict *new;
+ int i;
+
+ for (i = 0; i < 1024; i++) {
+ char key[32];
+
+ snprintf(key, sizeof(key), "key%d", i);
+ qdict_put(tests_dict, key, qint_from_int(i));
+ }
+
+ new = qdict_copy(tests_dict);
+
+ g_assert(qdict_size(new) == qdict_size(tests_dict));
+ for (ent = qdict_first(tests_dict); ent; ent = qdict_next(tests_dict, ent)){
+ const char *key = qdict_entry_key(ent);
+
+ g_assert(qdict_haskey(new, key) == 1);
+ g_assert(qdict_get_int(new, key) == qdict_get_int(tests_dict, key));
+ }
+
+ QDECREF(new);
+ QDECREF(tests_dict);
+}
+
/*
* Errors test-cases
*/
@@ -365,6 +393,7 @@ int main(int argc, char **argv)
g_test_add_func("/public/del", qdict_del_test);
g_test_add_func("/public/to_qdict", qobject_to_qdict_test);
g_test_add_func("/public/iterapi", qdict_iterapi_test);
+ g_test_add_func("/public/copy", qdict_copy_test);
g_test_add_func("/errors/put_exists", qdict_put_exists_test);
g_test_add_func("/errors/get_not_exists", qdict_get_not_exists_test);
diff --git a/qdict.c b/qdict.c
index 4bf308b..dc9141f 100644
--- a/qdict.c
+++ b/qdict.c
@@ -401,6 +401,24 @@ const QDictEntry *qdict_next(const QDict *qdict, const QDictEntry *entry)
}
/**
+ * qdict_copy(): Build a new dictionary from an existing one.
+ */
+QDict *qdict_copy(const QDict *from)
+{
+ const QDictEntry *ent;
+ QDict *new;
+
+ new = qdict_new();
+
+ for (ent = qdict_first(from); ent; ent = qdict_next(from, ent)) {
+ qdict_put_obj(new, qdict_entry_key(ent), qdict_entry_value(ent));
+ qobject_incref(qdict_entry_value(ent));
+ }
+
+ return new;
+}
+
+/**
* qentry_destroy(): Free all the memory allocated by a QDictEntry
*/
static void qentry_destroy(QDictEntry *e)
diff --git a/qdict.h b/qdict.h
index 929d8d2..9976a18 100644
--- a/qdict.h
+++ b/qdict.h
@@ -36,6 +36,7 @@ typedef struct QDict {
QDict *qdict_new(void);
const char *qdict_entry_key(const QDictEntry *entry);
QObject *qdict_entry_value(const QDictEntry *entry);
+QDict *qdict_copy(const QDict *from);
size_t qdict_size(const QDict *qdict);
void qdict_put_obj(QDict *qdict, const char *key, QObject *value);
void qdict_del(QDict *qdict, const char *key);
--
1.7.9.111.gf3fb0.dirty
next prev parent reply other threads:[~2012-02-10 19:31 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-10 19:31 [Qemu-devel] [PATCH 0/6] qapi: Convert migrate Luiz Capitulino
2012-02-10 19:31 ` [Qemu-devel] [PATCH 1/6] QError: Introduce new errors for the migration command Luiz Capitulino
2012-02-15 13:10 ` Juan Quintela
2012-02-10 19:31 ` [Qemu-devel] [PATCH 2/6] monitor: Introduce qemu_get_fd() Luiz Capitulino
2012-02-15 13:11 ` Juan Quintela
2012-02-10 19:31 ` Luiz Capitulino [this message]
2012-02-15 13:10 ` [Qemu-devel] [PATCH 3/6] QDict: Introduce qdict_copy() Juan Quintela
2012-02-15 17:01 ` Luiz Capitulino
2012-02-10 19:31 ` [Qemu-devel] [PATCH 4/6] Error: Introduce error_copy() Luiz Capitulino
2012-02-15 9:04 ` Paolo Bonzini
2012-02-15 13:05 ` Luiz Capitulino
2012-02-15 13:16 ` Paolo Bonzini
2012-02-15 13:27 ` Juan Quintela
2012-02-10 19:31 ` [Qemu-devel] [PATCH 5/6] Purge migration of (almost) everything to do with monitors Luiz Capitulino
2012-02-15 9:02 ` Jan Kiszka
2012-02-15 12:53 ` Luiz Capitulino
2012-02-15 13:32 ` Jan Kiszka
2012-02-15 13:15 ` Juan Quintela
2012-02-10 19:31 ` [Qemu-devel] [PATCH 6/6] qapi: Convert migrate Luiz Capitulino
2012-02-15 9:07 ` Wen Congyang
2012-02-15 13:06 ` Luiz Capitulino
2012-02-15 13:25 ` Juan Quintela
2012-02-15 16:01 ` Michael Roth
2012-02-15 17:13 ` Luiz Capitulino
2012-02-15 13:31 ` Jan Kiszka
2012-02-15 13:44 ` Jan Kiszka
2012-02-15 8:59 ` [Qemu-devel] [PATCH 0/6] " Jan Kiszka
2012-02-15 12:49 ` Luiz Capitulino
2012-02-15 13:34 ` Jan Kiszka
2012-02-15 17:23 ` Luiz Capitulino
2012-02-15 17:39 ` Jan Kiszka
2012-02-15 17:49 ` Luiz Capitulino
2012-02-15 17:56 ` Jan Kiszka
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1328902266-25308-4-git-send-email-lcapitulino@redhat.com \
--to=lcapitulino@redhat.com \
--cc=aliguori@us.ibm.com \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).