From patchwork Thu Jan 24 10:23:04 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dong Xu Wang X-Patchwork-Id: 215303 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1F09D2C007E for ; Thu, 24 Jan 2013 21:23:54 +1100 (EST) Received: from localhost ([::1]:40445 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TyJyK-0002pI-1d for incoming@patchwork.ozlabs.org; Thu, 24 Jan 2013 05:23:52 -0500 Received: from eggs.gnu.org ([208.118.235.92]:53704) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TyJxz-0002dn-EP for qemu-devel@nongnu.org; Thu, 24 Jan 2013 05:23:34 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TyJxv-0008KS-HN for qemu-devel@nongnu.org; Thu, 24 Jan 2013 05:23:31 -0500 Received: from e28smtp04.in.ibm.com ([122.248.162.4]:54112) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TyJxu-0008Ia-Pz for qemu-devel@nongnu.org; Thu, 24 Jan 2013 05:23:27 -0500 Received: from /spool/local by e28smtp04.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 24 Jan 2013 15:51:41 +0530 Received: from d28dlp03.in.ibm.com (9.184.220.128) by e28smtp04.in.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 24 Jan 2013 15:51:40 +0530 Received: from d28relay03.in.ibm.com (d28relay03.in.ibm.com [9.184.220.60]) by d28dlp03.in.ibm.com (Postfix) with ESMTP id 6E01A125804C for ; Thu, 24 Jan 2013 15:53:44 +0530 (IST) Received: from d28av04.in.ibm.com (d28av04.in.ibm.com [9.184.220.66]) by d28relay03.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id r0OANGEF43450476 for ; Thu, 24 Jan 2013 15:53:16 +0530 Received: from d28av04.in.ibm.com (loopback [127.0.0.1]) by d28av04.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id r0OANIcx018227 for ; Thu, 24 Jan 2013 21:23:18 +1100 Received: from dong-T410.cn.ibm.com (dong-t410.cn.ibm.com [9.115.118.84]) by d28av04.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id r0OANE7w017931; Thu, 24 Jan 2013 21:23:17 +1100 From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Thu, 24 Jan 2013 18:23:04 +0800 Message-Id: <1359022987-6274-3-git-send-email-wdongxu@vnet.linux.ibm.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1359022987-6274-1-git-send-email-wdongxu@vnet.linux.ibm.com> References: <1359022987-6274-1-git-send-email-wdongxu@vnet.linux.ibm.com> X-Content-Scanned: Fidelis XPS MAILER x-cbid: 13012410-5564-0000-0000-00000656A53A X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 122.248.162.4 Cc: kwolf@redhat.com, Dong Xu Wang , stefanha@redhat.com Subject: [Qemu-devel] [PATCH V11 2/4] Create four opts list related functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch will create 4 functions, count_opts_list, append_opts_list, free_opts_list and print_opts_list, they will used in following commits. Signed-off-by: Dong Xu Wang --- v6->v7): 1) Fix typo. v5->v6): 1) allocate enough space in append_opts_list function. include/qemu/option.h | 4 +++ util/qemu-option.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/include/qemu/option.h b/include/qemu/option.h index 394170a..f784c2e 100644 --- a/include/qemu/option.h +++ b/include/qemu/option.h @@ -156,4 +156,8 @@ int qemu_opts_print(QemuOpts *opts, void *dummy); int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, int abort_on_failure); +QemuOptsList *append_opts_list(QemuOptsList *dest, + QemuOptsList *list); +void free_opts_list(QemuOptsList *list); +void print_opts_list(QemuOptsList *list); #endif diff --git a/util/qemu-option.c b/util/qemu-option.c index 1aed418..f4bbbf8 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -1152,3 +1152,93 @@ int qemu_opts_foreach(QemuOptsList *list, qemu_opts_loopfunc func, void *opaque, loc_pop(&loc); return rc; } + +static size_t count_opts_list(QemuOptsList *list) +{ + size_t i = 0; + + while (list && list->desc[i].name) { + i++; + } + + return i; +} + +/* Create a new QemuOptsList and make its desc to the merge of first and second. + * It will allocate space for one new QemuOptsList plus enough space for + * QemuOptDesc in first and second QemuOptsList. First argument's QemuOptDesc + * members take precedence over second's. + */ +QemuOptsList *append_opts_list(QemuOptsList *first, + QemuOptsList *second) +{ + size_t num_first_options, num_second_options; + QemuOptsList *dest = NULL; + int i = 0; + int index = 0; + + num_first_options = count_opts_list(first); + num_second_options = count_opts_list(second); + if (num_first_options + num_second_options == 0) { + return NULL; + } + + dest = g_malloc0(sizeof(QemuOptsList) + + (num_first_options + num_second_options + 1) * sizeof(QemuOptDesc)); + + dest->name = "append_opts_list"; + dest->implied_opt_name = NULL; + dest->merge_lists = false; + QTAILQ_INIT(&dest->head); + while (first && (first->desc[i].name)) { + if (!find_desc_by_name(dest->desc, first->desc[i].name)) { + dest->desc[index].name = g_strdup(first->desc[i].name); + dest->desc[index].help = g_strdup(first->desc[i].help); + dest->desc[index].type = first->desc[i].type; + dest->desc[index].def_value_str = + g_strdup(first->desc[i].def_value_str); + ++index; + } + i++; + } + i = 0; + while (second && (second->desc[i].name)) { + if (!find_desc_by_name(dest->desc, second->desc[i].name)) { + dest->desc[index].name = g_strdup(first->desc[i].name); + dest->desc[index].help = g_strdup(first->desc[i].help); + dest->desc[index].type = second->desc[i].type; + dest->desc[index].def_value_str = + g_strdup(second->desc[i].def_value_str); + ++index; + } + i++; + } + dest->desc[index].name = NULL; + return dest; +} + +void free_opts_list(QemuOptsList *list) +{ + int i = 0; + + while (list && list->desc[i].name) { + g_free((char *)list->desc[i].name); + g_free((char *)list->desc[i].help); + g_free((char *)list->desc[i].def_value_str); + i++; + } + + g_free(list); +} + +void print_opts_list(QemuOptsList *list) +{ + int i = 0; + printf("Supported options:\n"); + while (list && list->desc[i].name) { + printf("%-16s %s\n", list->desc[i].name, + list->desc[i].help ? + list->desc[i].help : "No description available"); + i++; + } +}