[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH 23 of 23] libxl: autogenerate libxl_FOO_init and libxl_FOO_init_FIELD
# HG changeset patch # User Ian Campbell <ian.campbell@xxxxxxxxxx> # Date 1329150454 0 # Node ID 50c87c40031ca91265812881b18cecae7cdde138 # Parent ba485f9fce317ba31a96b407f5712ba62239e223 libxl: autogenerate libxl_FOO_init and libxl_FOO_init_FIELD Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx> diff -r ba485f9fce31 -r 50c87c40031c tools/libxl/gentypes.py --- a/tools/libxl/gentypes.py Mon Feb 13 16:27:34 2012 +0000 +++ b/tools/libxl/gentypes.py Mon Feb 13 16:27:34 2012 +0000 @@ -78,6 +78,88 @@ def libxl_C_type_dispose(ty, v, indent = s = indent + s return s.replace("\n", "\n%s" % indent).rstrip(indent) +def libxl_init_members(ty, nesting = 0): + """Returns a list of members of ty which require a separate init""" + + if isinstance(ty, idl.Aggregate): + return [f for f in ty.fields if not f.const and isinstance(f.type,idl.KeyedUnion)] + else: + return [] + +def _libxl_C_type_init(ty, v, indent = " ", parent = None, subinit=False): + s = "" + if isinstance(ty, idl.KeyedUnion): + if parent is None: + raise Exception("KeyedUnion type must have a parent") + if subinit: + s += "switch (%s) {\n" % (parent + ty.keyvar.name) + for f in ty.fields: + (nparent,fexpr) = ty.member(v, f, parent is None) + s += "case %s:\n" % f.enumname + s += _libxl_C_type_init(f.type, fexpr, " ", nparent) + s += " break;\n" + s += "}\n" + else: + if ty.keyvar.init_val: + s += "%s = %s;\n" % (parent + ty.keyvar.name, ty.keyvar.init_val) + elif ty.keyvar.type.init_val: + s += "%s = %s;\n" % (parent + ty.keyvar.name, ty.keyvar.type.init_val) + elif isinstance(ty, idl.Struct) and (parent is None or ty.init_fn is None): + for f in [f for f in ty.fields if not f.const]: + (nparent,fexpr) = ty.member(v, f, parent is None) + if f.init_val is not None: + s += "%s = %s;\n" % (fexpr, f.init_val) + else: + s += _libxl_C_type_init(f.type, fexpr, "", nparent) + else: + if ty.init_val is not None: + s += "%s = %s;\n" % (ty.pass_arg(v, parent is None), ty.init_val) + elif ty.init_fn is not None: + s += "%s(%s);\n" % (ty.init_fn, ty.pass_arg(v, parent is None)) + + if s != "": + s = indent + s + return s.replace("\n", "\n%s" % indent).rstrip(indent) + +def libxl_C_type_init(ty): + s = "" + s += "void %s(%s)\n" % (ty.init_fn, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE)) + s += "{\n" + s += " memset(p, '\\0', sizeof(*p));\n" + s += _libxl_C_type_init(ty, "p") + s += "}\n" + s += "\n" + return s + +def libxl_C_type_member_init(ty, field): + if not isinstance(field.type, idl.KeyedUnion): + raise Exception("Only KeyedUnion is supported for member init") + + ku = field.type + + s = "" + s += "void %s(%s, %s)\n" % (ty.init_fn + "_" + ku.keyvar.name, + ty.make_arg("p", passby=idl.PASS_BY_REFERENCE), + ku.keyvar.type.make_arg(ku.keyvar.name)) + s += "{\n" + + if ku.keyvar.init_val: + init_val = ku.keyvar.init_val + elif ku.keyvar.type.init_val: + init_val = ku.keyvar.type.init_val + else: + init_val = None + + if init_val is not None: + (nparent,fexpr) = ty.member(ty.pass_arg("p"), ku.keyvar, isref=True) + s += " assert(%s == %s);\n" % (fexpr, init_val) + s += " %s = %s;\n" % (fexpr, ku.keyvar.name) + (nparent,fexpr) = ty.member(ty.pass_arg("p"), field, isref=True) + s += _libxl_C_type_init(ku, fexpr, parent=nparent, subinit=True) + s += "}\n" + s += "\n" + return s + def libxl_C_type_gen_json(ty, v, indent = " ", parent = None): s = "" if parent is None: @@ -199,6 +281,15 @@ if __name__ == '__main__': f.write(libxl_C_type_define(ty) + ";\n") if ty.dispose_fn is not None: f.write("void %s(%s);\n" % (ty.dispose_fn, ty.make_arg("p"))) + if ty.init_fn is not None: + f.write("void %s(%s);\n" % (ty.init_fn, ty.make_arg("p"))) + for field in libxl_init_members(ty): + if not isinstance(field.type, idl.KeyedUnion): + raise Exception("Only KeyedUnion is supported for member init") + ku = field.type + f.write("void %s(%s, %s);\n" % (ty.init_fn + "_" + ku.keyvar.name, + ty.make_arg("p"), + ku.keyvar.type.make_arg(ku.keyvar.name))) if ty.json_fn is not None: f.write("char *%s_to_json(libxl_ctx *ctx, %s);\n" % (ty.typename, ty.make_arg("p"))) if isinstance(ty, idl.Enumeration): @@ -227,7 +318,7 @@ if __name__ == '__main__': """ % (header_json_define, header_json_define, " ".join(sys.argv))) - for ty in [ty for ty in types+builtins if ty.json_fn is not None]: + for ty in [ty for ty in types if ty.json_fn is not None]: f.write("yajl_gen_status %s_gen_json(yajl_gen hand, %s);\n" % (ty.typename, ty.make_arg("p", passby=idl.PASS_BY_REFERENCE))) f.write("\n") @@ -264,6 +355,11 @@ if __name__ == '__main__': f.write(" memset(p, LIBXL_DTOR_POISON, sizeof(*p));\n") f.write("}\n") f.write("\n") + + for ty in [t for t in types if t.init_fn is not None and t.autogenerate_init_fn]: + f.write(libxl_C_type_init(ty)) + for field in libxl_init_members(ty): + f.write(libxl_C_type_member_init(ty, field)) for ty in [t for t in types if isinstance(t,idl.Enumeration)]: f.write("const char *%s_to_string(%s e)\n" % (ty.typename, ty.typename)) diff -r ba485f9fce31 -r 50c87c40031c tools/libxl/idl.py --- a/tools/libxl/idl.py Mon Feb 13 16:27:34 2012 +0000 +++ b/tools/libxl/idl.py Mon Feb 13 16:27:34 2012 +0000 @@ -51,6 +51,10 @@ class Type(object): self.autogenerate_dispose_fn = kwargs.setdefault('autogenerate_dispose_fn', True) + self.init_fn = kwargs.setdefault('init_fn', None) + self.init_val = kwargs.setdefault('init_val', None) + self.autogenerate_init_fn = kwargs.setdefault('autogenerate_init_fn', False) + if self.typename is not None and not self.private: self.json_fn = kwargs.setdefault('json_fn', self.typename + "_gen_json") else: @@ -144,12 +148,20 @@ class Field(object): self.name = name self.const = kwargs.setdefault('const', False) self.enumname = kwargs.setdefault('enumname', None) + self.init_val = kwargs.setdefault('init_val', None) class Aggregate(Type): """A type containing a collection of other types""" def __init__(self, kind, typename, fields, **kwargs): Type.__init__(self, typename, **kwargs) + if self.typename is not None and self.dir in [DIR_IN, DIR_BOTH]: + self.init_fn = kwargs.setdefault('init_fn', self.typename + "_init") + else: + self.init_fn = kwargs.setdefault('init_fn', None) + + self.autogenerate_init_fn = kwargs.setdefault('autogenerate_init_fn', True) + self.kind = kind self.fields = [] diff -r ba485f9fce31 -r 50c87c40031c tools/libxl/idl.txt --- a/tools/libxl/idl.txt Mon Feb 13 16:27:34 2012 +0000 +++ b/tools/libxl/idl.txt Mon Feb 13 16:27:34 2012 +0000 @@ -44,6 +44,22 @@ Type.autogenerate_dispose_fn: (default: Indicates if the above named Type.dispose_fn should be autogenerated. +Type.init_val: (default: None) + + C expression for the value to initialise instances of this type to. + + If present takes precendence over init_fn (see below). + +Type.init_fn: (default: typename + "_init" if dir in [IN, BOTH] and + type != None) + + The name of the C function which will initialist Type. + +Type.autogenerate_init_fn: (default: True if dir in [IN, BOTH]) + + Indicates if the above named Type.init_fn should be + autogenerated. + Type.json_fn: (default: typename + "_gen_json" or None if type == None) The name of the C function which will generate a YAJL data structure @@ -105,10 +121,13 @@ idl.Aggregate Each field has the following properties: - Field.type The type of the member (a idl.Type). - Field.name The name of the member (can be None for anonymous - fields). - Field.const Boolean, true if the member is const. + Field.type The type of the member (a idl.Type). + Field.name The name of the member (can be None for anonymous + fields). + Field.const Boolean, true if the member is const. + Field.init_val The initialisation value for this field. Takes + precendence over both Field.type.init_val and + Field.type.init_fn. idl.Struct diff -r ba485f9fce31 -r 50c87c40031c tools/libxl/libxl.c --- a/tools/libxl/libxl.c Mon Feb 13 16:27:34 2012 +0000 +++ b/tools/libxl/libxl.c Mon Feb 13 16:27:34 2012 +0000 @@ -1226,11 +1226,6 @@ int libxl_vncviewer_exec(libxl_ctx *ctx, /******************************************************************************/ -void libxl_device_disk_init(libxl_device_disk *disk) -{ - memset(disk, 0x00, sizeof(libxl_device_disk)); -} - int libxl__device_disk_setdefault(libxl__gc *gc, libxl_device_disk *disk) { int rc; @@ -1717,10 +1712,6 @@ int libxl_device_disk_local_detach(libxl } /******************************************************************************/ -void libxl_device_nic_init(libxl_device_nic *nic) -{ - memset(nic, '\0', sizeof(*nic)); -} int libxl__device_nic_setdefault(libxl__gc *gc, libxl_device_nic *nic) { @@ -2141,10 +2132,6 @@ out: } /******************************************************************************/ -void libxl_device_vkb_init(libxl_device_vkb *vkb) -{ - memset(vkb, 0x00, sizeof(libxl_device_vkb)); -} int libxl__device_vkb_setdefault(libxl__gc *gc, libxl_device_vkb *vkb) { @@ -2253,10 +2240,6 @@ out: } /******************************************************************************/ -void libxl_device_vfb_init(libxl_device_vfb *vfb) -{ - memset(vfb, 0x00, sizeof(libxl_device_vfb)); -} int libxl__device_vfb_setdefault(libxl__gc *gc, libxl_device_vfb *vfb) { diff -r ba485f9fce31 -r 50c87c40031c tools/libxl/libxl.h --- a/tools/libxl/libxl.h Mon Feb 13 16:27:34 2012 +0000 +++ b/tools/libxl/libxl.h Mon Feb 13 16:27:34 2012 +0000 @@ -388,10 +388,6 @@ int libxl_ctx_free(libxl_ctx *ctx /* 0 i int libxl_ctx_postfork(libxl_ctx *ctx); /* domain related functions */ -void libxl_domain_create_info_init(libxl_domain_create_info *c_info); -void libxl_domain_build_info_init(libxl_domain_build_info *b_info); -void libxl_domain_build_info_init_type(libxl_domain_build_info *b_info, - libxl_domain_type type); typedef int (*libxl_console_ready)(libxl_ctx *ctx, uint32_t domid, void *priv); int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config, libxl_console_ready cb, void *priv, uint32_t *domid); int libxl_domain_create_restore(libxl_ctx *ctx, libxl_domain_config *d_config, libxl_console_ready cb, void *priv, uint32_t *domid, int restore_fd); @@ -527,7 +523,6 @@ libxl_vminfo * libxl_list_vm(libxl_ctx * */ /* Disks */ -void libxl_device_disk_init(libxl_device_disk *disk); int libxl_device_disk_add(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk); int libxl_device_disk_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk, @@ -553,7 +548,6 @@ char * libxl_device_disk_local_attach(li int libxl_device_disk_local_detach(libxl_ctx *ctx, libxl_device_disk *disk); /* Network Interfaces */ -void libxl_device_nic_init(libxl_device_nic *nic); int libxl_device_nic_add(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic); int libxl_device_nic_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic, @@ -565,7 +559,6 @@ int libxl_device_nic_getinfo(libxl_ctx * libxl_device_nic *nic, libxl_nicinfo *nicinfo); /* Keyboard */ -void libxl_device_vkb_init(libxl_device_vkb *vkb); int libxl_device_vkb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb); int libxl_device_vkb_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb, @@ -573,7 +566,6 @@ int libxl_device_vkb_remove(libxl_ctx *c int libxl_device_vkb_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb); /* Framebuffer */ -void libxl_device_vfb_init(libxl_device_vfb *vfb); int libxl_device_vfb_add(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb); int libxl_device_vfb_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb, @@ -581,7 +573,6 @@ int libxl_device_vfb_remove(libxl_ctx *c int libxl_device_vfb_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb); /* PCI Passthrough */ -void libxl_device_pci_init(libxl_device_pci *pci); int libxl_device_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev); int libxl_device_pci_remove(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev); int libxl_device_pci_destroy(libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev); diff -r ba485f9fce31 -r 50c87c40031c tools/libxl/libxl_create.c --- a/tools/libxl/libxl_create.c Mon Feb 13 16:27:34 2012 +0000 +++ b/tools/libxl/libxl_create.c Mon Feb 13 16:27:34 2012 +0000 @@ -50,11 +50,6 @@ void libxl_domain_config_dispose(libxl_d libxl_domain_build_info_dispose(&d_config->b_info); } -void libxl_domain_create_info_init(libxl_domain_create_info *c_info) -{ - memset(c_info, '\0', sizeof(*c_info)); -} - int libxl__domain_create_info_setdefault(libxl__gc *gc, libxl_domain_create_info *c_info) { @@ -69,35 +64,6 @@ int libxl__domain_create_info_setdefault return 0; } -void libxl_domain_build_info_init(libxl_domain_build_info *b_info) -{ - memset(b_info, '\0', sizeof(*b_info)); - b_info->type = -1; - - b_info->max_memkb = LIBXL_MEMKB_DEFAULT; - b_info->target_memkb = LIBXL_MEMKB_DEFAULT; - b_info->shadow_memkb = LIBXL_MEMKB_DEFAULT; - b_info->video_memkb = LIBXL_MEMKB_DEFAULT; - -} - -void libxl_domain_build_info_init_type(libxl_domain_build_info *b_info, - libxl_domain_type type) -{ - assert(b_info->type == -1); - b_info->type = type; - switch (b_info->type) { - case LIBXL_DOMAIN_TYPE_HVM: - b_info->u.hvm.timer_mode = LIBXL_TIMER_MODE_DEFAULT; - break; - case LIBXL_DOMAIN_TYPE_PV: - b_info->u.pv.slack_memkb = LIBXL_MEMKB_DEFAULT; - break; - default: - abort(); - } -} - int libxl__domain_build_info_setdefault(libxl__gc *gc, libxl_domain_build_info *b_info) { diff -r ba485f9fce31 -r 50c87c40031c tools/libxl/libxl_json.h --- a/tools/libxl/libxl_json.h Mon Feb 13 16:27:34 2012 +0000 +++ b/tools/libxl/libxl_json.h Mon Feb 13 16:27:34 2012 +0000 @@ -22,6 +22,20 @@ # include <yajl/yajl_version.h> #endif +yajl_gen_status libxl_defbool_gen_json(yajl_gen hand, libxl_defbool *p); +yajl_gen_status libxl_domid_gen_json(yajl_gen hand, libxl_domid *p); +yajl_gen_status libxl_uuid_gen_json(yajl_gen hand, libxl_uuid *p); +yajl_gen_status libxl_mac_gen_json(yajl_gen hand, libxl_mac *p); +yajl_gen_status libxl_cpumap_gen_json(yajl_gen hand, libxl_cpumap *p); +yajl_gen_status libxl_cpuid_policy_list_gen_json(yajl_gen hand, + libxl_cpuid_policy_list *p); +yajl_gen_status libxl_string_list_gen_json(yajl_gen hand, libxl_string_list *p); +yajl_gen_status libxl_key_value_list_gen_json(yajl_gen hand, + libxl_key_value_list *p); +yajl_gen_status libxl_file_reference_gen_json(yajl_gen hand, + libxl_file_reference *p); +yajl_gen_status libxl_hwcap_gen_json(yajl_gen hand, libxl_hwcap *p); + #include <_libxl_types_json.h> /* YAJL version check */ diff -r ba485f9fce31 -r 50c87c40031c tools/libxl/libxl_pci.c --- a/tools/libxl/libxl_pci.c Mon Feb 13 16:27:34 2012 +0000 +++ b/tools/libxl/libxl_pci.c Mon Feb 13 16:27:34 2012 +0000 @@ -765,11 +765,6 @@ static int libxl__device_pci_reset(libxl return -1; } -void libxl_device_pci_init(libxl_device_pci *pci) -{ - memset(pci, '\0', sizeof(*pci)); -} - int libxl__device_pci_setdefault(libxl__gc *gc, libxl_device_pci *pci) { return 0; diff -r ba485f9fce31 -r 50c87c40031c tools/libxl/libxl_types.idl --- a/tools/libxl/libxl_types.idl Mon Feb 13 16:27:34 2012 +0000 +++ b/tools/libxl/libxl_types.idl Mon Feb 13 16:27:34 2012 +0000 @@ -100,7 +100,7 @@ libxl_timer_mode = Enumeration("timer_mo (1, "no_delay_for_missed_ticks"), (2, "no_missed_ticks_pending"), (3, "one_missed_tick_pending"), - ]) + ], init_val = "LIBXL_TIMER_MODE_DEFAULT") # # Complex libxl types @@ -157,19 +157,19 @@ libxl_dominfo = Struct("dominfo",[ ("vcpu_max_id", uint32), ("vcpu_online", uint32), ("cpupool", uint32), - ], dispose_fn=None) + ], dispose_fn=None, dir=DIR_OUT) libxl_cpupoolinfo = Struct("cpupoolinfo", [ ("poolid", uint32), ("sched_id", uint32), ("n_dom", uint32), ("cpumap", libxl_cpumap) - ]) + ], dir=DIR_OUT) libxl_vminfo = Struct("vminfo", [ ("uuid", libxl_uuid), ("domid", libxl_domid), - ], dispose_fn=None) + ], dispose_fn=None, dir=DIR_OUT) libxl_version_info = Struct("version_info", [ ("xen_version_major", integer), @@ -184,7 +184,7 @@ libxl_version_info = Struct("version_inf ("virt_start", uint64), ("pagesize", integer), ("commandline", string), - ]) + ], dir=DIR_OUT) libxl_domain_create_info = Struct("domain_create_info",[ ("type", libxl_domain_type), @@ -196,7 +196,9 @@ libxl_domain_create_info = Struct("domai ("xsdata", libxl_key_value_list), ("platformdata", libxl_key_value_list), ("poolid", uint32), - ]) + ], dir=DIR_IN) + +MemKB = UInt(64, init_val = "LIBXL_MEMKB_DEFAULT") # Instances of libxl_file_reference contained in this struct which # have been mapped (with libxl_file_reference_map) will be unmapped @@ -273,8 +275,8 @@ libxl_domain_build_info = Struct("domain # Use host's E820 for PCI passthrough. ("e820_host", libxl_defbool), ])), - ])), - ], + ], keyvar_init_val = "-1")), + ], dir=DIR_IN ) libxl_device_vfb = Struct("device_vfb", [ @@ -336,7 +338,7 @@ libxl_diskinfo = Struct("diskinfo", [ ("state", integer), ("evtch", integer), ("rref", integer), - ]) + ], dir=DIR_OUT) libxl_nicinfo = Struct("nicinfo", [ ("backend", string), @@ -348,7 +350,7 @@ libxl_nicinfo = Struct("nicinfo", [ ("evtch", integer), ("rref_tx", integer), ("rref_rx", integer), - ]) + ], dir=DIR_OUT) libxl_vcpuinfo = Struct("vcpuinfo", [ ("vcpuid", uint32), @@ -358,7 +360,7 @@ libxl_vcpuinfo = Struct("vcpuinfo", [ ("running", bool), ("vcpu_time", uint64), # total vcpu time ran (ns) ("cpumap", libxl_cpumap), # current cpu's affinities - ]) + ], dir=DIR_OUT) libxl_physinfo = Struct("physinfo", [ ("threads_per_core", uint32), @@ -383,16 +385,16 @@ libxl_cputopology = Struct("cputopology" ("core", uint32), ("socket", uint32), ("node", uint32), - ]) + ], dir=DIR_OUT) libxl_sched_credit = Struct("sched_credit", [ ("weight", integer), ("cap", integer), - ], dispose_fn=None) + ], dispose_fn=None, init_fn=None) libxl_sched_credit2 = Struct("sched_credit2", [ ("weight", integer), - ], dispose_fn=None) + ], dispose_fn=None, init_fn=None) libxl_sched_sedf = Struct("sched_sedf", [ ("period", integer), @@ -400,7 +402,7 @@ libxl_sched_sedf = Struct("sched_sedf", ("latency", integer), ("extratime", integer), ("weight", integer), - ], dispose_fn=None) + ], dispose_fn=None, init_fn=None) libxl_event_type = Enumeration("event_type", [ (1, "DOMAIN_SHUTDOWN"), _______________________________________________ Xen-devel mailing list Xen-devel@xxxxxxxxxxxxxxxxxxx http://lists.xensource.com/xen-devel
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |