From patchwork Fri Nov 15 19:44:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Rosbrook X-Patchwork-Id: 11247025 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 67E96159A for ; Fri, 15 Nov 2019 19:46:37 +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 42F922053B for ; Fri, 15 Nov 2019 19:46:37 +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="ejd0rz21" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 42F922053B 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 1iVhX4-0001PC-8P; Fri, 15 Nov 2019 19:45:26 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iVhX2-0001OO-Ho for xen-devel@lists.xenproject.org; Fri, 15 Nov 2019 19:45:24 +0000 X-Inumbo-ID: 6bdb4180-07e0-11ea-9631-bc764e2007e4 Received: from mail-qk1-x743.google.com (unknown [2607:f8b0:4864:20::743]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 6bdb4180-07e0-11ea-9631-bc764e2007e4; Fri, 15 Nov 2019 19:45:05 +0000 (UTC) Received: by mail-qk1-x743.google.com with SMTP id i19so9061247qki.2 for ; Fri, 15 Nov 2019 11:45:05 -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=Yyg6acqD9xhelQayrdR16A89enPCBe73dWXMnc+J3Mk=; b=ejd0rz21/l7ql+pFRTBH7euKrPAU3uy/WDIZixTcohV/Wfpypw70QK3r6oqYXQe7t7 tM6R3JDPhuLlYQhtqgC/dri4g+sLVHBxgf0K7sRaFqkgjc2PHgB2CPbEG8Ea3X8gZXnZ JCrjwDrODQ4LkIBWH8GBfd+RZT+hurXbdEkZx24FHpJd/BxEz5gq8Lnj4eSZBxUI8rnS asR7i4ed3OV0GtSJEHNjw2X9uNvRTeiSsIhJRCcYJ4qQIGMXSQAHh0MbCRfs59d3eU7c UKZAd5BT5+Qi0VBk48ZhLqIppH9/zSmD+OeNWKn4v4wSSZsNAsKNy/RlIMpUYAuPqO70 ij1w== 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=Yyg6acqD9xhelQayrdR16A89enPCBe73dWXMnc+J3Mk=; b=k0U+gBCDANR4JFV8LSln7bffsE0/t5+1qzXTwEbrRtxpiuzJcCp/49IQYokz8HkCDa fQ3FJyaZzj4mwIamr0CrrZv90aAPDzUToixlG/K1n3s4GEJuvmlOQlHnto3rLWfQPzlT kZ39okBmk/M6nh9InYk/oih20GNUyVOs9/oGybYXl1akIncLrLPBKgwzCBf0ylS2IEY5 KSqmrZZAM3r2T5g9VSRot/Lk25CnTnEohvjxSx5KpOezmE4scYZRa9OE9eqNypOSJZE3 adJMCHS3AQbJh/FK5u/VKCZLoObOHf0/xJuwLyll8RQJO8+KyypZT1teo8Wg89Zcf9Im zi4g== X-Gm-Message-State: APjAAAXbEJSIHlcUH+0ZXOkimO+6f2QOHjwPwjfCeePbc1wha343z3+O r3OPWSFJVVq2ZDbv0/HC+5uLOYDo X-Google-Smtp-Source: APXvYqxdlLQBj0ld6fOMwZF8OTmvQbHK56CR1iJjd3e1/AJ1aA1PBEt7baEkxKTI5QOoUMQXr/UdHw== X-Received: by 2002:a37:a94a:: with SMTP id s71mr1183376qke.323.1573847104438; Fri, 15 Nov 2019 11:45:04 -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.45.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Nov 2019 11:45:03 -0800 (PST) From: Nick Rosbrook To: xen-devel@lists.xenproject.org Date: Fri, 15 Nov 2019 14:44:12 -0500 Message-Id: <4c635331e4a5171e99388391b536d5961ea2fd96.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 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 }