From patchwork Tue Feb 9 11:37:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 8260491 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B62559F38B for ; Tue, 9 Feb 2016 11:48:54 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id A33B120114 for ; Tue, 9 Feb 2016 11:48:53 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6735920260 for ; Tue, 9 Feb 2016 11:48:52 +0000 (UTC) Received: from localhost ([::1]:54662 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aT6mp-0001pp-Dc for patchwork-qemu-devel@patchwork.kernel.org; Tue, 09 Feb 2016 06:48:51 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34183) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aT6cV-0007q5-Hx for qemu-devel@nongnu.org; Tue, 09 Feb 2016 06:38:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aT6cS-0002z4-LP for qemu-devel@nongnu.org; Tue, 09 Feb 2016 06:38:11 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51558) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aT6cS-0002yP-C2 for qemu-devel@nongnu.org; Tue, 09 Feb 2016 06:38:08 -0500 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 08C062DC379 for ; Tue, 9 Feb 2016 11:38:08 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-34.ams2.redhat.com [10.36.116.34]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u19Bc5xZ027314 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 9 Feb 2016 06:38:07 -0500 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 908FD3011FAB; Tue, 9 Feb 2016 12:38:03 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Tue, 9 Feb 2016 12:37:38 +0100 Message-Id: <1455017883-25867-7-git-send-email-armbru@redhat.com> In-Reply-To: <1455017883-25867-1-git-send-email-armbru@redhat.com> References: <1455017883-25867-1-git-send-email-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL 06/31] qapi: Avoid use of misnamed DO_UPCAST() 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Blake The macro DO_UPCAST() is incorrectly named: it converts from a parent class to a derived class (which is a downcast). Better, and more consistent with some of the other qapi visitors, is to use the container_of() macro through a to_FOO() helper. Names like 'to_ov()' may be a bit short, but for a static helper it doesn't hurt too much, and matches existing practice in files like qmp-input-visitor.c. Our current definition of container_of() is weaker than DO_UPCAST(), in that it does not require the derived class to have Visitor as its first member, but this does not hurt our usage patterns in qapi visitors. Signed-off-by: Eric Blake Reviewed-by: Marc-André Lureau Message-Id: <1454075341-13658-3-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster --- qapi/opts-visitor.c | 28 +++++++++++++++++----------- qapi/string-input-visitor.c | 23 ++++++++++++++--------- qapi/string-output-visitor.c | 21 +++++++++++++-------- 3 files changed, 44 insertions(+), 28 deletions(-) diff --git a/qapi/opts-visitor.c b/qapi/opts-visitor.c index eb7d85b..4955ac8 100644 --- a/qapi/opts-visitor.c +++ b/qapi/opts-visitor.c @@ -90,6 +90,12 @@ struct OptsVisitor }; +static OptsVisitor *to_ov(Visitor *v) +{ + return container_of(v, OptsVisitor, visitor); +} + + static void destroy_list(gpointer list) { @@ -122,7 +128,7 @@ static void opts_start_struct(Visitor *v, void **obj, const char *kind, const char *name, size_t size, Error **errp) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); const QemuOpt *opt; if (obj) { @@ -161,7 +167,7 @@ ghr_true(gpointer ign_key, gpointer ign_value, gpointer ign_user_data) static void opts_end_struct(Visitor *v, Error **errp) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); GQueue *any; if (--ov->depth > 0) { @@ -203,7 +209,7 @@ lookup_distinct(const OptsVisitor *ov, const char *name, Error **errp) static void opts_start_list(Visitor *v, const char *name, Error **errp) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); /* we can't traverse a list in a list */ assert(ov->list_mode == LM_NONE); @@ -217,7 +223,7 @@ opts_start_list(Visitor *v, const char *name, Error **errp) static GenericList * opts_next_list(Visitor *v, GenericList **list, Error **errp) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); GenericList **link; switch (ov->list_mode) { @@ -266,7 +272,7 @@ opts_next_list(Visitor *v, GenericList **list, Error **errp) static void opts_end_list(Visitor *v, Error **errp) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); assert(ov->list_mode == LM_STARTED || ov->list_mode == LM_IN_PROGRESS || @@ -308,7 +314,7 @@ processed(OptsVisitor *ov, const char *name) static void opts_type_str(Visitor *v, char **obj, const char *name, Error **errp) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); const QemuOpt *opt; opt = lookup_scalar(ov, name, errp); @@ -324,7 +330,7 @@ opts_type_str(Visitor *v, char **obj, const char *name, Error **errp) static void opts_type_bool(Visitor *v, bool *obj, const char *name, Error **errp) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); const QemuOpt *opt; opt = lookup_scalar(ov, name, errp); @@ -357,7 +363,7 @@ opts_type_bool(Visitor *v, bool *obj, const char *name, Error **errp) static void opts_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); const QemuOpt *opt; const char *str; long long val; @@ -413,7 +419,7 @@ opts_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp) static void opts_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); const QemuOpt *opt; const char *str; unsigned long long val; @@ -465,7 +471,7 @@ opts_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp) static void opts_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); const QemuOpt *opt; int64_t val; char *endptr; @@ -491,7 +497,7 @@ opts_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp) static void opts_optional(Visitor *v, bool *present, const char *name) { - OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v); + OptsVisitor *ov = to_ov(v); /* we only support a single mandatory scalar field in a list node */ assert(ov->list_mode == LM_NONE); diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index 8c5ff7e..52db4cf 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -33,6 +33,11 @@ struct StringInputVisitor const char *string; }; +static StringInputVisitor *to_siv(Visitor *v) +{ + return container_of(v, StringInputVisitor, visitor); +} + static void free_range(void *range, void *dummy) { g_free(range); @@ -121,7 +126,7 @@ error: static void start_list(Visitor *v, const char *name, Error **errp) { - StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); + StringInputVisitor *siv = to_siv(v); parse_str(siv, errp); @@ -137,7 +142,7 @@ start_list(Visitor *v, const char *name, Error **errp) static GenericList * next_list(Visitor *v, GenericList **list, Error **errp) { - StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); + StringInputVisitor *siv = to_siv(v); GenericList **link; Range *r; @@ -176,14 +181,14 @@ next_list(Visitor *v, GenericList **list, Error **errp) static void end_list(Visitor *v, Error **errp) { - StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); + StringInputVisitor *siv = to_siv(v); siv->head = true; } static void parse_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp) { - StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); + StringInputVisitor *siv = to_siv(v); if (!siv->string) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", @@ -225,7 +230,7 @@ error: static void parse_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp) { - StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); + StringInputVisitor *siv = to_siv(v); Error *err = NULL; uint64_t val; @@ -247,7 +252,7 @@ static void parse_type_size(Visitor *v, uint64_t *obj, const char *name, static void parse_type_bool(Visitor *v, bool *obj, const char *name, Error **errp) { - StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); + StringInputVisitor *siv = to_siv(v); if (siv->string) { if (!strcasecmp(siv->string, "on") || @@ -271,7 +276,7 @@ static void parse_type_bool(Visitor *v, bool *obj, const char *name, static void parse_type_str(Visitor *v, char **obj, const char *name, Error **errp) { - StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); + StringInputVisitor *siv = to_siv(v); if (siv->string) { *obj = g_strdup(siv->string); } else { @@ -283,7 +288,7 @@ static void parse_type_str(Visitor *v, char **obj, const char *name, static void parse_type_number(Visitor *v, double *obj, const char *name, Error **errp) { - StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); + StringInputVisitor *siv = to_siv(v); char *endp = (char *) siv->string; double val; @@ -302,7 +307,7 @@ static void parse_type_number(Visitor *v, double *obj, const char *name, static void parse_optional(Visitor *v, bool *present, const char *name) { - StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v); + StringInputVisitor *siv = to_siv(v); if (!siv->string) { *present = false; diff --git a/qapi/string-output-visitor.c b/qapi/string-output-visitor.c index b04cec4..b0313cb 100644 --- a/qapi/string-output-visitor.c +++ b/qapi/string-output-visitor.c @@ -67,6 +67,11 @@ struct StringOutputVisitor GList *ranges; }; +static StringOutputVisitor *to_sov(Visitor *v) +{ + return container_of(v, StringOutputVisitor, visitor); +} + static void string_output_set(StringOutputVisitor *sov, char *string) { if (sov->string) { @@ -120,7 +125,7 @@ static void format_string(StringOutputVisitor *sov, Range *r, bool next, static void print_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp) { - StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); + StringOutputVisitor *sov = to_sov(v); GList *l; switch (sov->list_mode) { @@ -196,7 +201,7 @@ static void print_type_int(Visitor *v, int64_t *obj, const char *name, static void print_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp) { - StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); + StringOutputVisitor *sov = to_sov(v); static const char suffixes[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E' }; uint64_t div, val; char *out; @@ -227,14 +232,14 @@ static void print_type_size(Visitor *v, uint64_t *obj, const char *name, static void print_type_bool(Visitor *v, bool *obj, const char *name, Error **errp) { - StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); + StringOutputVisitor *sov = to_sov(v); string_output_set(sov, g_strdup(*obj ? "true" : "false")); } static void print_type_str(Visitor *v, char **obj, const char *name, Error **errp) { - StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); + StringOutputVisitor *sov = to_sov(v); char *out; if (sov->human) { @@ -248,14 +253,14 @@ static void print_type_str(Visitor *v, char **obj, const char *name, static void print_type_number(Visitor *v, double *obj, const char *name, Error **errp) { - StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); + StringOutputVisitor *sov = to_sov(v); string_output_set(sov, g_strdup_printf("%f", *obj)); } static void start_list(Visitor *v, const char *name, Error **errp) { - StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); + StringOutputVisitor *sov = to_sov(v); /* we can't traverse a list in a list */ assert(sov->list_mode == LM_NONE); @@ -266,7 +271,7 @@ start_list(Visitor *v, const char *name, Error **errp) static GenericList * next_list(Visitor *v, GenericList **list, Error **errp) { - StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); + StringOutputVisitor *sov = to_sov(v); GenericList *ret = NULL; if (*list) { if (sov->head) { @@ -293,7 +298,7 @@ next_list(Visitor *v, GenericList **list, Error **errp) static void end_list(Visitor *v, Error **errp) { - StringOutputVisitor *sov = DO_UPCAST(StringOutputVisitor, visitor, v); + StringOutputVisitor *sov = to_sov(v); assert(sov->list_mode == LM_STARTED || sov->list_mode == LM_END ||