From nobody Tue Nov 11 07:31:04 2025 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1570461287; cv=none; d=zoho.com; s=zohoarc; b=kCmsdlpwo8mpqeX6PjLRTE8vkWqM5pkE1AeU1ntCHyiYFXO4Uv4jeQPbMsESHNL5vubwbB3NVtzFq5U4LHuUt7GPkqaz+j+4cZMLA/dmF/GBmc/BvUqtnHO3MM31sExQuQYDILFujZs45FohgDUVpsZth8e7Hq4ibdDkqcvDdh4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1570461287; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=dTwurc8w9G221fznw1V1kMWl2FTjZVNs/ESV+HADhno=; b=cr4TKQw/DokfaZfUHrfyNblc2cAVZn+8rIMHfND6tFLgz+IWABFL73WlFWf6fxKaDZjcdq4fsVNAXxZrXP+X4FwLXw3H7Rg3INIzUjXZbhg6iVsUI/qPvhmWyUNnZrXwcyvKkBsX0ZE4MSR2a+r05936KAJX2xjB7o5N+RiNB08= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1570461287093999.128005749376; Mon, 7 Oct 2019 08:14:47 -0700 (PDT) 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 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) 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) X-Inumbo-ID: 00e8ecea-e915-11e9-8c93-bc764e2007e4 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) 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) 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 functio= n. 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/xenl= ight.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 } =20 -// 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 } =20 +func (bm *Bitmap) fromC(cbm *C.libxl_bitmap) error { + // Alloc a Go slice for the bytes + size :=3D int(cbm.size) + bm.bitmap =3D make([]C.uint8_t, size) + + // Make a slice pointing to the C array + mapslice :=3D (*[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 :=3D len(bm.bitmap) + cbm.size =3D C.uint32_t(size) + cbm._map =3D (*C.uint8_t)(C.malloc(C.ulong(cbm.size) * C.sizeof_uint8_t)) + cslice :=3D (*[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 =3D C.GoString(cci.pool_name) gci.Scheduler =3D Scheduler(cci.sched) gci.DomainCount =3D int(cci.n_dom) - gci.Cpumap =3D cci.cpumap.toGo() + gci.Cpumap.fromC(&cci.cpumap) =20 return } @@ -521,7 +550,10 @@ func (Ctx *Context) CpupoolCreate(Name string, Schedul= er Scheduler, Cpumap Bitma var uuid C.libxl_uuid C.libxl_uuid_generate(&uuid) =20 - cbm :=3D Cpumap.toC() + cbm, err :=3D Cpumap.toC() + if err !=3D nil { + return + } defer C.libxl_bitmap_dispose(&cbm) =20 ret :=3D C.libxl_cpupool_create(Ctx.ctx, name, C.libxl_scheduler(Schedule= r), @@ -576,7 +608,10 @@ func (Ctx *Context) CpupoolCpuaddCpumap(Poolid uint32,= Cpumap Bitmap) (err error return } =20 - cbm :=3D Cpumap.toC() + cbm, err :=3D Cpumap.toC() + if err !=3D nil { + return + } defer C.libxl_bitmap_dispose(&cbm) =20 ret :=3D C.libxl_cpupool_cpuadd_cpumap(Ctx.ctx, C.uint32_t(Poolid), &cbm) @@ -612,7 +647,10 @@ func (Ctx *Context) CpupoolCpuremoveCpumap(Poolid uint= 32, Cpumap Bitmap) (err er return } =20 - cbm :=3D Cpumap.toC() + cbm, err :=3D Cpumap.toC() + if err !=3D nil { + return + } defer C.libxl_bitmap_dispose(&cbm) =20 ret :=3D C.libxl_cpupool_cpuremove_cpumap(Ctx.ctx, C.uint32_t(Poolid), &c= bm) @@ -735,41 +773,6 @@ func (Ctx *Context) CpupoolMakeFree(Cpumap Bitmap) (er= r error) { * Bitmap operations */ =20 -// 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 :=3D int(cbm.size) - gbm.bitmap =3D make([]C.uint8_t, size) - - // Make a slice pointing to the C array - mapslice :=3D (*[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 :=3D len(gbm.bitmap) - cbm._map =3D (*C.uint8_t)(C.malloc(C.size_t(size))) - cbm.size =3D C.uint32_t(size) - if cbm._map =3D=3D nil { - panic("C.calloc failed!") - } - - // Make a slice pointing to the C array - mapslice :=3D (*[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 :=3D uint(bit) if bit > bm.Max() || bm.bitmap =3D=3D nil { @@ -1158,8 +1161,8 @@ func (cvci C.libxl_vcpuinfo) toGo() (gvci Vcpuinfo) { gvci.Blocked =3D bool(cvci.blocked) gvci.Running =3D bool(cvci.running) gvci.VCpuTime =3D time.Duration(cvci.vcpu_time) - gvci.Cpumap =3D cvci.cpumap.toGo() - gvci.CpumapSoft =3D cvci.cpumap_soft.toGo() + gvci.Cpumap.fromC(&cvci.cpumap) + gvci.CpumapSoft.fromC(&cvci.cpumap_soft) =20 return } --=20 2.19.1 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel