[Xen-devel] [PATCH v3 12/22] golang/xenlight: re-factor Hwcap type implementation

Nick Rosbrook posted 22 patches 6 years, 2 months ago
There is a newer version of this series
[Xen-devel] [PATCH v3 12/22] golang/xenlight: re-factor Hwcap type implementation
Posted by Nick Rosbrook 6 years, 2 months ago
From: Nick Rosbrook <rosbrookn@ainfosec.com>

Re-define Hwcap as [8]uint32, and implement toC function. Also, re-name and
modify signature of toGo function to fromC.

Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
Reviewed-by: George Dunlap <george.dunlap@citrix.com>
---
Changes in v2:
- Fix comment in fromC since an array is being used now, not a slice.
- Use a concise variable name instead of mapslice for the C array.
Changes in v3:
- In fromC, iterate over the indirect of hwcap instead of creating
  a slice from the C type.
---
 tools/golang/xenlight/xenlight.go | 27 ++++++++++++++++-----------
 1 file changed, 16 insertions(+), 11 deletions(-)

diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
index f9c2f84c81..b395963512 100644
--- a/tools/golang/xenlight/xenlight.go
+++ b/tools/golang/xenlight/xenlight.go
@@ -306,20 +306,25 @@ type Context struct {
 	logger *C.xentoollog_logger_stdiostream
 }
 
-type Hwcap []C.uint32_t
+// Hwcap represents a libxl_hwcap.
+type Hwcap [8]uint32
 
-func (chwcap C.libxl_hwcap) toGo() (ghwcap Hwcap) {
-	// Alloc a Go slice for the bytes
-	size := 8
-	ghwcap = make([]C.uint32_t, size)
+func (hwcap *Hwcap) fromC(chwcap *C.libxl_hwcap) error {
+	for i := range *hwcap {
+		hwcap[i] = uint32(chwcap[i])
+	}
 
-	// Make a slice pointing to the C array
-	mapslice := (*[1 << 30]C.uint32_t)(unsafe.Pointer(&chwcap[0]))[:size:size]
+	return nil
+}
 
-	// And copy the C array into the Go array
-	copy(ghwcap, mapslice)
+func (hwcap *Hwcap) toC() (C.libxl_hwcap, error) {
+	var chwcap C.libxl_hwcap
 
-	return
+	for i, v := range hwcap {
+		chwcap[i] = C.uint32_t(v)
+	}
+
+	return chwcap, nil
 }
 
 // KeyValueList represents a libxl_key_value_list.
@@ -442,7 +447,7 @@ func (cphys *C.libxl_physinfo) toGo() (physinfo *Physinfo) {
 	physinfo.SharingFreedPages = uint64(cphys.sharing_freed_pages)
 	physinfo.SharingUsedFrames = uint64(cphys.sharing_used_frames)
 	physinfo.NrNodes = uint32(cphys.nr_nodes)
-	physinfo.HwCap = cphys.hw_cap.toGo()
+	physinfo.HwCap.fromC(&cphys.hw_cap)
 	physinfo.CapHvm = bool(cphys.cap_hvm)
 	physinfo.CapHvmDirectio = bool(cphys.cap_hvm_directio)
 
-- 
2.19.1


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