Message ID | 3bf4e3167d703d680a6bd6f3226ba394b0ed443f.1573840474.git.rosbrookn@ainfosec.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | generated Go libxl bindings using IDL | expand |
On 11/15/19 7:44 PM, Nick Rosbrook wrote: > 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. > > tools/golang/xenlight/xenlight.go | 31 ++++++++++++++++++++----------- > 1 file changed, 20 insertions(+), 11 deletions(-) > > diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go > index 67c1bb1225..d6d912a037 100644 > --- a/tools/golang/xenlight/xenlight.go > +++ b/tools/golang/xenlight/xenlight.go > @@ -312,20 +312,29 @@ 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) > - > - // Make a slice pointing to the C array > - mapslice := (*[1 << 30]C.uint32_t)(unsafe.Pointer(&chwcap[0]))[:size:size] > +func (hwcap *Hwcap) fromC(chwcap *C.libxl_hwcap) error { > + // Make an array pointing to the C array > + a := (*[8]C.uint32_t)(unsafe.Pointer(chwcap)) Same thing with casting. -George
> Same thing with casting.
Ack for all such cases of this casting. That's a good way to simplify.
-NR
diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go index 67c1bb1225..d6d912a037 100644 --- a/tools/golang/xenlight/xenlight.go +++ b/tools/golang/xenlight/xenlight.go @@ -312,20 +312,29 @@ 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) - - // Make a slice pointing to the C array - mapslice := (*[1 << 30]C.uint32_t)(unsafe.Pointer(&chwcap[0]))[:size:size] +func (hwcap *Hwcap) fromC(chwcap *C.libxl_hwcap) error { + // Make an array pointing to the C array + a := (*[8]C.uint32_t)(unsafe.Pointer(chwcap)) // And copy the C array into the Go array - copy(ghwcap, mapslice) + for i, v := range a { + hwcap[i] = uint32(v) + } - return + return nil +} + +func (hwcap *Hwcap) toC() (C.libxl_hwcap, error) { + var chwcap C.libxl_hwcap + + for i, v := range hwcap { + chwcap[i] = C.uint32_t(v) + } + + return chwcap, nil } // KeyValueList represents a libxl_key_value_list. @@ -448,7 +457,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)