Re: [Xen-devel] [PATCH v3 17/22] golang/xenlight: implement array C to Go marshaling
On Tue, Dec 17, 2019 at 6:16 AM George Dunlap wrote: > > On 12/10/19 3:47 PM, Nick Rosbrook wrote: > > From: Nick Rosbrook > > > > Signed-off-by: Nick Rosbrook > > --- > > tools/golang/xenlight/gengotypes.py | 39 +++- > > tools/golang/xenlight/helpers.gen.go | 300 +++ > > 2 files changed, 338 insertions(+), 1 deletion(-) > > > > diff --git a/tools/golang/xenlight/gengotypes.py > > b/tools/golang/xenlight/gengotypes.py > > index b68c1aa66b..ee9aaf9eff 100644 > > --- a/tools/golang/xenlight/gengotypes.py > > +++ b/tools/golang/xenlight/gengotypes.py > > @@ -252,7 +252,7 @@ def xenlight_golang_define_from_C(ty = None): > > for f in ty.fields: > > if f.type.typename is not None: > > if isinstance(f.type, idl.Array): > > -# TODO > > +body += xenlight_golang_array_from_C(f) > > continue > > > > body += xenlight_golang_convert_from_C(f) > > @@ -399,6 +399,43 @@ def xenlight_golang_union_from_C(ty = None, union_name > > = '', struct_name = ''): > > > > return (s,extras) > > > > +def xenlight_golang_array_from_C(ty = None): > > +""" > > +Convert C array to Go slice using the method > > +described here: > > + > > +https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices > > +""" > > +s = '' > > + > > +gotypename = xenlight_golang_fmt_name(ty.type.elem_type.typename) > > +goname = xenlight_golang_fmt_name(ty.name) > > +ctypename = ty.type.elem_type.typename > > +cname = ty.name > > +cslice = 'c{}'.format(goname) > > +clenvar= ty.type.lenvar.name > > +golenvar = xenlight_golang_fmt_name(clenvar,exported=False) > > + > > +s += '{} := int(xc.{})\n'.format(golenvar, clenvar) > > +s += '{} := '.format(cslice) > > +s > > +='(*[1<<28]C.{})(unsafe.Pointer(xc.{}))[:{}:{}]\n'.format(ctypename, cname, > > +golenvar, > > golenvar) > > +s += 'x.{} = make([]{}, {})\n'.format(goname, gotypename, golenvar) > > +s += 'for i, v := range {} {{\n'.format(cslice) > > + > > +is_enum = isinstance(ty.type.elem_type,idl.Enumeration) > > +if gotypename in go_builtin_types or is_enum: > > +s += 'x.{}[i] = {}(v)\n'.format(goname, gotypename) > > +else: > > +s += 'var e {}\n'.format(gotypename) > > +s += 'if err := e.fromC(); err != nil {\n' > > +s += 'return err }\n' > > +s += 'x.{}[i] = e\n'.format(goname) > > + > > +s += '}\n' > > + > > +return s > > + > > def xenlight_golang_fmt_name(name, exported = True): > > """ > > Take a given type name and return an > > diff --git a/tools/golang/xenlight/helpers.gen.go > > b/tools/golang/xenlight/helpers.gen.go > > index e6eee234c0..2f917cac58 100644 > > --- a/tools/golang/xenlight/helpers.gen.go > > +++ b/tools/golang/xenlight/helpers.gen.go > > @@ -263,6 +263,16 @@ func (x *SchedParams) fromC(xc *C.libxl_sched_params) > > error { > > > > func (x *VcpuSchedParams) fromC(xc *C.libxl_vcpu_sched_params) error { > > x.Sched = Scheduler(xc.sched) > > + numVcpus := int(xc.num_vcpus) > > + cVcpus := (*[1 << > > 28]C.libxl_sched_params)(unsafe.Pointer(xc.vcpus))[:numVcpus:numVcpus] > > + x.Vcpus = make([]SchedParams, numVcpus) > > + for i, v := range cVcpus { > > + var e SchedParams > > + if err := e.fromC(); err != nil { > > + return err > > + } > > + x.Vcpus[i] = e > > Along the same lines, any reason not to do the following? > > if err := x.Vcpus[i].fromC(); err != nil { > return err > } Nope, no problem with that. Thanks, -NR ___ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [PATCH v3 17/22] golang/xenlight: implement array C to Go marshaling
On 12/10/19 3:47 PM, Nick Rosbrook wrote: > From: Nick Rosbrook > > Signed-off-by: Nick Rosbrook > --- > tools/golang/xenlight/gengotypes.py | 39 +++- > tools/golang/xenlight/helpers.gen.go | 300 +++ > 2 files changed, 338 insertions(+), 1 deletion(-) > > diff --git a/tools/golang/xenlight/gengotypes.py > b/tools/golang/xenlight/gengotypes.py > index b68c1aa66b..ee9aaf9eff 100644 > --- a/tools/golang/xenlight/gengotypes.py > +++ b/tools/golang/xenlight/gengotypes.py > @@ -252,7 +252,7 @@ def xenlight_golang_define_from_C(ty = None): > for f in ty.fields: > if f.type.typename is not None: > if isinstance(f.type, idl.Array): > -# TODO > +body += xenlight_golang_array_from_C(f) > continue > > body += xenlight_golang_convert_from_C(f) > @@ -399,6 +399,43 @@ def xenlight_golang_union_from_C(ty = None, union_name = > '', struct_name = ''): > > return (s,extras) > > +def xenlight_golang_array_from_C(ty = None): > +""" > +Convert C array to Go slice using the method > +described here: > + > +https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices > +""" > +s = '' > + > +gotypename = xenlight_golang_fmt_name(ty.type.elem_type.typename) > +goname = xenlight_golang_fmt_name(ty.name) > +ctypename = ty.type.elem_type.typename > +cname = ty.name > +cslice = 'c{}'.format(goname) > +clenvar= ty.type.lenvar.name > +golenvar = xenlight_golang_fmt_name(clenvar,exported=False) > + > +s += '{} := int(xc.{})\n'.format(golenvar, clenvar) > +s += '{} := '.format(cslice) > +s +='(*[1<<28]C.{})(unsafe.Pointer(xc.{}))[:{}:{}]\n'.format(ctypename, > cname, > +golenvar, > golenvar) > +s += 'x.{} = make([]{}, {})\n'.format(goname, gotypename, golenvar) > +s += 'for i, v := range {} {{\n'.format(cslice) > + > +is_enum = isinstance(ty.type.elem_type,idl.Enumeration) > +if gotypename in go_builtin_types or is_enum: > +s += 'x.{}[i] = {}(v)\n'.format(goname, gotypename) > +else: > +s += 'var e {}\n'.format(gotypename) > +s += 'if err := e.fromC(); err != nil {\n' > +s += 'return err }\n' > +s += 'x.{}[i] = e\n'.format(goname) > + > +s += '}\n' > + > +return s > + > def xenlight_golang_fmt_name(name, exported = True): > """ > Take a given type name and return an > diff --git a/tools/golang/xenlight/helpers.gen.go > b/tools/golang/xenlight/helpers.gen.go > index e6eee234c0..2f917cac58 100644 > --- a/tools/golang/xenlight/helpers.gen.go > +++ b/tools/golang/xenlight/helpers.gen.go > @@ -263,6 +263,16 @@ func (x *SchedParams) fromC(xc *C.libxl_sched_params) > error { > > func (x *VcpuSchedParams) fromC(xc *C.libxl_vcpu_sched_params) error { > x.Sched = Scheduler(xc.sched) > + numVcpus := int(xc.num_vcpus) > + cVcpus := (*[1 << > 28]C.libxl_sched_params)(unsafe.Pointer(xc.vcpus))[:numVcpus:numVcpus] > + x.Vcpus = make([]SchedParams, numVcpus) > + for i, v := range cVcpus { > + var e SchedParams > + if err := e.fromC(); err != nil { > + return err > + } > + x.Vcpus[i] = e Along the same lines, any reason not to do the following? if err := x.Vcpus[i].fromC(); err != nil { return err } Everything else looks good. ___ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel
[Xen-devel] [PATCH v3 17/22] golang/xenlight: implement array C to Go marshaling
From: Nick Rosbrook Signed-off-by: Nick Rosbrook --- tools/golang/xenlight/gengotypes.py | 39 +++- tools/golang/xenlight/helpers.gen.go | 300 +++ 2 files changed, 338 insertions(+), 1 deletion(-) diff --git a/tools/golang/xenlight/gengotypes.py b/tools/golang/xenlight/gengotypes.py index b68c1aa66b..ee9aaf9eff 100644 --- a/tools/golang/xenlight/gengotypes.py +++ b/tools/golang/xenlight/gengotypes.py @@ -252,7 +252,7 @@ def xenlight_golang_define_from_C(ty = None): for f in ty.fields: if f.type.typename is not None: if isinstance(f.type, idl.Array): -# TODO +body += xenlight_golang_array_from_C(f) continue body += xenlight_golang_convert_from_C(f) @@ -399,6 +399,43 @@ def xenlight_golang_union_from_C(ty = None, union_name = '', struct_name = ''): return (s,extras) +def xenlight_golang_array_from_C(ty = None): +""" +Convert C array to Go slice using the method +described here: + +https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices +""" +s = '' + +gotypename = xenlight_golang_fmt_name(ty.type.elem_type.typename) +goname = xenlight_golang_fmt_name(ty.name) +ctypename = ty.type.elem_type.typename +cname = ty.name +cslice = 'c{}'.format(goname) +clenvar= ty.type.lenvar.name +golenvar = xenlight_golang_fmt_name(clenvar,exported=False) + +s += '{} := int(xc.{})\n'.format(golenvar, clenvar) +s += '{} := '.format(cslice) +s +='(*[1<<28]C.{})(unsafe.Pointer(xc.{}))[:{}:{}]\n'.format(ctypename, cname, +golenvar, golenvar) +s += 'x.{} = make([]{}, {})\n'.format(goname, gotypename, golenvar) +s += 'for i, v := range {} {{\n'.format(cslice) + +is_enum = isinstance(ty.type.elem_type,idl.Enumeration) +if gotypename in go_builtin_types or is_enum: +s += 'x.{}[i] = {}(v)\n'.format(goname, gotypename) +else: +s += 'var e {}\n'.format(gotypename) +s += 'if err := e.fromC(); err != nil {\n' +s += 'return err }\n' +s += 'x.{}[i] = e\n'.format(goname) + +s += '}\n' + +return s + def xenlight_golang_fmt_name(name, exported = True): """ Take a given type name and return an diff --git a/tools/golang/xenlight/helpers.gen.go b/tools/golang/xenlight/helpers.gen.go index e6eee234c0..2f917cac58 100644 --- a/tools/golang/xenlight/helpers.gen.go +++ b/tools/golang/xenlight/helpers.gen.go @@ -263,6 +263,16 @@ func (x *SchedParams) fromC(xc *C.libxl_sched_params) error { func (x *VcpuSchedParams) fromC(xc *C.libxl_vcpu_sched_params) error { x.Sched = Scheduler(xc.sched) + numVcpus := int(xc.num_vcpus) + cVcpus := (*[1 << 28]C.libxl_sched_params)(unsafe.Pointer(xc.vcpus))[:numVcpus:numVcpus] + x.Vcpus = make([]SchedParams, numVcpus) + for i, v := range cVcpus { + var e SchedParams + if err := e.fromC(); err != nil { + return err + } + x.Vcpus[i] = e + } return nil } @@ -282,6 +292,12 @@ func (x *DomainSchedParams) fromC(xc *C.libxl_domain_sched_params) error { func (x *VnodeInfo) fromC(xc *C.libxl_vnode_info) error { x.Memkb = uint64(xc.memkb) + numDistances := int(xc.num_distances) + cDistances := (*[1 << 28]C.uint32_t)(unsafe.Pointer(xc.distances))[:numDistances:numDistances] + x.Distances = make([]uint32, numDistances) + for i, v := range cDistances { + x.Distances[i] = uint32(v) + } x.Pnode = uint32(xc.pnode) if err := x.Vcpus.fromC(); err != nil { return err @@ -308,6 +324,26 @@ func (x *DomainBuildInfo) fromC(xc *C.libxl_domain_build_info) error { if err := x.Nodemap.fromC(); err != nil { return err } + numVcpuHardAffinity := int(xc.num_vcpu_hard_affinity) + cVcpuHardAffinity := (*[1 << 28]C.libxl_bitmap)(unsafe.Pointer(xc.vcpu_hard_affinity))[:numVcpuHardAffinity:numVcpuHardAffinity] + x.VcpuHardAffinity = make([]Bitmap, numVcpuHardAffinity) + for i, v := range cVcpuHardAffinity { + var e Bitmap + if err := e.fromC(); err != nil { + return err + } + x.VcpuHardAffinity[i] = e + } + numVcpuSoftAffinity := int(xc.num_vcpu_soft_affinity) + cVcpuSoftAffinity := (*[1 << 28]C.libxl_bitmap)(unsafe.Pointer(xc.vcpu_soft_affinity))[:numVcpuSoftAffinity:numVcpuSoftAffinity] + x.VcpuSoftAffinity = make([]Bitmap, numVcpuSoftAffinity) + for i, v := range cVcpuSoftAffinity { + var e Bitmap + if err := e.fromC(); err != nil { + return err + } + x.VcpuSoftAffinity[i] = e + } if err :=