libxl: autogenerate type definitions and destructor functions
PATCH 1/1 libxl: autogenerate type definitions and destructor functions
The series introduces auto-generation of the type definitions used in the libxl interface followed by auto-generation of a destructor function for each type. In the future it may be possible to use the related data structures for other purposes, for example auto-generation of the functions to marshal between C and language binding data types.
This series complements Gianni Tedesco's "Implement per-API-call garbage-collection lifetime" patch but doesn't yet tie the two together with a neat bow.
I have tested with xl create and xl destroy but since the only practical impact on the running code is in the final patch (which impacts create only) I think the probabilty of complete carnage are small (ish).
tools/_libxl_types.h should be identical both before applying and after applying+building "[PATCH 7 of 9] libxl: autogenerate _libxl_types.h".
Ian.
PATCH 1/1 xl: use the regular implicit rules to build the xl .o files
# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281706573 -3600
# Node ID d033ff85952a36f7c5022832dd8a6381802b87b8 # Parent 08b7ef5bb11ab1611e3a9c618b97a8e169b322a2
xl: use the regular implicit rules to build the xl .o files
Having $(XL_OBJS) makes part of a subsequent patch a little cleaner.
tools/libxl/Makefile +3 -10
@@ -25,6 +25,8 @@ LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_ LIBXLU_OBJS = libxlu_cfg_y.o libxlu_cfg_l.o libxlu_cfg.o CLIENTS = xl + +XL_OBJS = xl.o xl_cmdimpl.o xl_cmdtable.o .PHONY: all all: $(CLIENTS) libxenlight.so libxenlight.a libxlutil.so libxlutil.a \
@@ -76,16 +78,7 @@ libxlutil.a: $(LIBXLU_OBJS) libxlutil.a: $(LIBXLU_OBJS) $(AR) rcs libxlutil.a $^ -xl.o: xl.c - $(CC) $(CFLAGS) -c xl.c - -xl_cmdimpl.o: xl_cmdimpl.c - $(CC) $(CFLAGS) -c xl_cmdimpl.c - -xl_cmdtable.o: xl_cmdtable.c - $(CC) $(CFLAGS) -c xl_cmdtable.c - -$(CLIENTS): xl.o xl_cmdimpl.o xl_cmdtable.o libxlutil.so libxenlight.so +$(CLIENTS): $(XL_OBJS) libxlutil.so libxenlight.so $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) .PHONY: install
PATCH 1/1 libxl: define specific types for string list and key, value list
In reply to Ian Campbell# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281706573 -3600
# Node ID bee7f853a66535787786e99f5eba951717cea0ac
# Parent d033ff85952a36f7c5022832dd8a6381802b87b8
libxl: define specific types for string list and key,value list
tools/libxl/libxl.h +7 -3
@@ -25,6 +25,10 @@ typedef uint8_t libxl_uuid[16];typedef uint8_t libxl_uuid [16 ];typedef uint8_t libxl_mac [6 ]; + +typedef char **libxl_string_list ; + +typedef char **libxl_key_value_list ; typedef struct { libxl_uuid uuid;
@@ -94,8 +98,8 @@ typedef struct {int ssidref;char *name;libxl_uuid uuid; -char **xsdata; -char **platformdata; +libxl_key_value_list xsdata; +libxl_key_value_list platformdata;uint32_t poolid;char *poolname; } libxl_domain_create_info;
@@ -198,7 +202,7 @@ typedef struct {int vcpus;/* max number of vcpus */ int vcpu_avail;/* vcpus actually available */ int xen_platform_pci;/* enable/disable the xen platform pci device */ -char **extra;/* extra parameters pass directly to qemu, NULL terminated */ +libxl_string_list extra;/* extra parameters pass directly to qemu, NULL terminated */ /* Network is missing */ } libxl_device_model_info;
PATCH 1/1 libxl: move various enum and #defines above datastructure definitions
In reply to Ian Campbell# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281706573 -3600
# Node ID 06e63fc8240a8d5902087947e016b7f69ab53071
# Parent bee7f853a66535787786e99f5eba951717cea0ac
libxl: move various enum and #defines above datastructure definitions.
These are left behind when the datastructures move to _lixl_types.hg in a following patch and moving them first makes that pure code motion patch more obviously correct.
tools/libxl/libxl.h +26 -25
@@ -29,6 +29,32 @@ typedef char **libxl_string_list;typedef char **libxl_string_list ;typedef char **libxl_key_value_list ; + +typedef enum { +XENFV =1 , +XENPV , +}libxl_qemu_machine_type ; + +typedef enum { +CONSTYPE_XENCONSOLED , +CONSTYPE_IOEMU , +}libxl_console_constype ; + +typedef enum { +PHYSTYPE_QCOW =1 , +PHYSTYPE_QCOW2 , +PHYSTYPE_VHD , +PHYSTYPE_AIO , +PHYSTYPE_FILE , +PHYSTYPE_PHY , +}libxl_disk_phystype ; + +typedef enum { +NICTYPE_IOEMU =1 , +NICTYPE_VIF , +}libxl_nic_type ; + +#define LIBXL_PCI_FUNC_ALL (~0U) typedef struct { libxl_uuid uuid;
@@ -170,11 +196,6 @@ typedef struct {int (*suspend_callback)(void *,int ); } libxl_domain_suspend_info; -typedef enum { -XENFV =1 , -XENPV , -}libxl_qemu_machine_type ; - typedef struct { int domid; libxl_uuid uuid; /* this is use only with stubdom, and must be different from the domain uuid */
@@ -228,11 +249,6 @@ typedef struct {int devid; } libxl_device_vkb; -typedef enum { -CONSTYPE_XENCONSOLED , -CONSTYPE_IOEMU , -}libxl_console_constype ; - typedef struct { uint32_t backend_domid; uint32_t domid;
@@ -240,15 +256,6 @@ typedef struct {libxl_console_constype constype;libxl_domain_build_state *build_state; } libxl_device_console; - -typedef enum { -PHYSTYPE_QCOW =1 , -PHYSTYPE_QCOW2 , -PHYSTYPE_VHD , -PHYSTYPE_AIO , -PHYSTYPE_FILE , -PHYSTYPE_PHY , -}libxl_disk_phystype ; typedef struct { uint32_t backend_domid;
@@ -260,11 +267,6 @@ typedef struct {int readwrite;int is_cdrom; } libxl_device_disk; - -typedef enum { -NICTYPE_IOEMU =1 , -NICTYPE_VIF , -}libxl_nic_type ; typedef struct { uint32_t backend_domid;
@@ -310,7 +312,6 @@ typedef struct { };unsigned int domain;unsigned int vdevfn; -#define LIBXL_PCI_FUNC_ALL (~0U)unsigned int vfunc_mask;bool msitranslate;bool power_mgmt;
PATCH 1/1 libxl: ensure result of libxl_poolid_to_name is always dynamically allocated
In reply to Ian Campbell# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281706573 -3600
# Node ID 4fa4efb9c63e1ef60daae928b0d7c5ca992681dd
# Parent 06e63fc8240a8d5902087947e016b7f69ab53071
libxl: ensure result of libxl_poolid_to_name is always dynamically allocated
tools/libxl/libxl_utils.c +1 -1
@@ -97,7 +97,7 @@ char *libxl_poolid_to_name(libxl_ctx *ctchar *s;if (poolid ==0 ) -return "Pool-0" ; +return strdup ("Pool-0" );snprintf (path,sizeof (path),"/local/pool/%d/name" , poolid); s =xs_read (ctx->xsh,XBT_NULL , path, &len);return s;
PATCH 1/1 libxl: move type definitions into _libxl_types.h
In reply to Ian Campbell# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281706573 -3600
# Node ID d44e1dca002e723c8c58f622329d4dbd37287b1c # Parent 4fa4efb9c63e1ef60daae928b0d7c5ca992681dd
libxl: move type definitions into _libxl_types.h
The intention is to autogenerate this file in a future patch. This pure code motion patch allows for easier before and after comparison of that patch.
tools/libxl/Makefile +4 -1
@@ -96,12 +96,15 @@ install: all ln -sf libxlutil.so.$(XLUMAJOR).$(XLUMINOR) $(DESTDIR)$(LIBDIR)/libxlutil.so.$(XLUMAJOR) ln -sf libxlutil.so.$(XLUMAJOR) $(DESTDIR)$(LIBDIR)/libxlutil.so $(INSTALL_DATA) libxlutil.a $(DESTDIR)$(LIBDIR) - $(INSTALL_DATA) libxl.h $(DESTDIR)$(INCLUDEDIR) + $(INSTALL_DATA) libxl.h _libxl_types.h $(DESTDIR)$(INCLUDEDIR) $(INSTALL_DATA) bash-completion $(DESTDIR)$(BASH_COMPLETION_DIR)/xl.sh .PHONY: clean clean: + # XXX Preserve during transition to autogeneration + cp _libxl_types.h SAVED__libxl_types.h $(RM) -f _*.h *.o *.so* *.a $(CLIENTS) $(DEPS) + mv SAVED__libxl_types.h _libxl_types.h # $(RM) -f $(AUTOSRCS) $(AUTOINCS) distclean: clean
tools/libxl/_libxl_types.h new file +244 -0
244 lines not on this page · raw message
tools/libxl/libxl.h +1 -239
257 lines not on this page · raw message
PATCH 1/1 libxl: tweak formatting/whitespace of _libxl_types.h
In reply to Ian Campbell# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281706573 -3600
# Node ID d0aa5e519845806c77a3236fe9c9fe4bc0c237b9 # Parent d44e1dca002e723c8c58f622329d4dbd37287b1c
libxl: tweak formatting/whitespace of _libxl_types.h to better suit autogeneration
This makes it easier to do a before/after comparison when the file becomes autogenerated.
tools/libxl/_libxl_types.h +157 -65
@@ -9,7 +9,6 @@ typedef struct { uint8_t paused:1; uint8_t shutdown:1; uint8_t dying:1; -/* * Valid SHUTDOWN_* value from xen/sched.h iff (shutdown||dying). *
@@ -17,7 +16,6 @@ typedef struct {* SHUTDOWN_* constant. */ unsigned int shutdown_reason; -uint64_t max_memkb;uint64_t cpu_time;uint32_t vcpu_max_id;
@@ -36,16 +34,16 @@ typedef struct {typedef struct {int xen_version_major;int xen_version_minor; -char *xen_version_extra; -char *compiler; -char *compile_by; -char *compile_domain; -char *compile_date; -char *capabilities; -char *changeset; +char * xen_version_extra; +char * compiler; +char * compile_by; +char * compile_domain; +char * compile_date; +char * capabilities; +char * changeset;unsigned long virt_start;unsigned long pagesize; -char *commandline; +char * commandline; }libxl_version_info ; typedef struct {
@@ -53,22 +51,22 @@ typedef struct {bool hap;bool oos;int ssidref; -char *name; +char * name;libxl_uuid uuid;libxl_key_value_list xsdata;libxl_key_value_list platformdata;uint32_t poolid; -char *poolname; +char * poolname; } libxl_domain_create_info;typedef struct {/* -* Path is always set if the file refernece is valid. However if +* Path is always set if the file reference is valid. However if * mapped is true then the actual file may already be unlinked. */ -char *path; +char * path;int mapped; -void *data; +void * data;size_t size; }libxl_file_reference ;
@@ -97,18 +95,18 @@ typedef struct {bool acpi;bool nx;bool viridian; -char *timeoffset; +char * timeoffset;bool hpet;bool vpt_align;int timer_mode; } hvm;struct { -uint32_t slack_memkb; -const char *bootloader; -const char *bootloader_args; -char *cmdline; +uint32_t slack_memkb; +const char * bootloader; +const char * bootloader_args; +char * cmdline;libxl_file_reference ramdisk; -const char *features; +const char * features; } pv; } u; } libxl_domain_build_info;
@@ -120,51 +118,145 @@ typedef struct {unsigned long console_mfn; } libxl_domain_build_state; +/* +* Device Model information. +* +* Network is missing +*/ typedef struct {int domid; -libxl_uuid uuid;/* this is use only with stubdom, and must be different from the domain uuid */ -char *dom_name; -char *device_model; -char *saved_state; +/* +* this is use only with stubdom, and must be different from the domain uuid +*/ +libxl_uuid uuid; +char * dom_name; +char * device_model; +char * saved_state;libxl_qemu_machine_type type; -int videoram;/* size of the videoram in MB */ -bool stdvga;/* stdvga enabled or disabled */ -bool vnc;/* vnc enabled or disabled */ -char *vnclisten;/* address:port that should be listened on for the VNC server if vnc is set */ -char *vncpasswd;/* the VNC password */ -int vncdisplay;/* set VNC display number */ -bool vncunused;/* try to find an unused port for the VNC server */ -char *keymap;/* set keyboard layout, default is en-us keyboard */ -bool sdl;/* sdl enabled or disabled */ -bool opengl;/* opengl enabled or disabled (if enabled requires sdl enabled) */ -bool nographic;/* no graphics, use serial port */ -char *serial;/* serial port re-direct to pty deivce */ -char *boot;/* boot order, for example dca */ -bool usb;/* usb support enabled or disabled */ -char *usbdevice;/* enable usb mouse: tablet for absolute mouse, mouse for PS/2 protocol relative mouse */ -char *soundhw;/* enable sound hardware */ -bool apic;/* apic enabled or disabled */ -int vcpus;/* max number of vcpus */ -int vcpu_avail;/* vcpus actually available */ -int xen_platform_pci;/* enable/disable the xen platform pci device */ -libxl_string_list extra;/* extra parameters pass directly to qemu, NULL terminated */ -/* Network is missing */ +/* +* size of the videoram in MB +*/ +int videoram; +/* +* stdvga enabled or disabled +*/ +bool stdvga; +/* +* vnc enabled or disabled +*/ +bool vnc; +/* +* address:port that should be listened on for the VNC server if vnc is set +*/ +char * vnclisten; +/* +* the VNC password +*/ +char * vncpasswd; +/* +* set VNC display number +*/ +int vncdisplay; +/* +* try to find an unused port for the VNC server +*/ +bool vncunused; +/* +* set keyboard layout, default is en-us keyboard +*/ +char * keymap; +/* +* sdl enabled or disabled +*/ +bool sdl; +/* +* opengl enabled or disabled (if enabled requires sdl enabled) +*/ +bool opengl; +/* +* no graphics, use serial port +*/ +bool nographic; +/* +* serial port re-direct to pty deivce +*/ +char * serial; +/* +* boot order, for example dca +*/ +char * boot; +/* +* usb support enabled or disabled +*/ +bool usb; +/* +* enable usb mouse: tablet for absolute mouse, mouse for PS/2 protocol relative mouse +*/ +char * usbdevice; +/* +* enable sound hardware +*/ +char * soundhw; +/* +* apic enabled or disabled +*/ +bool apic; +/* +* max number of vcpus +*/ +int vcpus; +/* +* vcpus actually available +*/ +int vcpu_avail; +/* +* enable/disable the xen platform pci device +*/ +int xen_platform_pci; +/* +* extra parameters pass directly to qemu, NULL terminated +*/ +libxl_string_list extra; }libxl_device_model_info ;typedef struct {uint32_t backend_domid;uint32_t domid;int devid; -bool vnc;/* vnc enabled or disabled */ -char *vnclisten;/* address:port that should be listened on for the VNC server if vnc is set */ -char *vncpasswd;/* the VNC password */ -int vncdisplay;/* set VNC display number */ -bool vncunused;/* try to find an unused port for the VNC server */ -char *keymap;/* set keyboard layout, default is en-us keyboard */ -bool sdl;/* sdl enabled or disabled */ -bool opengl;/* opengl enabled or disabled (if enabled requires sdl enabled) */ -char *display; -char *xauthority; +/* +* vnc enabled or disabled +*/ +bool vnc; +/* +* address:port that should be listened on for the VNC server if vnc is set +*/ +char * vnclisten; +/* +* the VNC password +*/ +char * vncpasswd; +/* +* set VNC display number +*/ +int vncdisplay; +/* +* try to find an unused port for the VNC server +*/ +bool vncunused; +/* +* set keyboard layout, default is en-us keyboard +*/ +char * keymap; +/* +* sdl enabled or disabled +*/ +bool sdl; +/* +* opengl enabled or disabled (if enabled requires sdl enabled) +*/ +bool opengl; +char * display; +char * xauthority; }libxl_device_vfb ; typedef struct {
@@ -178,15 +270,15 @@ typedef struct {uint32_t domid;int devid;libxl_console_constype constype; -libxl_domain_build_state *build_state; +libxl_domain_build_state * build_state; } libxl_device_console; typedef struct { uint32_t backend_domid; uint32_t domid; - char *physpath; + char * physpath; libxl_disk_phystype phystype; - char *virtpath; + char * virtpath; int unpluggable; int readwrite; int is_cdrom;
@@ -197,12 +289,12 @@ typedef struct {uint32_t domid;int devid;int mtu; -char *model; +char * model;libxl_mac mac;struct in_addr ip; -char *bridge; -char *ifname; -char *script; +char * bridge; +char * ifname; +char * script;libxl_nic_type nictype; } libxl_device_nic;
@@ -218,7 +310,7 @@ typedef struct { uint32_t front_filter_mac:1;uint32_t pdev;uint32_t max_bypasses; -char *bridge; +char * bridge; } libxl_device_net2; typedef struct {
PATCH 1/1 libxl: autogenerate _libxl_types.h
In reply to Ian Campbell# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281707196 -3600
# Node ID eb6b31dba5994e748d1aaf3f3eb60ba46e3ae7c3 # Parent d0aa5e519845806c77a3236fe9c9fe4bc0c237b9
libxl: autogenerate _libxl_types.h
The libxl interface types are represented by a simple python data structure (which could be parsed from a bespoke language in the future).
This will allow the autogeneration of functions to free the component members of the libxl types. In the future it may also enable auto generation of type marshalling code for language bindings.
The generated file should be identical to before with the exception of the "DO NOT EDIT" header.
It was unfortunately necessary to add explcit an dependency on _libxl_types.h (indirectly via libxl.h) to all C files since the autogenerated dependencies are not available in time.
tools/libxl/Makefile +8 -3
@@ -54,6 +54,14 @@ _libxl_paths.h: genpath libxl_paths.c: _libxl_paths.h +libxl.h: _libxl_types.h + +$(LIBXL_OBJS:%.o=%.c) $(LIBXLU_OBJS:%.o=%.c) $(XL_OBJS:%.o=%.c): libxl.h + +_libxl_types.h: libxltypes.idl gentypes.py libxltypes.py + python gentypes.py libxltypes.idl __libxl_types.h + mv __libxl_types.h _libxl_types.h + libxenlight.so: libxenlight.so.$(MAJOR) ln -sf $< $@
@@ -101,10 +109,7 @@ install: all .PHONY: clean clean: - # XXX Preserve during transition to autogeneration - cp _libxl_types.h SAVED__libxl_types.h $(RM) -f _*.h *.o *.so* *.a $(CLIENTS) $(DEPS) - mv SAVED__libxl_types.h _libxl_types.h # $(RM) -f $(AUTOSRCS) $(AUTOINCS) distclean: clean
tools/libxl/_libxl_types.h deleted +0 -336
336 lines not on this page · raw message
tools/libxl/gentypes.py new file +91 -0
91 lines not on this page · raw message
tools/libxl/libxltypes.idl new file +253 -0
253 lines not on this page · raw message
tools/libxl/libxltypes.py new file +168 -0
168 lines not on this page · raw message
PATCH 1/1 libxl: generate destructors for each libxl defined type
In reply to Ian Campbell# HG changeset patch
# User Ian Campbell <[email protected]> # Date 1281707272 -3600
# Node ID 9b62c9e42f2472aaf4a3d9ce492c5c14cdfa8249 # Parent eb6b31dba5994e748d1aaf3f3eb60ba46e3ae7c3
libxl: generate destructors for each libxl defined type
I chose the name "_destroy" rather than "_free" because the destructor functions will free only the members of a type recursively but will not free the actual type structure itself. The allocation of the type is typically done by the caller and may not be a single allocation, e.g. lists/arrays of types or embedded in other strucutures etc.
The exceptions to this rule are libxl_string_list_destroy and libxl_key_value_list_destroy but I'm not 100% convinced they are exceptions (since they are kind-of opaque) and I couldn't see a cleanerway to express this concept. I have made a best effort attempt to implement these functions sanely but since as far as I can tell nothing in the current code base ever sets libxl_domain_create_info.{xsdata,platformdata} I'm flying somewhat blind.
.hgignore +1 -0
@@ -182,6 +182,7 @@ ^tools/libxen/test/test_bindings$ ^tools/libxen/test/test_event_handling$ ^tools/libxl/_.*\.h$ +^tools/libxl/_.*\.c$ ^tools/libxl/libxlu_cfg_y\.output$ ^tools/libxl/xl$ ^tools/libaio/src/.*\.ol$
tools/libxl/Makefile +5 -2
@@ -19,6 +19,7 @@ LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_lib LIBXL_OBJS-y = osdeps.o libxl_paths.o libxl_bootloader.o LIBXL_OBJS = flexarray.o libxl.o libxl_pci.o libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o libxl_internal.o xenguest.o libxl_utils.o $(LIBXL_OBJS-y) +LIBXL_OBJS += _libxl_types.o AUTOINCS= libxlu_cfg_y.h libxlu_cfg_l.h AUTOSRCS= libxlu_cfg_y.c libxlu_cfg_l.c
@@ -58,9 +59,10 @@ libxl.h: _libxl_types.h $(LIBXL_OBJS:%.o=%.c) $(LIBXLU_OBJS:%.o=%.c) $(XL_OBJS:%.o=%.c): libxl.h -_libxl_types.h: libxltypes.idl gentypes.py libxltypes.py - python gentypes.py libxltypes.idl __libxl_types.h +_libxl_types.h _libxl_types.c: gentypes.py libxltypes.py + python gentypes.py libxltypes.idl __libxl_types.h __libxl_types.c mv __libxl_types.h _libxl_types.h + mv __libxl_types.c _libxl_types.c libxenlight.so: libxenlight.so.$(MAJOR) ln -sf $< $@
@@ -110,6 +112,7 @@ install: all .PHONY: clean clean: $(RM) -f _*.h *.o *.so* *.a $(CLIENTS) $(DEPS) + $(RM) -f _*.c # $(RM) -f $(AUTOSRCS) $(AUTOINCS) distclean: clean
tools/libxl/gentypes.py +73 -5
97 lines not on this page · raw message
tools/libxl/libxl.c +27 -0
@@ -72,6 +72,33 @@ int libxl_ctx_free(libxl_ctx *ctx)do_free_version_info (&ctx->version_info);if (ctx->xsh)xs_daemon_close (ctx->xsh);return 0 ; +} + +void libxl_string_list_destroy (libxl_string_list sl) +{ +int i; + +if (!sl) +return ; + +for (i =0 ; sl[i] !=NULL ; i++) +free (sl[i]); +free (sl); +} + +void libxl_key_value_list_destroy (libxl_key_value_list kvl) +{ +int i; + +if (!kvl) +return ; + +for (i =0 ; kvl[i] !=NULL ; i +=2 ) { +free (kvl[i]); +if (kvl[i +1 ]) +free (kvl[i +1 ]); + } +free (kvl); }/******************************************************************************/
tools/libxl/libxl.h +2 -0
@@ -27,8 +27,10 @@ typedef uint8_t libxl_mac[6];typedef uint8_t libxl_mac [6 ];typedef char **libxl_string_list ; +void libxl_string_list_destroy (libxl_string_list sl);typedef char **libxl_key_value_list ; +void libxl_key_value_list_destroy (libxl_key_value_list kvl); typedef enum { XENFV = 1,
tools/libxl/libxltypes.idl +2 -2
@@ -11,8 +11,8 @@ libxl_disk_phystype = Builtin("disk_phys libxl_disk_phystype = Builtin("disk_phystype") libxl_nic_type = Builtin("nic_type") -libxl_string_list = Builtin("string_list") -libxl_key_value_list = Builtin("key_value_list") +libxl_string_list = Builtin("string_list", destructor_fn="libxl_string_list_destroy") +libxl_key_value_list = Builtin("key_value_list", destructor_fn="libxl_key_value_list_destroy") # # Complex libxl types
tools/libxl/libxltypes.py +31 -1
@@ -1,9 +1,16 @@ import sys import sys + +PASS_BY_VALUE = 1 +PASS_BY_REFERENCE = 2 class Type(object): def __init__(self, typename, **kwargs): self.comment = kwargs.setdefault('comment', None) self.namespace = kwargs.setdefault('namespace', "libxl_") + + self.passby = kwargs.setdefault('passby', PASS_BY_VALUE) + if self.passby not in [PASS_BY_VALUE, PASS_BY_REFERENCE]: + raise ValueError if typename is None: # Anonymous type self.typename = None
@@ -12,14 +19,23 @@ class Type(object): else: self.typename = self.namespace + typename + if self.typename is not None: + self.destructor_fn = kwargs.setdefault('destructor_fn', self.typename + "_destroy") + else: + self.destructor_fn = kwargs.setdefault('destructor_fn', None) + + self.autogenerate_destructor = kwargs.setdefault('autogenerate_destructor', True) + class Builtin(Type): """Builtin type""" def __init__(self, typename, **kwargs): + kwargs.setdefault('destructor_fn', None) Type.__init__(self, typename, **kwargs) class UInt(Type): def __init__(self, w, **kwargs): kwargs.setdefault('namespace', None) + kwargs.setdefault('destructor_fn', None) Type.__init__(self, "uint%d_t" % w, **kwargs) self.width = w
@@ -27,6 +43,7 @@ class BitField(Type): class BitField(Type): def __init__(self, ty, w, **kwargs): kwargs.setdefault('namespace', None) + kwargs.setdefault('destructor_fn', None) Type.__init__(self, ty.typename, **kwargs) self.width = w
@@ -63,10 +80,16 @@ class Aggregate(Type): class Struct(Aggregate): def __init__(self, name, fields, **kwargs): + kwargs.setdefault('passby', PASS_BY_REFERENCE) Aggregate.__init__(self, "struct", name, fields, **kwargs) class Union(Aggregate): def __init__(self, name, fields, **kwargs): + # Generally speaking some intelligence is required to free a + # union therefore any specific instance of this class will + # need to provide an explicit destructor function. + kwargs.setdefault('passby', PASS_BY_REFERENCE) + kwargs.setdefault('destructor_fn', None) Aggregate.__init__(self, "union", name, fields, **kwargs) class KeyedUnion(Aggregate):
@@ -87,7 +110,14 @@ class Reference(Type): class Reference(Type): """A reference to another type""" def __init__(self, ty, **kwargs): + self.ref_type = ty + # Ugh + + kwargs.setdefault('destructor_fn', "free") + kwargs.setdefault('autogenerate_destructor', False) + kwargs.setdefault('passby', PASS_BY_VALUE) + kwargs.setdefault('namespace', ty.namespace) typename = ty.typename[len(kwargs['namespace']):] Type.__init__(self, typename + " *", **kwargs)
@@ -112,7 +142,7 @@ uint64 = UInt(64) domid = UInt(32) -string = Builtin("char *", namespace = None) +string = Builtin("char *", namespace = None, destructor_fn = "free") inaddr_ip = Builtin("struct in_addr", namespace = None)
PATCH 1/1 xl: free the libxl types contained in struct domain_config
In reply to Ian Campbell# HG changeset patch
# User Ian Campbell <[email protected]>
# Date 1281707273 -3600
# Node ID adefd8ef59992b04b6d7da35f81ff6d7cc48a6a6 # Parent 9b62c9e42f2472aaf4a3d9ce492c5c14cdfa8249
xl: free the libxl types contained in struct domain_config
Resolves the following leaks found using "valgrind xl create -n ..." (plus presumably others which didn't trigger because I have no devices of that type)
==21241== 5 bytes in 1 blocks are definitely lost in loss record 1 of 6
==21241== at 0x4022F0A: malloc (vg_replace_malloc.c:236) ==21241== by 0x411B22F: strdup (in /lib/i686/cmov/libc-2.7.so) ==21241== by 0x804E56B: parse_config_data (xl_cmdimpl.c:760) ==21241== by 0x8056B04: create_domain (xl_cmdimpl.c:1357) ==21241== by 0x8057ECE: main_create (xl_cmdimpl.c:3154) ==21241== by 0x804B2FB: main (xl.c:76) ==21241== ==21241== 6 bytes in 1 blocks are definitely lost in loss record 2 of 6 ==21241== at 0x4022F0A: malloc (vg_replace_malloc.c:236) ==21241== by 0x411B22F: strdup (in /lib/i686/cmov/libc-2.7.so) ==21241== by 0x804DAAA: parse_config_data (xl_cmdimpl.c:586) ==21241== by 0x8056B04: create_domain (xl_cmdimpl.c:1357) ==21241== by 0x8057ECE: main_create (xl_cmdimpl.c:3154) ==21241== by 0x804B2FB: main (xl.c:76) ==21241== ==21241== 24 bytes in 1 blocks are definitely lost in loss record 3 of 6 ==21241== at 0x4022F0A: malloc (vg_replace_malloc.c:236) ==21241== by 0x411B22F: strdup (in /lib/i686/cmov/libc-2.7.so) ==21241== by 0x804E51F: parse_config_data (xl_cmdimpl.c:754) ==21241== by 0x8056B04: create_domain (xl_cmdimpl.c:1357) ==21241== by 0x8057ECE: main_create (xl_cmdimpl.c:3154) ==21241== by 0x804B2FB: main (xl.c:76) ==21241== ==21241== 27 bytes in 1 blocks are definitely lost in loss record 4 of 6 ==21241== at 0x4022F0A: malloc (vg_replace_malloc.c:236) ==21241== by 0x411B22F: strdup (in /lib/i686/cmov/libc-2.7.so) ==21241== by 0x804DF4A: parse_config_data (xl_cmdimpl.c:664) ==21241== by 0x8056B04: create_domain (xl_cmdimpl.c:1357) ==21241== by 0x8057ECE: main_create (xl_cmdimpl.c:3154) ==21241== by 0x804B2FB: main (xl.c:76) ==21241== ==21241== 28 bytes in 1 blocks are definitely lost in loss record 5 of 6 ==21241== at 0x4022F0A: malloc (vg_replace_malloc.c:236) ==21241== by 0x410C1A7: vasprintf (in /lib/i686/cmov/libc-2.7.so) ==21241== by 0x40EE59A: asprintf (in /lib/i686/cmov/libc-2.7.so) ==21241== by 0x804CA8B: init_nic_info (xl_cmdimpl.c:322) ==21241== by 0x804E6FF: parse_config_data (xl_cmdimpl.c:791) ==21241== by 0x8056B04: create_domain (xl_cmdimpl.c:1357) ==21241== by 0x8057ECE: main_create (xl_cmdimpl.c:3154) ==21241== by 0x804B2FB: main (xl.c:76) ==21241== ==21241== 44 bytes in 1 blocks are definitely lost in loss record 6 of 6 ==21241== at 0x4022F0A: malloc (vg_replace_malloc.c:236) ==21241== by 0x411B22F: strdup (in /lib/i686/cmov/libc-2.7.so) ==21241== by 0x804E168: parse_config_data (xl_cmdimpl.c:694) ==21241== by 0x8056B04: create_domain (xl_cmdimpl.c:1357) ==21241== by 0x8057ECE: main_create (xl_cmdimpl.c:3154) ==21241== by 0x804B2FB: main (xl.c:76)
tools/libxl/xl_cmdimpl.c +26 -2
@@ -146,12 +146,34 @@ struct domain_config {static void free_domain_config (struct domain_config *d_config) { +int i; + +for (i=0 ; i<d_config->num_disks; i++) +libxl_device_disk_destroy (&d_config->disks[i]);free (d_config->disks); + +for (i=0 ; i<d_config->num_vifs; i++) +libxl_device_nic_destroy (&d_config->vifs[i]);free (d_config->vifs); + +for (i=0 ; i<d_config->num_vif2s; i++) +libxl_device_net2_destroy (&d_config->vif2s[i]);free (d_config->vif2s); + +for (i=0 ; i<d_config->num_pcidevs; i++) +libxl_device_pci_destroy (&d_config->pcidevs[i]);free (d_config->pcidevs); + +for (i=0 ; i<d_config->num_vfbs; i++) +libxl_device_vfb_destroy (&d_config->vfbs[i]);free (d_config->vfbs); + +for (i=0 ; i<d_config->num_vkbs; i++) +libxl_device_vkb_destroy (&d_config->vkbs[i]);free (d_config->vkbs); + +libxl_domain_create_info_destroy (&d_config->c_info); +libxl_domain_build_info_destroy (&d_config->b_info); }/* Optional data, in order:
@@ -311,7 +333,7 @@ static void init_nic_info(libxl_device_n nic_info->domid =0 ; nic_info->devid = devnum; nic_info->mtu =1492 ; - nic_info->model ="e1000" ; + nic_info->model =strdup ("e1000" ); nic_info->mac[0 ] =0x00 ; nic_info->mac[1 ] =0x16 ; nic_info->mac[2 ] =0x3e ;
@@ -319,7 +341,7 @@ static void init_nic_info(libxl_device_n nic_info->mac[4 ] =1 + (int ) (0xff * (rand () / (RAND_MAX +1.0 ))); nic_info->mac[5 ] =1 + (int ) (0xff * (rand () / (RAND_MAX +1.0 ))); nic_info->ifname =NULL ; - nic_info->bridge ="xenbr0" ; + nic_info->bridge =strdup ("xenbr0" );CHK_ERRNO (asprintf (&nic_info->script,"%s/vif-bridge" ,libxl_xen_script_dir_path ()) ); nic_info->nictype =NICTYPE_IOEMU ;
@@ -801,6 +823,7 @@ static void parse_config_data(const charbreak ; *p2 ='\0' ;if (!strcmp (p,"model" )) { +free (nic->model); nic->model =strdup (p2 +1 ); }else if (!strcmp (p,"mac" )) {char *p3 = p2 +1 ;
@@ -822,6 +845,7 @@ static void parse_config_data(const char *(p3 +2 ) ='\0' ; nic->mac[5 ] =strtol (p3,NULL ,16 ); }else if (!strcmp (p,"bridge" )) { +free (nic->bridge); nic->bridge =strdup (p2 +1 ); }else if (!strcmp (p,"type" )) {if (!strcmp (p2 +1 ,"ioemu" ))