diff options
author | Gianni Tedesco <gianni.tedesco@citrix.com> | 2010-08-12 18:55:16 +0100 |
---|---|---|
committer | Gianni Tedesco <gianni.tedesco@citrix.com> | 2010-08-12 18:55:16 +0100 |
commit | 94413e51b0558f0b55137328d51ce8c26dd358a0 (patch) | |
tree | 1716442ff500684121d4da656788c812ce35606a /tools/libxl/libxl_xshelp.c | |
parent | db6b874fcd1430219176a4f86c2bf21dc4946388 (diff) | |
download | xen-94413e51b0558f0b55137328d51ce8c26dd358a0.tar.gz xen-94413e51b0558f0b55137328d51ce8c26dd358a0.tar.bz2 xen-94413e51b0558f0b55137328d51ce8c26dd358a0.zip |
xl: Implement per-API-call garbage-collection lifetime
Currently scratch variables allocated by libxl have the same lifetime as
the context. While this is suitable for one off invocations of xl. It is
not so great for a daemon process linking to libxl. In that case there
will be prolific leakage of heap memory.
My proposed solution involves create a new libxl_gc structure, which
contains a pointer to an owning context as well as the garbage
collection data. Top-level library functions which expect to do a lot of
scratch allocations put gc struct on the stack and initialize it with a
macro. Before returning they then call libxl_free_all on this struct.
This means that static helper functions called by such functions will
usually take a gc instead of a ctx as a first parameter.
The patch touches almost every code-path so a close review and testing
would be much appreciated. I have tested with valgrind all of the parts
I could which looked non-straightforward. Suffice to say that it seems
crash-free even if we have exposed a few real memory leaks. These are
for cases where we return eg. block list to an xl caller but there is no
appropriate block_list_free() function to call. Ian Campbells work in
this area should sew up all these loose ends.
Signed-off-by: Gianni Tedesco <gianni.tedesco@citrix.com>
committer: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Diffstat (limited to 'tools/libxl/libxl_xshelp.c')
-rw-r--r-- | tools/libxl/libxl_xshelp.c | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/tools/libxl/libxl_xshelp.c b/tools/libxl/libxl_xshelp.c index 8db2e5bb69..3098f343c2 100644 --- a/tools/libxl/libxl_xshelp.c +++ b/tools/libxl/libxl_xshelp.c @@ -44,12 +44,12 @@ int xs_writev(struct xs_handle *xsh, xs_transaction_t t, char *dir, char *kvs[]) return 0; } -char **libxl_xs_kvs_of_flexarray(libxl_ctx *ctx, flexarray_t *array, int length) +char **libxl_xs_kvs_of_flexarray(libxl_gc *gc, flexarray_t *array, int length) { char **kvs; int i; - kvs = libxl_calloc(ctx, length + 2, sizeof(char *)); + kvs = libxl_calloc(gc, length + 2, sizeof(char *)); if (kvs) { for (i = 0; i < length; i += 2) { void *ptr; @@ -65,9 +65,10 @@ char **libxl_xs_kvs_of_flexarray(libxl_ctx *ctx, flexarray_t *array, int length) return kvs; } -int libxl_xs_writev(libxl_ctx *ctx, xs_transaction_t t, +int libxl_xs_writev(libxl_gc *gc, xs_transaction_t t, char *dir, char *kvs[]) { + libxl_ctx *ctx = libxl_gc_owner(gc); char *path; int i; @@ -75,19 +76,19 @@ int libxl_xs_writev(libxl_ctx *ctx, xs_transaction_t t, return 0; for (i = 0; kvs[i] != NULL; i += 2) { - path = libxl_sprintf(ctx, "%s/%s", dir, kvs[i]); + path = libxl_sprintf(gc, "%s/%s", dir, kvs[i]); if (path && kvs[i + 1]) { int length = strlen(kvs[i + 1]); xs_write(ctx->xsh, t, path, kvs[i + 1], length); } - libxl_free(ctx, path); } return 0; } -int libxl_xs_write(libxl_ctx *ctx, xs_transaction_t t, +int libxl_xs_write(libxl_gc *gc, xs_transaction_t t, char *path, char *fmt, ...) { + libxl_ctx *ctx = libxl_gc_owner(gc); char *s; va_list ap; int ret; @@ -103,35 +104,38 @@ int libxl_xs_write(libxl_ctx *ctx, xs_transaction_t t, return 0; } -char * libxl_xs_read(libxl_ctx *ctx, xs_transaction_t t, char *path) +char * libxl_xs_read(libxl_gc *gc, xs_transaction_t t, char *path) { + libxl_ctx *ctx = libxl_gc_owner(gc); unsigned int len; char *ptr; ptr = xs_read(ctx->xsh, t, path, &len); if (ptr != NULL) { - libxl_ptr_add(ctx, ptr); + libxl_ptr_add(gc, ptr); return ptr; } return 0; } -char *libxl_xs_get_dompath(libxl_ctx *ctx, uint32_t domid) +char *libxl_xs_get_dompath(libxl_gc *gc, uint32_t domid) { + libxl_ctx *ctx = libxl_gc_owner(gc); char *s = xs_get_domain_path(ctx->xsh, domid); if (!s) { XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to get dompath for %" PRIu32, domid); return NULL; } - libxl_ptr_add(ctx, s); + libxl_ptr_add(gc, s); return s; } -char **libxl_xs_directory(libxl_ctx *ctx, xs_transaction_t t, char *path, unsigned int *nb) +char **libxl_xs_directory(libxl_gc *gc, xs_transaction_t t, char *path, unsigned int *nb) { + libxl_ctx *ctx = libxl_gc_owner(gc); char **ret = NULL; ret = xs_directory(ctx->xsh, XBT_NULL, path, nb); - libxl_ptr_add(ctx, ret); + libxl_ptr_add(gc, ret); return ret; } |