]> xenbits.xensource.com Git - people/liuw/xen.git/commitdiff
golang/xenlight: define StringList builtin type
authorNick Rosbrook <rosbrookn@ainfosec.com>
Mon, 16 Dec 2019 18:08:02 +0000 (18:08 +0000)
committerGeorge Dunlap <george.dunlap@citrix.com>
Mon, 16 Dec 2019 18:08:02 +0000 (18:08 +0000)
Define StringList as []string an implement fromC and toC functions.

Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
Reviewed-by: George Dunlap <george.dunlap@citrix.com>
tools/golang/xenlight/xenlight.go

index 1c5e3c0cc792657ea77f9a00a60199b0d91de047..72afc3cf145e9bf393ac29129656af19b8aca50d 100644 (file)
@@ -212,6 +212,35 @@ 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 }
 
+// StringList represents a libxl_string_list.
+type StringList []string
+
+func (sl *StringList) fromC(csl *C.libxl_string_list) error {
+       size := int(C.libxl_string_list_length(csl))
+       list := (*[1 << 30]*C.char)(unsafe.Pointer(csl))[:size:size]
+
+       *sl = make([]string, size)
+
+       for i, v := range list {
+               (*sl)[i] = C.GoString(v)
+       }
+
+       return nil
+}
+
+func (sl StringList) toC() (C.libxl_string_list, error) {
+       var char *C.char
+       size := len(sl)
+       csl := (C.libxl_string_list)(C.malloc(C.ulong(size) * C.ulong(unsafe.Sizeof(char))))
+       clist := (*[1 << 30]*C.char)(unsafe.Pointer(csl))[:size:size]
+
+       for i, v := range sl {
+               clist[i] = C.CString(v)
+       }
+
+       return csl, nil
+}
+
 // Bitmap represents a libxl_bitmap.
 //
 // Implement the Go bitmap type such that the underlying data can