From patchwork Tue Dec 10 15:47:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Rosbrook X-Patchwork-Id: 11282865 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 1BB0F14BD for ; Tue, 10 Dec 2019 15:49:00 +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 EBFDC2077B for ; Tue, 10 Dec 2019 15:48:59 +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="sTkcmyd/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EBFDC2077B 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 1iehk2-0002t6-H5; Tue, 10 Dec 2019 15:48:02 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iehk0-0002sh-QL for xen-devel@lists.xenproject.org; Tue, 10 Dec 2019 15:48:00 +0000 X-Inumbo-ID: 6ce50dd0-1b64-11ea-a1e1-bc764e2007e4 Received: from mail-qt1-x842.google.com (unknown [2607:f8b0:4864:20::842]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 6ce50dd0-1b64-11ea-a1e1-bc764e2007e4; Tue, 10 Dec 2019 15:47:52 +0000 (UTC) Received: by mail-qt1-x842.google.com with SMTP id 38so3130244qtb.13 for ; Tue, 10 Dec 2019 07:47:52 -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=sTkcmyd/QE8Ga9ksK7HINz32oVWTxWm1Oz6EGNaTzojblW3hjqInZ6PmX4cd1ng+Y5 9oRhcuErUmHvFXvP1gyo+oq3AC2E1njw5ZDDUxzBnRJv4u9xsZq2QOvzMLaIjJo83kpG hjvUGpC3+TcULSaVXyUCgVI6cKxcyVb56B1jwjHb328B+4XDSVnDGagFP094+FAQzffn I90MQMqjpTdBcmMWfctsVlJEJKHO4M98iT5fHB+BCP8CMFfrKGn4KLDqgtAEBUeqXu2V CdC4FYdXLlN60oZ2Qs/N5GvDA1l/5b/CyXuWjucl0n+a6VfODFooCEPbiAmvAwQDTz74 QN7w== 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=Xm2qklwdGOkVvoq0cAbZs6e0bvMMamMfXQXXE1uYUfLmjlXZ5uqudUx823zahKE6nq Ffv8u7NV7P2oPp6l6qB+2FHImm0XyDcw7wirl461q2E2mhofhpCSw45qmKTeqL80MOOT Py0h+RBwgHMjqw12NhVBQo0z1t3CfT5Lh2uIX+UhbFTif+pgWnUJDOg0vEfmgKne5IOp ubqmCPt2oxeQfTBfE9zAOXg8pHRI1XyziBU03MD3U2mBuPMWtsufifZWor4AyB6e1fjT M7qDw4I1JgvS3Bip2KkZ/ng1Gw94K+uHsJ/W8zCh1gnnXXrkGXnGVqAv78FYQ9Hrv08f DPww== X-Gm-Message-State: APjAAAUFd+UTpJpUbP7efQvYMsGL01HzF/DVc2iDd96bPeF/Mzs74nBK OMKsTvUF1wz9b3DJv7cKO/+jlOHQ X-Google-Smtp-Source: APXvYqyQ4WyzJYCN4Qe97lJpPL1oO/AQYKsGsBR++9pRgagf/KT88xX6WmiZPrKygBsCH9qI/7sgzw== X-Received: by 2002:ac8:5513:: with SMTP id j19mr30537125qtq.143.1575992871961; Tue, 10 Dec 2019 07:47:51 -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 m29sm1247848qtf.1.2019.12.10.07.47.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Dec 2019 07:47:50 -0800 (PST) From: Nick Rosbrook To: xen-devel@lists.xenproject.org Date: Tue, 10 Dec 2019 10:47:17 -0500 Message-Id: X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v3 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