From patchwork Mon Oct 7 15:12:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Rosbrook X-Patchwork-Id: 11177773 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 5A938112B for ; Mon, 7 Oct 2019 15:14:48 +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 36B4E21479 for ; Mon, 7 Oct 2019 15:14:48 +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="jNrfBccZ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 36B4E21479 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 1iHUhY-0006ja-Cf; Mon, 07 Oct 2019 15:13:32 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iHUhX-0006jP-MN for xen-devel@lists.xenproject.org; Mon, 07 Oct 2019 15:13:31 +0000 X-Inumbo-ID: fefe6f2c-e914-11e9-8c93-bc764e2007e4 Received: from mail-qk1-x744.google.com (unknown [2607:f8b0:4864:20::744]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id fefe6f2c-e914-11e9-8c93-bc764e2007e4; Mon, 07 Oct 2019 15:13:19 +0000 (UTC) Received: by mail-qk1-x744.google.com with SMTP id w2so12924280qkf.2 for ; Mon, 07 Oct 2019 08:13:19 -0700 (PDT) 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=xQqpt0YafGJMaNYxl21IIT6y/TTnX+GZJV/giFMiXnI=; b=jNrfBccZSNbkttkZbiOwgNfoD0F67D0LOFLjxnSHLooSxGd31JsNUi5RG2fKBrwoCc aSRhQO1wK+X7sm0QM8sCEkrrp7lnPAleqP5TJGwlDkkXZ9YCXAlmf/7jCgOhrfa+17zY fGWZvZFkzcwS7YPxO9hMRRXITjDTiCWOSAkYjyVu1GIrinDWMb9cBL3663Tf7OhatBDh /zwXIRytMciZwHJbaVKpjledyPIzyL9t4fq9PQPDTRfxx5HU1XW6OrFk1oM8SdohHVby cQkZChnDHwRZzAyYw1YfMFHiWWFcTmO8Cmc6kjbB181kuX5gm14sxAoY4Ypm1xrrJbAC 1+eQ== 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=xQqpt0YafGJMaNYxl21IIT6y/TTnX+GZJV/giFMiXnI=; b=hFUIITV8IJ3XZ3t8vZFr0COjKZW0TFJmnN7zA0EfXaU7dc5sVlQ3DIGMUr7nmUuNc3 XEq8OrhsXBN0xhIhfp6gKHbV4c9HyZhtgB8rFsLKsSYZMj0soaSOEiC0re4+B+IfP+HB ANg6N9Bi9TmbbY17HoAzKQUbEr4kc7DUWHpZ4OZS9mqEtvdcUTZ/XgiHQr9haPjCUiTx 5S9TXBPEzzuyaHSGHw0vMMJctoNoLppdEpiiscX7ITOLX2Y0pgfNVqNEZHxW7mNVQuUI zoQ3jXgEAj0CNT4tpCIgFqwhoBjvnHBKeFoFA4OxdWV6TVsIBiRwOsLfSqy2e//AsQ8y tb4g== X-Gm-Message-State: APjAAAU/dfbK1ifKFOIgU8fuCOPRXG4LVtgfgUW66iMpdy761FKpSnvN JxA14LS4JviciUdq/BLxyoXZ4/F3 X-Google-Smtp-Source: APXvYqxP2rTblQcbxxoIJ5QVf27v0p3Jk4MbsLngumglIZ/QMG0qtLAewbKHoEqjs/Fp6sBTCMOjZA== X-Received: by 2002:a37:4e48:: with SMTP id c69mr23555685qkb.182.1570461199191; Mon, 07 Oct 2019 08:13:19 -0700 (PDT) Received: from five.crux.rad.ainfosec.com (209-217-208-226.northland.net. [209.217.208.226]) by smtp.googlemail.com with ESMTPSA id c185sm8354313qkg.74.2019.10.07.08.13.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2019 08:13:18 -0700 (PDT) From: Nick Rosbrook To: xen-devel@lists.xenproject.org Date: Mon, 7 Oct 2019 11:12:50 -0400 Message-Id: <00b468deae50cb125be01ff90ceb739fd25f8e5c.1570456846.git.rosbrookn@ainfosec.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 03/24] 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 --- Cc: George Dunlap Cc: Ian Jackson Cc: Wei Liu 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 e617f22fcf..7bf16dc03b 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