From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37506) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WQn62-0000Wn-M6 for qemu-devel@nongnu.org; Thu, 20 Mar 2014 20:14:07 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WQn5x-0007XH-6d for qemu-devel@nongnu.org; Thu, 20 Mar 2014 20:14:02 -0400 Received: from mail-yh0-f50.google.com ([209.85.213.50]:43641) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WQn5x-0007XD-3Y for qemu-devel@nongnu.org; Thu, 20 Mar 2014 20:13:57 -0400 Received: by mail-yh0-f50.google.com with SMTP id c41so1702205yho.23 for ; Thu, 20 Mar 2014 17:13:56 -0700 (PDT) From: Leandro Dorileo Date: Thu, 20 Mar 2014 21:13:11 -0300 Message-Id: <1395360813-2833-5-git-send-email-l@dorileo.org> In-Reply-To: <1395360813-2833-1-git-send-email-l@dorileo.org> References: <1395360813-2833-1-git-send-email-l@dorileo.org> Subject: [Qemu-devel] [PATCH 04/26] QemuOpt: introduce qemu_opts_append() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Fam Zheng , Stefan Hajnoczi , Liu Yuan , Jeff Cody , Markus Armbruster , Peter Lieven , "Richard W.M. Jones" , Luiz Capitulino , Chunyan Liu , Leandro Dorileo , Ronnie Sahlberg , Josh Durgin , Anthony Liguori , Paolo Bonzini , Stefan Weil , Max Reitz , MORITA Kazutaka , Benoit Canet From: Chunyan Liu The qemu_opts_append() function is intended to merge to different QemuOptsList's. The resulting list must be freed by its user. Signed-off-by: Chunyan Liu Signed-off-by: Leandro Dorileo --- include/qemu/option.h | 1 + util/qemu-option.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/include/qemu/option.h b/include/qemu/option.h index f0d4798..3f4976d 100644 --- a/include/qemu/option.h +++ b/include/qemu/option.h @@ -157,6 +157,7 @@ int qemu_opt_foreach(QemuOpts *opts, qemu_opt_loopfunc func, void *opaque, QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id); QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists, Error **errp); +QemuOptsList *qemu_opts_append(QemuOptsList *head, QemuOptsList *tail); void qemu_opts_reset(QemuOptsList *list); void qemu_opts_loc_restore(QemuOpts *opts); int qemu_opts_set(QemuOptsList *list, const char *id, diff --git a/util/qemu-option.c b/util/qemu-option.c index 612a966..026d2ff 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -863,6 +863,75 @@ QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, return opts; } +static size_t count_opts_list(QemuOptsList *list) +{ + QemuOptDesc *desc = NULL; + size_t num_opts = 0; + + if (!list) { + return 0; + } + + desc = list->desc; + while (desc && desc->name) { + num_opts++; + desc++; + } + + return num_opts; +} + +/** + * Merge two QEMUOptsList. First argument's QemuOptDesc members take precedence + * over second's. + * + * @note The result's name and implied_opt_name are not copied from them. + * Both merge_lists should not be set. Both lists can be NULL. + * + * The resulting QemuOptsList should be freed by this functions caller. + */ +QemuOptsList *qemu_opts_append(QemuOptsList *dst, QemuOptsList *list) +{ + size_t num_opts, num_dst_opts; + QemuOptsList *tmp; + QemuOptDesc *desc; + + if (!dst && !list) { + return NULL; + } + + num_opts = count_opts_list(dst); + num_opts += count_opts_list(list); + tmp = g_malloc0(sizeof(QemuOptsList) + + (num_opts + 1) * sizeof(QemuOptDesc)); + QTAILQ_INIT(&tmp->head); + num_dst_opts = 0; + + /* copy dst->desc to new list */ + if (dst) { + desc = dst->desc; + while (desc && desc->name) { + tmp->desc[num_dst_opts++] = *desc; + tmp->desc[num_dst_opts].name = NULL; + desc++; + } + } + + /* add list->desc to new list */ + if (list) { + desc = list->desc; + while (desc && desc->name) { + if (find_desc_by_name(tmp->desc, desc->name) == NULL) { + tmp->desc[num_dst_opts++] = *desc; + tmp->desc[num_dst_opts].name = NULL; + } + desc++; + } + } + + return tmp; +} + void qemu_opts_reset(QemuOptsList *list) { QemuOpts *opts, *next_opts; -- 1.9.0