From patchwork Mon Oct 7 15:12:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Rosbrook X-Patchwork-Id: 11177779 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 EBE5E112B for ; Mon, 7 Oct 2019 15:14:53 +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 C7F0F2070B for ; Mon, 7 Oct 2019 15:14:53 +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="YxdGMLfD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C7F0F2070B 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 1iHUhj-0006oN-3Z; Mon, 07 Oct 2019 15:13:43 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iHUhh-0006o1-NB for xen-devel@lists.xenproject.org; Mon, 07 Oct 2019 15:13:41 +0000 X-Inumbo-ID: 004593ce-e915-11e9-8c93-bc764e2007e4 Received: from mail-qt1-x844.google.com (unknown [2607:f8b0:4864:20::844]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 004593ce-e915-11e9-8c93-bc764e2007e4; Mon, 07 Oct 2019 15:13:22 +0000 (UTC) Received: by mail-qt1-x844.google.com with SMTP id r5so19712303qtd.0 for ; Mon, 07 Oct 2019 08:13:22 -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=TJRUOKvva5SLQ3Yr7539G+VB4p4r5aLPOvKs+CyiVbA=; b=YxdGMLfDM9zzVkeza3Z/PJHpdIk+Ls3pmKxvgSkI0YCRhMEcAIIEyft7svew0LH6jq sAUaT9TxcfrHdhLYcH7gqMwzlvyg9ekCR7cyVapi3MtlEUo0EXfVWhsOcmUCqiwQRIg5 10UWzY0FPaffJ2cO3utvRGMK519XY5sfgf0LbjNE/xGSI1faEWaRc7pSA/mdtq1xWe8I dOxYGPFBv1DhS7lblDGccbSNwev20PNUN2D4FVXylAa8qQGh/xGu3s5mASRkujDMNqvf sqRDL1zLRxyVhVYAtmFktCRpIkoLQV0JgWrVQjxi2ALhR/x2UZWTRrZQD32YISMoCw6q Mlwg== 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=TJRUOKvva5SLQ3Yr7539G+VB4p4r5aLPOvKs+CyiVbA=; b=AC4s8JyGdPypGMgpDcB7wgAjsGFSJKucqw1ZUP1ndtB+45TGUact5Zdn0Z7AOw5YqT 4/uAXjiJ1UTF/9tGQ3rguldfIeIJ0BDhbyj9m2eQrfV3Xpq6UnzxhXOehYwSWYytbh83 azuMSUoKiwVXcfFr9sqM+WaR6ij1a8fxhSocQu0Ce1FKdyY106K43wvvZs3S+y/ABRF0 e2V4QFl5wB+Wm/CmCKwao4cAxfVbe5XqtM3grx/8lbEeoIg3B+RSUKMOICQmedlwVFEW KoZfg8nPAL7Rln4d/eXxOFNUP5wEKxP2GcOR3/zMKRrExE1nniy7gwyZ3AcLT2OLH0cn Ux9Q== X-Gm-Message-State: APjAAAWDG7IK0xZy79QEOCLMw9VGgIqHEBVFtgCqLhf2TPzJBY305EpL nhQRuxV7JMfyQbzE5hkgjUSwduPo X-Google-Smtp-Source: APXvYqzVk64vhu/ZdN8eKk0DeCll8ZmeZyAj6CigP0BLVi6+bqSdNrny7mL7x03P5QjLROfXVNee4Q== X-Received: by 2002:a0c:88f0:: with SMTP id 45mr27359942qvo.78.1570461201345; Mon, 07 Oct 2019 08:13:21 -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.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2019 08:13:20 -0700 (PDT) From: Nick Rosbrook To: xen-devel@lists.xenproject.org Date: Mon, 7 Oct 2019 11:12:52 -0400 Message-Id: <1a60b855c0886c8e7147d48923f16b4d0815db81.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 05/24] golang/xenlight: define KeyValueList 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 KeyValueList builtin type, analagous to libxl_key_value_list as map[string]string, and implement its fromC and toC functions. Signed-off-by: Nick Rosbrook --- Cc: George Dunlap Cc: Ian Jackson Cc: Wei Liu tools/golang/xenlight/xenlight.go | 33 ++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go index 4d4fad2a9d..8196a42855 100644 --- a/tools/golang/xenlight/xenlight.go +++ b/tools/golang/xenlight/xenlight.go @@ -202,11 +202,42 @@ func (chwcap C.libxl_hwcap) toGo() (ghwcap Hwcap) { return } +// KeyValueList represents a libxl_key_value_list. +type KeyValueList map[string]string + +func (kvl KeyValueList) fromC(ckvl *C.libxl_key_value_list) error { + size := int(C.libxl_key_value_list_length(ckvl)) + list := (*[1 << 30]*C.char)(unsafe.Pointer(ckvl))[:size:size] + + for i := 0; i < size*2; i += 2 { + kvl[C.GoString(list[i])] = C.GoString(list[i+1]) + } + + return nil +} + +func (kvl KeyValueList) toC() (C.libxl_key_value_list, error) { + // Add extra slot for sentinel + var char *C.char + csize := 2*len(kvl) + 1 + ckvl := (C.libxl_key_value_list)(C.malloc(C.ulong(csize) * C.ulong(unsafe.Sizeof(char)))) + clist := (*[1 << 31]*C.char)(unsafe.Pointer(ckvl))[:csize:csize] + + i := 0 + for k, v := range kvl { + clist[i] = C.CString(k) + clist[i+1] = C.CString(v) + i += 2 + } + clist[len(clist)-1] = nil + + return ckvl, nil +} + // typedef struct { // uint32_t size; /* number of bytes in map */ // uint8_t *map; // } libxl_bitmap; - // Implement the Go bitmap type such that the underlying data can // easily be copied in and out. NB that we still have to do copies // both directions, because cgo runtime restrictions forbid passing to