[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 <george.dunlap@xxxxxxxxxx> wrote:
>
> On 12/10/19 3:47 PM, Nick Rosbrook wrote:
> > From: Nick Rosbrook <rosbrookn@xxxxxxxxxxxx>
> >
> > Signed-off-by: Nick Rosbrook <rosbrookn@xxxxxxxxxxxx>
> > ---
> >  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(&v); 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(&v); 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(&v); err != nil {
>         return err
>     }

Nope, no problem with that.

Thanks,
-NR

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

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.