From patchwork Tue Dec 10 15:47:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Rosbrook X-Patchwork-Id: 11282873 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 51860138C for ; Tue, 10 Dec 2019 15:49:14 +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 2DA5D206D5 for ; Tue, 10 Dec 2019 15:49:14 +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="Yjx05uyV" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2DA5D206D5 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 1iehkH-00030L-JE; Tue, 10 Dec 2019 15:48:17 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iehkF-0002zU-RU for xen-devel@lists.xenproject.org; Tue, 10 Dec 2019 15:48:15 +0000 X-Inumbo-ID: 6f37b24a-1b64-11ea-a1e1-bc764e2007e4 Received: from mail-qv1-xf41.google.com (unknown [2607:f8b0:4864:20::f41]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 6f37b24a-1b64-11ea-a1e1-bc764e2007e4; Tue, 10 Dec 2019 15:47:56 +0000 (UTC) Received: by mail-qv1-xf41.google.com with SMTP id b18so4338651qvo.8 for ; Tue, 10 Dec 2019 07:47:56 -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=BBGUyrHNGFQPFq/o+TvXylCQY7vgNJpmV7qKIg4M/Cs=; b=Yjx05uyVX0tJ+6VD1idFE4HzfRIdrW2VKoWs/6QhrcLl0clYS8Rq/VSJYMF/GbnEiH 0F+jhy60Hb9mINGYG1IzhB3hUsjuWUC5Z2O3OYz7OvdAFLwatM3L8WzxqYAKsLVmRd6Y 9oRVQ/iUFNwGReYucx3kB57AhH/wQxFWulUEta5+NcjlUKJOeMtrBQCjN8HlJsjtUgId ohEX/8brNUCZl5jQl4EwZFAmfG/23Iq2HKrp97kuyiTtArbEPgwHCldWpgaBIBI7MwhM c4bqdeRckna1bO3cwRIISdlYoHXs7MKoJiPBTgn6cRV9ELS/WwPaH9Ic2NMsj1xgfc3k eeYA== 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=BBGUyrHNGFQPFq/o+TvXylCQY7vgNJpmV7qKIg4M/Cs=; b=YzioM/ZWal3MXa8XUQITagQ4Izw2SiPktXcs6/vQlq+CeFbF3Gpn7FULpb5dai9WdE 4QWzBqm8MuxA4D7BUxUwz1h8OYLGpM/U+9K6avDcqy2IbmbimajzpiAdNjr1Dqjrx4Hr A9SqU2TmdGloIo3fL+TlVc+qFYJzYs3C4PZozc02Tcajwogy2SE0uDLtZxt+k6FcAKy/ c7Al+oghiifJOsEkLoMIi48P8DtTEP6Yh+7H4EmGFXcb3J6zBohMq4mr6dfJAS/5OwRA e6hFLHGOky1FNLp1H1UKBb+y6bziCKfgncVzP51rjjPmunaKhcE+HFjUEgbN5xbyKxGA dBhg== X-Gm-Message-State: APjAAAXtYcbv88ALl81gX4c9t+FNlDYgHZ8yiwlwpYNNxq/M/Xf35tRR bjUjkgFMq9jQ2OJ7WIpaaSutK/Ok X-Google-Smtp-Source: APXvYqyxqDs6L4y/NujGTJemWBuyzz4BTtAgbsJN5j+PS1on0omvebxiKpX9wSMHsodSqFqB8NTnyg== X-Received: by 2002:a0c:f792:: with SMTP id s18mr29619358qvn.118.1575992875684; Tue, 10 Dec 2019 07:47:55 -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.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 Dec 2019 07:47:55 -0800 (PST) From: Nick Rosbrook To: xen-devel@lists.xenproject.org Date: Tue, 10 Dec 2019 10:47:20 -0500 Message-Id: <688067dbf85cc8f2487165af21379039dcc30e9a.1575990937.git.rosbrookn@ainfosec.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [Xen-devel] [PATCH v3 05/22] golang/xenlight: re-name Bitmap marshaling functions 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 Re-name and modify signature of toGo function to fromC. The reason for using 'fromC' rather than 'toGo' is that it is not a good idea to define methods on the C types. Also, add error return type to Bitmap's toC function. Finally, as code-cleanup, re-organize the Bitmap type's comments as per Go conventions. Signed-off-by: Nick Rosbrook Acked-by: George Dunlap --- Changes in v2: - Use consistent variable naming for slice created from libxl_bitmap. --- tools/golang/xenlight/xenlight.go | 94 ++++++++++++++++--------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go index 3edff18471..1c5e3c0cc7 100644 --- a/tools/golang/xenlight/xenlight.go +++ b/tools/golang/xenlight/xenlight.go @@ -212,20 +212,48 @@ type KeyValueList struct{} func (kvl KeyValueList) fromC(ckvl *C.libxl_key_value_list) error { return nil } func (kvl KeyValueList) toC() (ckvl C.libxl_key_value_list, err error) { return } -// typedef struct { -// uint32_t size; /* number of bytes in map */ -// uint8_t *map; -// } libxl_bitmap; - +// Bitmap represents a 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 // a C function a pointer to a Go-allocated structure which contains a // pointer. type Bitmap struct { + // typedef struct { + // uint32_t size; /* number of bytes in map */ + // uint8_t *map; + // } libxl_bitmap; bitmap []C.uint8_t } +func (bm *Bitmap) fromC(cbm *C.libxl_bitmap) error { + // Alloc a Go slice for the bytes + size := int(cbm.size) + bm.bitmap = make([]C.uint8_t, size) + + // Make a slice pointing to the C array + cs := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size] + + // And copy the C array into the Go array + copy(bm.bitmap, cs) + + return nil +} + +func (bm *Bitmap) toC() (C.libxl_bitmap, error) { + var cbm C.libxl_bitmap + + size := len(bm.bitmap) + cbm.size = C.uint32_t(size) + cbm._map = (*C.uint8_t)(C.malloc(C.ulong(cbm.size) * C.sizeof_uint8_t)) + cs := (*[1 << 31]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size] + + copy(cs, bm.bitmap) + + return cbm, nil +} + /* * Types: IDL * @@ -426,7 +454,7 @@ func (cci C.libxl_cpupoolinfo) toGo() (gci CpupoolInfo) { gci.PoolName = C.GoString(cci.pool_name) gci.Scheduler = Scheduler(cci.sched) gci.DomainCount = int(cci.n_dom) - gci.Cpumap = cci.cpumap.toGo() + gci.Cpumap.fromC(&cci.cpumap) return } @@ -500,7 +528,10 @@ func (Ctx *Context) CpupoolCreate(Name string, Scheduler Scheduler, Cpumap Bitma var uuid C.libxl_uuid C.libxl_uuid_generate(&uuid) - cbm := Cpumap.toC() + cbm, err := Cpumap.toC() + if err != nil { + return + } defer C.libxl_bitmap_dispose(&cbm) ret := C.libxl_cpupool_create(Ctx.ctx, name, C.libxl_scheduler(Scheduler), @@ -555,7 +586,10 @@ func (Ctx *Context) CpupoolCpuaddCpumap(Poolid uint32, Cpumap Bitmap) (err error return } - cbm := Cpumap.toC() + cbm, err := Cpumap.toC() + if err != nil { + return + } defer C.libxl_bitmap_dispose(&cbm) ret := C.libxl_cpupool_cpuadd_cpumap(Ctx.ctx, C.uint32_t(Poolid), &cbm) @@ -591,7 +625,10 @@ func (Ctx *Context) CpupoolCpuremoveCpumap(Poolid uint32, Cpumap Bitmap) (err er return } - cbm := Cpumap.toC() + cbm, err := Cpumap.toC() + if err != nil { + return + } defer C.libxl_bitmap_dispose(&cbm) ret := C.libxl_cpupool_cpuremove_cpumap(Ctx.ctx, C.uint32_t(Poolid), &cbm) @@ -714,41 +751,6 @@ func (Ctx *Context) CpupoolMakeFree(Cpumap Bitmap) (err error) { * Bitmap operations */ -// Return a Go bitmap which is a copy of the referred C bitmap. -func (cbm C.libxl_bitmap) toGo() (gbm Bitmap) { - // Alloc a Go slice for the bytes - size := int(cbm.size) - gbm.bitmap = make([]C.uint8_t, size) - - // Make a slice pointing to the C array - mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size] - - // And copy the C array into the Go array - copy(gbm.bitmap, mapslice) - - return -} - -// Must be C.libxl_bitmap_dispose'd of afterwards -func (gbm Bitmap) toC() (cbm C.libxl_bitmap) { - C.libxl_bitmap_init(&cbm) - - size := len(gbm.bitmap) - cbm._map = (*C.uint8_t)(C.malloc(C.size_t(size))) - cbm.size = C.uint32_t(size) - if cbm._map == nil { - panic("C.calloc failed!") - } - - // Make a slice pointing to the C array - mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size] - - // And copy the Go array into the C array - copy(mapslice, gbm.bitmap) - - return -} - func (bm *Bitmap) Test(bit int) bool { ubit := uint(bit) if bit > bm.Max() || bm.bitmap == nil { @@ -1137,8 +1139,8 @@ func (cvci C.libxl_vcpuinfo) toGo() (gvci Vcpuinfo) { gvci.Blocked = bool(cvci.blocked) gvci.Running = bool(cvci.running) gvci.VCpuTime = time.Duration(cvci.vcpu_time) - gvci.Cpumap = cvci.cpumap.toGo() - gvci.CpumapSoft = cvci.cpumap_soft.toGo() + gvci.Cpumap.fromC(&cvci.cpumap) + gvci.CpumapSoft.fromC(&cvci.cpumap_soft) return }