[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [Xen-devel] [PATCH RFC 6/8] golang/xenlight: Implement libxl_bitmap and helper operations
On 23/01/17 16:43, Ronald Rojas wrote: > Implement Bitmap type, along with helper functions. > > The Bitmap type is implemented interllay in a way which makes it > easy to copy into and out of the C libxl_bitmap type. > > Signed-off-by: George Dunlap <george.dunlap@xxxxxxxxxx> > Signed-off-by: Ronald Rojas <ronladred@xxxxxxxxx> > --- > tools/golang/xenlight/xenlight.go | 166 > ++++++++++++++++++++++++++++++++++++++ > 1 file changed, 166 insertions(+) > > diff --git a/tools/golang/xenlight/xenlight.go > b/tools/golang/xenlight/xenlight.go > index dd6893c..54692fd 100644 > --- a/tools/golang/xenlight/xenlight.go > +++ b/tools/golang/xenlight/xenlight.go > @@ -131,6 +131,19 @@ func (chwcap C.libxl_hwcap)CToGo() (ghwcap Hwcap) { > return > } > > +// typedef struct { > +// uint32_t size; /* number of bytes in map */ > +// uint8_t *map; > +// } 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 { > + bitmap []C.uint8_t > +} > > /* > * Types: IDL > @@ -199,6 +212,159 @@ type Dominfo struct { > } > > /* > + * Bitmap operations > + */ > + > +// Return a Go bitmap which is a copy of the referred C bitmap. > +func (cbm C.libxl_bitmap) CToGo() (gbm Bitmap) { Probably just toGo()... > + // 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)GoToC() (cbm C.libxl_bitmap) { ...and toC (lower case so they're not visible outside the package). Also 'go fmt'. > + 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!") malloc (This is probably my typo). Other than that, looks good. -George _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxx https://lists.xen.org/xen-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |