From patchwork Mon Oct 7 15:12:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Rosbrook X-Patchwork-Id: 11177783 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 361E713BD for ; Mon, 7 Oct 2019 15:14:59 +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 122E121479 for ; Mon, 7 Oct 2019 15:14: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="up8uzz54" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 122E121479 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 1iHUho-0006qW-E5; Mon, 07 Oct 2019 15:13:48 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iHUhm-0006pw-O6 for xen-devel@lists.xenproject.org; Mon, 07 Oct 2019 15:13:46 +0000 X-Inumbo-ID: 00e8ecea-e915-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 00e8ecea-e915-11e9-8c93-bc764e2007e4; Mon, 07 Oct 2019 15:13:23 +0000 (UTC) Received: by mail-qk1-x744.google.com with SMTP id 201so12866887qkd.13 for ; Mon, 07 Oct 2019 08:13:23 -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=BUbnKDo081lVXKsaKIYs70qz0PceipUWvFB9s5JEki4=; b=up8uzz54gZghBEz0/x5N783jpiTahuGM73Q7GL0o93GgyN7alt3vOvaAnH+UPCixMW cYDqoqjbY52vWXJFzlCb+S6paHlWVgNbtLo2ESIcS+IZZZe6IzssGsqJIMnV14z+wBQw huuX0oebf1srvLhfBfaHBnNtqWA1Vi4renjKZGVMzTIvrTWUlBRdLfmqG3QfXrPiFXTS cSYif42YI3ZSJYZn/tn/r/eo5drhiXeaOFfBd7545VO6kSSwbx1+GNqVXjtdmkYOmnz+ kYud6wp8IJwuGk9JMI4zNuATS04jRwKmvEmYpTDM2PThcS3XUcby3ERaBGmfeVAlmSep xdlw== 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=BUbnKDo081lVXKsaKIYs70qz0PceipUWvFB9s5JEki4=; b=d+8GOPC438L5U46JiYfKrYWdjGrv8MTLPHpr07QiVGf4Js3K7tucjnQFW9q1u+Fah3 ZYIE2RjCOGFbst6aPf8WCBMZc4WYEmQSYZwG4ci85u1rjKDdKoBtvL428uNEJYRhpBAN 8jo2qULeznkeIBpyp5akZmIShDHrHij+WtYNIjMrvyrjQtxC6hwxWS2yM3wUA50QT1pk hfaLPzp1t692E6C83DX9vRjED5lQz23H4EGbQskx21JQ4QFU9C4WJk5im89XyFaE5dDv ix+4ZYBHncb3vxfpC1TuRt2ePxYNK6KBY6RGyOtxCml08XDvGJQuLDaVJedA3Bpo7qYH MPPQ== X-Gm-Message-State: APjAAAVggubFAEBUMRdlJTt5P3dxNbba9jo9ytGBBz5HKYOqiY30h6TE OYyE9xQUba6ye8vMPftgMCqqwK86 X-Google-Smtp-Source: APXvYqyyG4OZmelmI1gpqzphQZNWz8m1Z2JtWS4FB0CwjNng28nBtkCOKUiKQkPuHdSEM4029naHWQ== X-Received: by 2002:a37:a755:: with SMTP id q82mr1962749qke.113.1570461202375; Mon, 07 Oct 2019 08:13:22 -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.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2019 08:13:21 -0700 (PDT) From: Nick Rosbrook To: xen-devel@lists.xenproject.org Date: Mon, 7 Oct 2019 11:12:53 -0400 Message-Id: X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 06/24] 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 --- Cc: George Dunlap Cc: Ian Jackson Cc: Wei Liu tools/golang/xenlight/xenlight.go | 93 ++++++++++++++++--------------- 1 file changed, 48 insertions(+), 45 deletions(-) diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go index 8196a42855..09fcdca5d1 100644 --- a/tools/golang/xenlight/xenlight.go +++ b/tools/golang/xenlight/xenlight.go @@ -234,19 +234,48 @@ func (kvl KeyValueList) toC() (C.libxl_key_value_list, error) { return ckvl, nil } -// 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 + mapslice := (*[1 << 30]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size] + + // And copy the C array into the Go array + copy(bm.bitmap, mapslice) + + 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)) + cslice := (*[1 << 31]C.uint8_t)(unsafe.Pointer(cbm._map))[:size:size] + + copy(cslice, bm.bitmap) + + return cbm, nil +} + /* * Types: IDL * @@ -447,7 +476,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 } @@ -521,7 +550,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), @@ -576,7 +608,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) @@ -612,7 +647,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) @@ -735,41 +773,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 { @@ -1158,8 +1161,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 }