From patchwork Fri Nov 15 19:44:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Rosbrook X-Patchwork-Id: 11247017 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 962441393 for ; Fri, 15 Nov 2019 19:46:19 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 726B62073A for ; Fri, 15 Nov 2019 19:46:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cHdSGO5P" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 726B62073A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iVhWp-0001I0-5u; Fri, 15 Nov 2019 19:45:11 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iVhWn-0001Hb-FQ for xen-devel@lists.xenproject.org; Fri, 15 Nov 2019 19:45:09 +0000 X-Inumbo-ID: 69ee5754-07e0-11ea-984a-bc764e2007e4 Received: from mail-qv1-xf43.google.com (unknown [2607:f8b0:4864:20::f43]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 69ee5754-07e0-11ea-984a-bc764e2007e4; Fri, 15 Nov 2019 19:45:02 +0000 (UTC) Received: by mail-qv1-xf43.google.com with SMTP id n4so1108033qvq.9 for ; Fri, 15 Nov 2019 11:45:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Z2NYMlNEa239ID0p7i58khGBjYrcb8RrDrNqVGHrC6k=; b=cHdSGO5Pa9hdZtK9gT4kafagfrh6aiA0qIAmH9YQTSpUp9ficR2/GL30rydigxnGc1 NIdBOM41LqTqmaayhgaGb4skovSBXW7PqX4WLpCK38yOlkRlMkoHPbQBf881A5hVPPpc nDFcCLnMFIdlLja4RD1ZBYP1fOS4fVKh0JHSXn1DV7xHP+yuT0AAVez913x6io/nj7z5 DY+LSRU0KnAaDM9qaDKoJ4PrlBYrBO6/zgPW+eggGPYKkVNdlFRSj9DGqNm4rUed9eHF EZIxeq+6I5uVJEb8K/bPetyruwzuaTjRPpWXjH+lWZBd2gJodr0HzKZdJl4nY962zrpo BXEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Z2NYMlNEa239ID0p7i58khGBjYrcb8RrDrNqVGHrC6k=; b=WyjCso08xUn4dDs2JFlwvclIreVJIk4BPLJ9kRL03LffbJn8ih6pEbFUbluwp8FXnS xxMpu5N2QgKljB37UqrMtrJhgulfRtDRmVBA+0PCrL+ZqcfTsxLVLjgFJ6kSCENLfqbX jIJahMDJlsy02m1Oo/lHHALc2UfrW+HX3Aya5sF7qoyc1gpVbX1At/NRIQvKxIfDN/C0 6iLATFbsTjPI+dTjRHa1hVncYGpQGJY14OXNrnCtsT61+1F9xja7fcgRK0BpJ6X5ANvO 3xKbs4o3CPSJu7I4/2SX1Cn0xaRb54KXb+DopeuIqicPpf5wOJwnEUoBMEMPrEFHxxXl VthQ== X-Gm-Message-State: APjAAAWDIKmi1FEstoiL6+YoETJnh1qzlZ9LlpYZhSEG4QrMmDjRlvP3 CqmRsTYfyZMbGuWkmHNR6r3rWi9X X-Google-Smtp-Source: APXvYqw7FNPqkFy7t/u/nhyR6YxN0OXdubmFgFsW0T5uwZMUYuJVA3G/BPPUiJw4eIraEM3kfhdR2A== X-Received: by 2002:a0c:e603:: with SMTP id z3mr14478483qvm.50.1573847100797; Fri, 15 Nov 2019 11:45:00 -0800 (PST) Received: from five.crux.rad.ainfosec.com (209-217-208-226.northland.net. [209.217.208.226]) by smtp.googlemail.com with ESMTPSA id y10sm4649679qkb.55.2019.11.15.11.44.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2019 11:44:59 -0800 (PST) From: Nick Rosbrook To: xen-devel@lists.xenproject.org Date: Fri, 15 Nov 2019 14:44:09 -0500 Message-Id: <1997f553223508247777fb47fce9af3f658c5d74.1573840474.git.rosbrookn@ainfosec.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v2 02/22] golang/xenlight: define Defbool builtin type X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Nick Rosbrook , Ian Jackson , kerriganb@ainfosec.com, George Dunlap , Wei Liu Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" From: Nick Rosbrook Define Defbool as struct analagous to the C type, and define the type 'defboolVal' that represent true, false, and default defbool values. Implement Set, Unset, SetIfDefault, IsDefault, Val, and String functions on Defbool so that the type can be used in Go analagously to how its used in C. Finally, implement fromC and toC functions. Signed-off-by: Nick Rosbrook Reviewed-by: George Dunlap --- tools/golang/xenlight/xenlight.go | 93 +++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go index 89ed439fd0..640d82f35f 100644 --- a/tools/golang/xenlight/xenlight.go +++ b/tools/golang/xenlight/xenlight.go @@ -85,6 +85,99 @@ type MemKB uint64 type Uuid C.libxl_uuid +// defboolVal represents a defbool value. +type defboolVal int + +const ( + defboolDefault defboolVal = 0 + defboolFalse defboolVal = -1 + defboolTrue defboolVal = 1 +) + +// Defbool represents a libxl_defbool. +type Defbool struct { + val defboolVal +} + +func (d Defbool) String() string { + switch d.val { + case defboolDefault: + return "" + case defboolFalse: + return "False" + case defboolTrue: + return "True" + } + + return "" +} + +// Set sets the value of the Defbool. +func (d *Defbool) Set(b bool) { + if b { + d.val = defboolTrue + return + } + d.val = defboolFalse +} + +// Unset resets the Defbool to default value. +func (d *Defbool) Unset() { + d.val = defboolDefault +} + +// SetIfDefault sets the value of Defbool only if +// its current value is default. +func (d *Defbool) SetIfDefault(b bool) { + if d.IsDefault() { + d.Set(b) + } +} + +// IsDefault returns true if the value of Defbool +// is default, returns false otherwise. +func (d *Defbool) IsDefault() bool { + return d.val == defboolDefault +} + +// Val returns the boolean value associated with the +// Defbool value. An error is returned if the value +// is default. +func (d *Defbool) Val() (bool, error) { + if d.IsDefault() { + return false, fmt.Errorf("%v: cannot take value of default defbool", ErrorInval) + } + + return (d.val > 0), nil +} + +func (d *Defbool) fromC(c *C.libxl_defbool) error { + if C.libxl_defbool_is_default(*c) { + d.val = defboolDefault + return nil + } + + if C.libxl_defbool_val(*c) { + d.val = defboolTrue + return nil + } + + d.val = defboolFalse + + return nil +} + +func (d *Defbool) toC() (C.libxl_defbool, error) { + var c C.libxl_defbool + + if !d.IsDefault() { + val, _ := d.Val() + C.libxl_defbool_set(&c, C.bool(val)) + } + + return c, nil +} + type Context struct { ctx *C.libxl_ctx logger *C.xentoollog_logger_stdiostream