All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nick Rosbrook <rosbrookn@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: Nick Rosbrook <rosbrookn@ainfosec.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>,
	kerriganb@ainfosec.com, George Dunlap <george.dunlap@citrix.com>,
	Wei Liu <wl@xen.org>
Subject: [Xen-devel] [PATCH v3 05/22] golang/xenlight: re-name Bitmap marshaling functions
Date: Tue, 10 Dec 2019 10:47:20 -0500	[thread overview]
Message-ID: <688067dbf85cc8f2487165af21379039dcc30e9a.1575990937.git.rosbrookn@ainfosec.com> (raw)
In-Reply-To: <cover.1575990937.git.rosbrookn@ainfosec.com>

From: Nick Rosbrook <rosbrookn@ainfosec.com>

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 <rosbrookn@ainfosec.com>
Acked-by: George Dunlap <george.dunlap@citrix.com>
--
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
 }
-- 
2.19.1


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2019-12-10 15:48 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-10 15:47 [Xen-devel] [PATCH v3 00/22] generated Go libxl bindings using IDL Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 01/22] golang/xenlight: generate enum types from IDL Nick Rosbrook
2019-12-16 17:07   ` George Dunlap
2019-12-16 17:50     ` Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 02/22] golang/xenlight: define Defbool builtin type Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 03/22] golang/xenlight: define Devid type as int Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 04/22] golang/xenlight: define KeyValueList as empty struct Nick Rosbrook
2019-12-10 15:47 ` Nick Rosbrook [this message]
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 06/22] golang/xenlight: define StringList builtin type Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 07/22] golang/xenlight: define Mac " Nick Rosbrook
2019-12-16 17:15   ` George Dunlap
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 08/22] golang/xenlight: define MsVmGenid " Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 09/22] golang/xenlight: define EvLink builtin as empty struct Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 10/22] golang/xenlight: define CpuidPolicyList builtin type Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 11/22] golang/xenlight: re-factor Uuid type implementation Nick Rosbrook
2019-12-16 18:06   ` George Dunlap
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 12/22] golang/xenlight: re-factor Hwcap " Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 13/22] golang/xenlight: generate structs from the IDL Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 14/22] golang/xenlight: remove no-longer used type MemKB Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 15/22] golang/xenlight: begin C to Go type marshaling Nick Rosbrook
2019-12-16 17:56   ` George Dunlap
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 16/22] golang/xenlight: implement keyed union C to Go marshaling Nick Rosbrook
2019-12-16 17:57   ` George Dunlap
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 17/22] golang/xenlight: implement array " Nick Rosbrook
2019-12-17 11:16   ` George Dunlap
2019-12-17 14:51     ` Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 18/22] golang/xenlight: begin Go to C type marshaling Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 19/22] golang/xenlight: implement keyed union Go to C marshaling Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 20/22] golang/xenlight: implement array " Nick Rosbrook
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 21/22] golang/xenlight: revise use of Context type Nick Rosbrook
2019-12-17 17:28   ` George Dunlap
2019-12-10 15:47 ` [Xen-devel] [PATCH v3 22/22] golang/xenlight: add error return type to Context.Cpupoolinfo Nick Rosbrook
2019-12-17 17:29   ` George Dunlap

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=688067dbf85cc8f2487165af21379039dcc30e9a.1575990937.git.rosbrookn@ainfosec.com \
    --to=rosbrookn@gmail.com \
    --cc=george.dunlap@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=kerriganb@ainfosec.com \
    --cc=rosbrookn@ainfosec.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.