From patchwork Tue Jun 16 12:49:07 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?UTF-8?B?Wm9sdMOhbiBLxZF2w6Fnw7M=?= X-Patchwork-Id: 484976 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 02F6A140281 for ; Tue, 16 Jun 2015 22:53:37 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=KEqOiOBd; dkim-atps=neutral Received: from localhost ([::1]:40023 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4qMx-00010h-7g for incoming@patchwork.ozlabs.org; Tue, 16 Jun 2015 08:53:35 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48916) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4qIq-0002tU-L7 for qemu-devel@nongnu.org; Tue, 16 Jun 2015 08:49:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z4qIm-0005vc-CU for qemu-devel@nongnu.org; Tue, 16 Jun 2015 08:49:20 -0400 Received: from mail-wg0-x234.google.com ([2a00:1450:400c:c00::234]:33415) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z4qIm-0005vF-70 for qemu-devel@nongnu.org; Tue, 16 Jun 2015 08:49:16 -0400 Received: by wgez8 with SMTP id z8so11457210wge.0 for ; Tue, 16 Jun 2015 05:49:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=mxlCPhpgjC+6CYM3OwxpKGhYybFBlLECj4nitgkln1o=; b=KEqOiOBdlw/htQbFYhI1VUtCcCPR37Q6W1eeQBwhTi2to1//Ox/Ctk0JZ2xGd4GuW8 X5GJpI9bnIbLCUnslRkK86z7rRnxu4uTfD7LLbPe2ncMcYVxC/VUvqn3fAtOGJzIzCJG +MFiOllUv8fZapzeKS8G73rZbibBHOP7xQjbK8bqm3v8qQ1rVIaAJGuG7aH+KXv4KKkt FoXPNdxhr3bBFrtPeZH25GitOEDX2JBFaVBYw+u+egD2Bk0OYHcdRUK2mHxKbO3FaUek TL1i/RG42i2oF7lzFdvoHJ+ITxrsR7pB5kbBgsBnTKIEQltLObkIFIvYfG2g0E8zpWCc Dmnw== X-Received: by 10.180.206.211 with SMTP id lq19mr3484415wic.81.1434458955524; Tue, 16 Jun 2015 05:49:15 -0700 (PDT) Received: from nullptr.home.dirty-ice.org (178-164-167-192.pool.digikabel.hu. [178.164.167.192]) by mx.google.com with ESMTPSA id ez19sm20657456wid.19.2015.06.16.05.49.14 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 16 Jun 2015 05:49:15 -0700 (PDT) From: "=?UTF-8?q?K=C5=91v=C3=A1g=C3=B3=2C=20Zolt=C3=A1n?=" X-Google-Original-From: =?UTF-8?q?K=C5=91v=C3=A1g=C3=B3=2C=20Zolt=C3=A1n?= To: qemu-devel@nongnu.org Date: Tue, 16 Jun 2015 14:49:07 +0200 Message-Id: X-Mailer: git-send-email 2.4.3 In-Reply-To: References: MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c00::234 Cc: Michael Roth , Gerd Hoffmann , Markus Armbruster Subject: [Qemu-devel] [PATCH v2 4/6] qapi: AllocVisitor 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 Simple visitor that recursively allocates structures with only optional variables. Unions are initialized to the first type specified. Other non optional types are not supported. Signed-off-by: Kővágó, Zoltán --- include/qapi/alloc-visitor.h | 18 +++++++++++++ qapi/Makefile.objs | 1 + qapi/alloc-visitor.c | 62 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 include/qapi/alloc-visitor.h create mode 100644 qapi/alloc-visitor.c diff --git a/include/qapi/alloc-visitor.h b/include/qapi/alloc-visitor.h new file mode 100644 index 0000000..3d54295 --- /dev/null +++ b/include/qapi/alloc-visitor.h @@ -0,0 +1,18 @@ +/* + * Alloc Visitor. + * Recursively allocates structs, leaving all optional fields unset. In case of + * a non-optional field it fails. + */ + +#ifndef ALLOC_VISITOR_H +#define ALLOC_VISITOR_H + +#include "qapi/visitor.h" + +typedef struct AllocVisitor AllocVisitor; + +AllocVisitor *alloc_visitor_new(void); +void alloc_visitor_cleanup(AllocVisitor *v); +Visitor *alloc_visitor_get_visitor(AllocVisitor *v); + +#endif diff --git a/qapi/Makefile.objs b/qapi/Makefile.objs index 2278970..7bc26a3 100644 --- a/qapi/Makefile.objs +++ b/qapi/Makefile.objs @@ -4,3 +4,4 @@ util-obj-y += string-input-visitor.o string-output-visitor.o util-obj-y += opts-visitor.o util-obj-y += qmp-event.o util-obj-y += qapi-util.o +util-obj-y += alloc-visitor.o diff --git a/qapi/alloc-visitor.c b/qapi/alloc-visitor.c new file mode 100644 index 0000000..dbb83af --- /dev/null +++ b/qapi/alloc-visitor.c @@ -0,0 +1,62 @@ +#include "qapi/alloc-visitor.h" +#include "qemu-common.h" +#include "qapi/visitor-impl.h" + +struct AllocVisitor { + Visitor visitor; +}; + +static void alloc_start_struct(Visitor *v, void **obj, const char* kind, + const char *name, size_t size, Error **errp) +{ + if (obj) { + *obj = g_malloc0(size); + } +} + +static void alloc_end_struct(Visitor *v, Error **errp) +{ +} + +static void alloc_start_implicit_struct(Visitor *v, void **obj, size_t size, + Error **errp) +{ + if (obj) { + *obj = g_malloc0(size); + } +} + +static void alloc_end_implicit_struct(Visitor *v, Error **errp) +{ +} + +static void alloc_type_enum(Visitor *v, int *obj, const char *strings[], + const char *kind, const char *name, Error **errp) +{ + assert(*strings); /* there is at least one valid enum value... */ + *obj = 0; +} + +AllocVisitor *alloc_visitor_new(void) +{ + AllocVisitor *v = g_malloc0(sizeof(AllocVisitor)); + + v->visitor.start_struct = alloc_start_struct; + v->visitor.end_struct = alloc_end_struct; + v->visitor.start_implicit_struct = alloc_start_implicit_struct; + v->visitor.end_implicit_struct = alloc_end_implicit_struct; + + v->visitor.type_enum = alloc_type_enum; + + return v; +} + +void alloc_visitor_cleanup(AllocVisitor *v) +{ + g_free(v); +} + +Visitor *alloc_visitor_get_visitor(AllocVisitor *v) +{ + return &v->visitor; +}