WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-devel

[Xen-devel] [PATCH] libxl: Make all hidden/static functions take a gc no

To: xen-devel@xxxxxxxxxxxxxxxxxxx
Subject: [Xen-devel] [PATCH] libxl: Make all hidden/static functions take a gc not a ctx
From: Ian Campbell <ian.campbell@xxxxxxxxxx>
Date: Tue, 15 Mar 2011 11:23:25 +0000
Cc: Ian Campbell <ian.campbell@xxxxxxxxxx>
Delivery-date: Tue, 15 Mar 2011 04:24:43 -0700
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-help: <mailto:xen-devel-request@lists.xensource.com?subject=help>
List-id: Xen developer discussion <xen-devel.lists.xensource.com>
List-post: <mailto:xen-devel@lists.xensource.com>
List-subscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=subscribe>
List-unsubscribe: <http://lists.xensource.com/mailman/listinfo/xen-devel>, <mailto:xen-devel-request@lists.xensource.com?subject=unsubscribe>
Sender: xen-devel-bounces@xxxxxxxxxxxxxxxxxxx
# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxx>
# Date 1300188153 0
# Node ID c0fef8dca729457137f875ada5564dcfd2ab40c2
# Parent  541961e0e1565c799fc432e2a11f06d3a604538e
libxl: Make all hidden/static functions take a gc not a ctx

Also ensure that static and hidden functions use the libxl__ prefix
not just libxl_ (in the case of static functions only when they use a
libxl prefix to start with).

This follows the policy described in libxl.h "libxl memory
management".

Based on a manual audit of:
grep ^static tools/libxl/libxl*.[ch]| grep libxl_ctx
grep libxl__ tools/libxl/*.h| grep libxl_ctx

Signed-off-by: Ian Campbell <ian.campbell@xxxxxxxxxx>

diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl.c
--- a/tools/libxl/libxl.c       Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl.c       Tue Mar 15 11:22:33 2011 +0000
@@ -223,7 +223,7 @@ int libxl_domain_resume(libxl_ctx *ctx, 
     libxl__gc gc = LIBXL_INIT_GC(ctx);
     int rc = 0;
 
-    if (libxl__domain_is_hvm(ctx, domid)) {
+    if (libxl__domain_is_hvm(&gc, domid)) {
         LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Called domain_resume on "
                 "non-cooperative hvm domain %u", domid);
         rc = ERROR_NI;
@@ -455,14 +455,16 @@ int libxl_domain_suspend(libxl_ctx *ctx,
 int libxl_domain_suspend(libxl_ctx *ctx, libxl_domain_suspend_info *info,
                          uint32_t domid, int fd)
 {
-    int hvm = libxl__domain_is_hvm(ctx, domid);
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    int hvm = libxl__domain_is_hvm(&gc, domid);
     int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
     int debug = info != NULL && info->flags & XL_SUSPEND_DEBUG;
     int rc = 0;
 
-    rc = libxl__domain_suspend_common(ctx, domid, fd, hvm, live, debug);
+    rc = libxl__domain_suspend_common(&gc, domid, fd, hvm, live, debug);
     if (!rc && hvm)
-        rc = libxl__domain_save_device_model(ctx, domid, fd);
+        rc = libxl__domain_save_device_model(&gc, domid, fd);
+    libxl__free_all(&gc);
     return rc;
 }
 
@@ -497,12 +499,12 @@ int libxl_domain_unpause(libxl_ctx *ctx,
     char *state;
     int ret, rc = 0;
 
-    if (libxl__domain_is_hvm(ctx, domid)) {
+    if (libxl__domain_is_hvm(&gc, domid)) {
         path = libxl__sprintf(&gc, "/local/domain/0/device-model/%d/state", 
domid);
         state = libxl__xs_read(&gc, XBT_NULL, path);
         if (state != NULL && !strcmp(state, "paused")) {
             libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, 
"/local/domain/0/device-model/%d/command", domid), "continue");
-            libxl__wait_for_device_model(ctx, domid, "running", NULL, NULL);
+            libxl__wait_for_device_model(&gc, domid, "running", NULL, NULL);
         }
     }
     ret = xc_domain_unpause(ctx->xch, domid);
@@ -539,7 +541,7 @@ int libxl_domain_shutdown(libxl_ctx *ctx
         return ERROR_FAIL;
     }
 
-    if (libxl__domain_is_hvm(ctx,domid)) {
+    if (libxl__domain_is_hvm(&gc, domid)) {
         unsigned long pvdriver = 0;
         int ret;
         ret = xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, 
&pvdriver);
@@ -713,7 +715,7 @@ int libxl_domain_destroy(libxl_ctx *ctx,
         return rc;
     }
 
-    if (libxl__domain_is_hvm(ctx, domid)) {
+    if (libxl__domain_is_hvm(&gc, domid)) {
         dm_present = 1;
     } else {
         char *pid;
@@ -734,10 +736,10 @@ int libxl_domain_destroy(libxl_ctx *ctx,
         LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, rc, "xc_domain_pause failed 
for %d", domid);
     }
     if (dm_present) {
-        if (libxl__destroy_device_model(ctx, domid) < 0)
+        if (libxl__destroy_device_model(&gc, domid) < 0)
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl__destroy_device_model 
failed for %d", domid);
     }
-    if (libxl__devices_destroy(ctx, domid, force) < 0)
+    if (libxl__devices_destroy(&gc, domid, force) < 0)
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "libxl_destroy_devices failed for 
%d", domid);
 
     vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/vm", 
dom_path));
@@ -748,7 +750,7 @@ int libxl_domain_destroy(libxl_ctx *ctx,
     if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_rm failed for %s", 
dom_path);
 
-    libxl__userdata_destroyall(ctx, domid);
+    libxl__userdata_destroyall(&gc, domid);
 
     rc = xc_domain_destroy(ctx->xch, domid);
     if (rc < 0) {
@@ -790,16 +792,20 @@ out:
 
 int libxl_primary_console_exec(libxl_ctx *ctx, uint32_t domid_vm)
 {
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
     uint32_t stubdomid = libxl_get_stubdom_id(ctx, domid_vm);
+    int rc;
     if (stubdomid)
-        return libxl_console_exec(ctx, stubdomid,
-                STUBDOM_CONSOLE_SERIAL, LIBXL_CONSTYPE_PV);
+        rc = libxl_console_exec(ctx, stubdomid,
+                                STUBDOM_CONSOLE_SERIAL, LIBXL_CONSTYPE_PV);
     else {
-        if (libxl__domain_is_hvm(ctx, domid_vm))
-            return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_SERIAL);
+        if (libxl__domain_is_hvm(&gc, domid_vm))
+            rc = libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_SERIAL);
         else
-            return libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_PV);
+            rc = libxl_console_exec(ctx, domid_vm, 0, LIBXL_CONSTYPE_PV);
     }
+    libxl__free_all(&gc);
+    return rc;
 }
 
 int libxl_vncviewer_exec(libxl_ctx *ctx, uint32_t domid, int autopass)
@@ -877,9 +883,10 @@ int libxl_vncviewer_exec(libxl_ctx *ctx,
 
 
/******************************************************************************/
 
-static int validate_virtual_disk(libxl_ctx *ctx, char *file_name,
-    libxl_device_disk *disk) 
+static int validate_virtual_disk(libxl__gc *gc, char *file_name,
+                                 libxl_device_disk *disk)
 {
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     struct stat stat_buf;
     char *delimiter;
 
@@ -924,7 +931,7 @@ int libxl_device_disk_add(libxl_ctx *ctx
     libxl__device device;
     int major, minor, rc;
 
-    rc = validate_virtual_disk(ctx, disk->pdev_path, disk); 
+    rc = validate_virtual_disk(&gc, disk->pdev_path, disk); 
     if (rc)
         return rc;
 
@@ -1035,7 +1042,7 @@ int libxl_device_disk_add(libxl_ctx *ctx
         flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
     }
 
-    libxl__device_generic_add(ctx, &device,
+    libxl__device_generic_add(&gc, &device,
                              libxl__xs_kvs_of_flexarray(&gc, back, 
back->count),
                              libxl__xs_kvs_of_flexarray(&gc, front, 
front->count));
 
@@ -1049,7 +1056,7 @@ out:
     return rc;
 }
 
-int libxl_device_disk_del(libxl_ctx *ctx, 
+int libxl_device_disk_del(libxl_ctx *ctx,
                           libxl_device_disk *disk, int wait)
 {
     libxl__gc gc = LIBXL_INIT_GC(ctx);
@@ -1059,12 +1066,12 @@ int libxl_device_disk_del(libxl_ctx *ctx
     devid = libxl__device_disk_dev_number(disk->vdev);
     device.backend_domid    = disk->backend_domid;
     device.backend_devid    = devid;
-    device.backend_kind     = 
+    device.backend_kind     =
         (disk->backend == DISK_BACKEND_PHY) ? DEVICE_VBD : DEVICE_TAP;
     device.domid            = disk->domid;
     device.devid            = devid;
     device.kind             = DEVICE_VBD;
-    rc = libxl__device_del(ctx, &device, wait);
+    rc = libxl__device_del(&gc, &device, wait);
     xs_rm(ctx->xsh, XBT_NULL, libxl__device_backend_path(&gc, &device));
     xs_rm(ctx->xsh, XBT_NULL, libxl__device_frontend_path(&gc, &device));
     libxl__free_all(&gc);
@@ -1078,7 +1085,7 @@ char * libxl_device_disk_local_attach(li
     char *ret = NULL;
 
     switch (disk->backend) {
-        case DISK_BACKEND_PHY: 
+        case DISK_BACKEND_PHY:
             if (disk->format != DISK_FORMAT_RAW) {
                 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "physical block device must"
                     " be raw");
@@ -1088,7 +1095,7 @@ char * libxl_device_disk_local_attach(li
                 disk->pdev_path);
             dev = disk->pdev_path;
             break;
-        case DISK_BACKEND_TAP: 
+        case DISK_BACKEND_TAP:
             if (disk->format == DISK_FORMAT_VHD || disk->format == 
DISK_FORMAT_RAW)
             {
                 if (libxl__blktap_enabled(&gc))
@@ -1115,7 +1122,7 @@ char * libxl_device_disk_local_attach(li
                     "type: %d", disk->backend);
                 break;
             }
-        case DISK_BACKEND_QDISK: 
+        case DISK_BACKEND_QDISK:
             if (disk->format != DISK_FORMAT_RAW) {
                 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "cannot locally attach a 
qdisk "
                     "image if the format is not raw");
@@ -1126,7 +1133,7 @@ char * libxl_device_disk_local_attach(li
             dev = disk->pdev_path;
             break;
         case DISK_BACKEND_UNKNOWN:
-        default: 
+        default:
             LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "unrecognized disk backend "
                 "type: %d", disk->backend);
             break;
@@ -1258,7 +1265,7 @@ int libxl_device_nic_add(libxl_ctx *ctx,
         flexarray_append(front, "x86_32-abi"); /* hardcoded ! */
     }
 
-    libxl__device_generic_add(ctx, &device,
+    libxl__device_generic_add(&gc, &device,
                              libxl__xs_kvs_of_flexarray(&gc, back, 
back->count),
                              libxl__xs_kvs_of_flexarray(&gc, front, 
front->count));
 
@@ -1272,10 +1279,12 @@ out:
     return rc;
 }
 
-int libxl_device_nic_del(libxl_ctx *ctx, 
+int libxl_device_nic_del(libxl_ctx *ctx,
                          libxl_device_nic *nic, int wait)
 {
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
     libxl__device device;
+    int rc;
 
     device.backend_devid    = nic->devid;
     device.backend_domid    = nic->backend_domid;
@@ -1284,7 +1293,9 @@ int libxl_device_nic_del(libxl_ctx *ctx,
     device.domid            = nic->domid;
     device.kind             = DEVICE_VIF;
 
-    return libxl__device_del(ctx, &device, wait);
+    rc = libxl__device_del(&gc, &device, wait);
+    libxl__free_all(&gc);
+    return rc;
 }
 
 libxl_nicinfo *libxl_list_nics(libxl_ctx *ctx, uint32_t domid, unsigned int 
*nb)
@@ -1470,7 +1481,7 @@ int libxl_device_net2_add(libxl_ctx *ctx
     flexarray_append(front, "state");
     flexarray_append(front, "1");
 
-    libxl__device_generic_add(ctx, &device,
+    libxl__device_generic_add(&gc, &device,
                              libxl__xs_kvs_of_flexarray(&gc, back, 
back->count),
                              libxl__xs_kvs_of_flexarray(&gc, front, 
front->count));
 
@@ -1547,7 +1558,9 @@ err:
 
 int libxl_device_net2_del(libxl_ctx *ctx, libxl_device_net2 *net2, int wait)
 {
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
     libxl__device device;
+    int rc;
 
     device.backend_devid    = net2->devid;
     device.backend_domid    = net2->backend_domid;
@@ -1556,7 +1569,9 @@ int libxl_device_net2_del(libxl_ctx *ctx
     device.domid            = net2->domid;
     device.kind             = DEVICE_VIF2;
 
-    return libxl__device_del(ctx, &device, wait);
+    rc = libxl__device_del(&gc, &device, wait);
+    libxl__free_all(&gc);
+    return rc;
 }
 
 
@@ -1626,7 +1641,7 @@ int libxl_device_console_add(libxl_ctx *
         flexarray_append(front, LIBXL_XENCONSOLE_PROTOCOL);
     }
 
-    libxl__device_generic_add(ctx, &device,
+    libxl__device_generic_add(&gc, &device,
                              libxl__xs_kvs_of_flexarray(&gc, back, 
back->count),
                              libxl__xs_kvs_of_flexarray(&gc, front, 
front->count));
     rc = 0;
@@ -1685,7 +1700,7 @@ int libxl_device_vkb_add(libxl_ctx *ctx,
     flexarray_append(front, "state");
     flexarray_append(front, libxl__sprintf(&gc, "%d", 1));
 
-    libxl__device_generic_add(ctx, &device,
+    libxl__device_generic_add(&gc, &device,
                              libxl__xs_kvs_of_flexarray(&gc, back, 
back->count),
                              libxl__xs_kvs_of_flexarray(&gc, front, 
front->count));
     rc = 0;
@@ -1707,22 +1722,22 @@ int libxl_device_vkb_hard_shutdown(libxl
     return ERROR_NI;
 }
 
-static unsigned int libxl_append_disk_list_of_type(libxl_ctx *ctx,
-                                                   uint32_t domid,
-                                                   const char *type,
-                                                   libxl_device_disk **disks,
-                                                   unsigned int *ndisks)
+static unsigned int libxl__append_disk_list_of_type(libxl__gc *gc,
+                                                    uint32_t domid,
+                                                    const char *type,
+                                                    libxl_device_disk **disks,
+                                                    unsigned int *ndisks)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     char *be_path = NULL;
     char **dir = NULL;
     unsigned int n = 0, len = 0;
     libxl_device_disk *pdisk = NULL, *pdisk_end = NULL;
     char *physpath_tmp = NULL;
 
-    be_path = libxl__sprintf(&gc, "%s/backend/%s/%d",
-                             libxl__xs_get_dompath(&gc, 0), type, domid);
-    dir = libxl__xs_directory(&gc, XBT_NULL, be_path, &n);
+    be_path = libxl__sprintf(gc, "%s/backend/%s/%d",
+                             libxl__xs_get_dompath(gc, 0), type, domid);
+    dir = libxl__xs_directory(gc, XBT_NULL, be_path, &n);
     if (dir) {
         *disks = realloc(*disks, sizeof (libxl_device_disk) * (*ndisks + n));
         pdisk = *disks + *ndisks;
@@ -1731,40 +1746,41 @@ static unsigned int libxl_append_disk_li
         for (; pdisk < pdisk_end; pdisk++, dir++) {
             pdisk->backend_domid = 0;
             pdisk->domid = domid;
-            physpath_tmp = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, 
"%s/%s/params", be_path, *dir), &len);
+            physpath_tmp = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(gc, 
"%s/%s/params", be_path, *dir), &len);
             if (physpath_tmp && strchr(physpath_tmp, ':')) {
                 pdisk->pdev_path = strdup(strchr(physpath_tmp, ':') + 1);
                 free(physpath_tmp);
             } else {
                 pdisk->pdev_path = physpath_tmp;
             }
-            libxl_string_to_backend(ctx, libxl__xs_read(&gc, XBT_NULL, 
-                libxl__sprintf(&gc, "%s/%s/type", be_path, *dir)), 
+            libxl_string_to_backend(ctx, libxl__xs_read(gc, XBT_NULL, 
+                libxl__sprintf(gc, "%s/%s/type", be_path, *dir)), 
                 &(pdisk->backend));
-            pdisk->vdev = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, 
"%s/%s/dev", be_path, *dir), &len);
-            pdisk->unpluggable = atoi(libxl__xs_read(&gc, XBT_NULL, 
libxl__sprintf(&gc, "%s/%s/removable", be_path, *dir)));
-            if (!strcmp(libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"%s/%s/mode", be_path, *dir)), "w"))
+            pdisk->vdev = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(gc, 
"%s/%s/dev", be_path, *dir), &len);
+            pdisk->unpluggable = atoi(libxl__xs_read(gc, XBT_NULL, 
libxl__sprintf(gc, "%s/%s/removable", be_path, *dir)));
+            if (!strcmp(libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, 
"%s/%s/mode", be_path, *dir)), "w"))
                 pdisk->readwrite = 1;
             else
                 pdisk->readwrite = 0;
-            type = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"%s/device-type", libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"%s/%s/frontend", be_path, *dir))));
+            type = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, 
"%s/device-type", libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, 
"%s/%s/frontend", be_path, *dir))));
             pdisk->is_cdrom = !strcmp(type, "cdrom");
         }
     }
 
-    libxl__free_all(&gc);
     return n;
 }
 
 libxl_device_disk *libxl_device_disk_list(libxl_ctx *ctx, uint32_t domid, int 
*num)
 {
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
     libxl_device_disk *disks = NULL;
     unsigned int ndisks = 0;
 
-    *num = libxl_append_disk_list_of_type(ctx, domid, "vbd", &disks, &ndisks);
-    *num += libxl_append_disk_list_of_type(ctx, domid, "tap", &disks, &ndisks);
-    *num += libxl_append_disk_list_of_type(ctx, domid, "qdisk", &disks, 
&ndisks);
+    *num = libxl__append_disk_list_of_type(&gc, domid, "vbd", &disks, &ndisks);
+    *num += libxl__append_disk_list_of_type(&gc, domid, "tap", &disks, 
&ndisks);
+    *num += libxl__append_disk_list_of_type(&gc, domid, "qdisk", &disks, 
&ndisks);
 
+    libxl__free_all(&gc);
     return disks;
 }
 
@@ -1908,7 +1924,7 @@ int libxl_device_vfb_add(libxl_ctx *ctx,
     flexarray_append_pair(front, "backend-id", libxl__sprintf(&gc, "%d", 
vfb->backend_domid));
     flexarray_append_pair(front, "state", libxl__sprintf(&gc, "%d", 1));
 
-    libxl__device_generic_add(ctx, &device,
+    libxl__device_generic_add(&gc, &device,
                              libxl__xs_kvs_of_flexarray(&gc, back, 
back->count),
                              libxl__xs_kvs_of_flexarray(&gc, front, 
front->count));
     rc = 0;
diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl_bootloader.c
--- a/tools/libxl/libxl_bootloader.c    Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl_bootloader.c    Tue Mar 15 11:22:33 2011 +0000
@@ -271,7 +271,7 @@ out_err:
     return NULL;
 }
 
-static void parse_bootloader_result(libxl_ctx *ctx,
+static void parse_bootloader_result(libxl__gc *gc,
                                     libxl_domain_build_info *info,
                                     const char *o)
 {
@@ -414,7 +414,7 @@ int libxl_run_bootloader(libxl_ctx *ctx,
         goto out_close;
     }
 
-    parse_bootloader_result(ctx, info, blout);
+    parse_bootloader_result(&gc, info, blout);
 
     rc = 0;
 out_close:
diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl_create.c
--- a/tools/libxl/libxl_create.c        Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl_create.c        Tue Mar 15 11:22:33 2011 +0000
@@ -147,44 +147,43 @@ static int init_console_info(libxl_devic
     return 0;
 }
 
-int libxl__domain_build(libxl_ctx *ctx, libxl_domain_build_info *info, 
uint32_t domid, libxl_domain_build_state *state)
+int libxl__domain_build(libxl__gc *gc, libxl_domain_build_info *info, uint32_t 
domid, libxl_domain_build_state *state)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
     char **vments = NULL, **localents = NULL;
     struct timeval start_time;
     int i, ret;
 
-    ret = libxl__build_pre(ctx, domid, info, state);
+    ret = libxl__build_pre(gc, domid, info, state);
     if (ret)
         goto out;
 
     gettimeofday(&start_time, NULL);
 
     if (info->hvm) {
-        ret = libxl__build_hvm(ctx, domid, info, state);
+        ret = libxl__build_hvm(gc, domid, info, state);
         if (ret)
             goto out;
 
-        vments = libxl__calloc(&gc, 7, sizeof(char *));
+        vments = libxl__calloc(gc, 7, sizeof(char *));
         vments[0] = "rtc/timeoffset";
         vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
         vments[2] = "image/ostype";
         vments[3] = "hvm";
         vments[4] = "start_time";
-        vments[5] = libxl__sprintf(&gc, "%lu.%02d", 
start_time.tv_sec,(int)start_time.tv_usec/10000);
+        vments[5] = libxl__sprintf(gc, "%lu.%02d", 
start_time.tv_sec,(int)start_time.tv_usec/10000);
     } else {
-        ret = libxl__build_pv(ctx, domid, info, state);
+        ret = libxl__build_pv(gc, domid, info, state);
         if (ret)
             goto out;
 
-        vments = libxl__calloc(&gc, 11, sizeof(char *));
+        vments = libxl__calloc(gc, 11, sizeof(char *));
         i = 0;
         vments[i++] = "image/ostype";
         vments[i++] = "linux";
         vments[i++] = "image/kernel";
         vments[i++] = (char*) info->kernel.path;
         vments[i++] = "start_time";
-        vments[i++] = libxl__sprintf(&gc, "%lu.%02d", 
start_time.tv_sec,(int)start_time.tv_usec/10000);
+        vments[i++] = libxl__sprintf(gc, "%lu.%02d", 
start_time.tv_sec,(int)start_time.tv_usec/10000);
         if (info->u.pv.ramdisk.path) {
             vments[i++] = "image/ramdisk";
             vments[i++] = (char*) info->u.pv.ramdisk.path;
@@ -194,49 +193,47 @@ int libxl__domain_build(libxl_ctx *ctx, 
             vments[i++] = (char*) info->u.pv.cmdline;
         }
     }
-    ret = libxl__build_post(ctx, domid, info, state, vments, localents);
+    ret = libxl__build_post(gc, domid, info, state, vments, localents);
 out:
-
-    libxl__free_all(&gc);
     return ret;
 }
 
-static int domain_restore(libxl_ctx *ctx, libxl_domain_build_info *info,
-                         uint32_t domid, int fd, libxl_domain_build_state 
*state,
-                         libxl_device_model_info *dm_info)
+static int domain_restore(libxl__gc *gc, libxl_domain_build_info *info,
+                          uint32_t domid, int fd, libxl_domain_build_state 
*state,
+                          libxl_device_model_info *dm_info)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     char **vments = NULL, **localents = NULL;
     struct timeval start_time;
     int i, ret, esave, flags;
 
-    ret = libxl__build_pre(ctx, domid, info, state);
+    ret = libxl__build_pre(gc, domid, info, state);
     if (ret)
         goto out;
 
-    ret = libxl__domain_restore_common(ctx, domid, info, state, fd);
+    ret = libxl__domain_restore_common(gc, domid, info, state, fd);
     if (ret)
         goto out;
 
     gettimeofday(&start_time, NULL);
 
     if (info->hvm) {
-        vments = libxl__calloc(&gc, 7, sizeof(char *));
+        vments = libxl__calloc(gc, 7, sizeof(char *));
         vments[0] = "rtc/timeoffset";
         vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
         vments[2] = "image/ostype";
         vments[3] = "hvm";
         vments[4] = "start_time";
-        vments[5] = libxl__sprintf(&gc, "%lu.%02d", 
start_time.tv_sec,(int)start_time.tv_usec/10000);
+        vments[5] = libxl__sprintf(gc, "%lu.%02d", 
start_time.tv_sec,(int)start_time.tv_usec/10000);
     } else {
-        vments = libxl__calloc(&gc, 11, sizeof(char *));
+        vments = libxl__calloc(gc, 11, sizeof(char *));
         i = 0;
         vments[i++] = "image/ostype";
         vments[i++] = "linux";
         vments[i++] = "image/kernel";
         vments[i++] = (char*) info->kernel.path;
         vments[i++] = "start_time";
-        vments[i++] = libxl__sprintf(&gc, "%lu.%02d", 
start_time.tv_sec,(int)start_time.tv_usec/10000);
+        vments[i++] = libxl__sprintf(gc, "%lu.%02d", 
start_time.tv_sec,(int)start_time.tv_usec/10000);
         if (info->u.pv.ramdisk.path) {
             vments[i++] = "image/ramdisk";
             vments[i++] = (char*) info->u.pv.ramdisk.path;
@@ -246,7 +243,7 @@ static int domain_restore(libxl_ctx *ctx
             vments[i++] = (char*) info->u.pv.cmdline;
         }
     }
-    ret = libxl__build_post(ctx, domid, info, state, vments, localents);
+    ret = libxl__build_post(gc, domid, info, state, vments, localents);
     if (ret)
         goto out;
 
@@ -275,16 +272,15 @@ out:
     }
 
     errno = esave;
-    libxl__free_all(&gc);
     return ret;
 }
 
-int libxl__domain_make(libxl_ctx *ctx, libxl_domain_create_info *info,
+int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info,
                        uint32_t *domid)
  /* on entry, libxl_domid_valid_guest(domid) must be false;
   * on exit (even error exit), domid may be valid and refer to a domain */
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx); /* fixme: should be done by caller */
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int flags, ret, i, rc;
     char *uuid_string;
     char *rw_paths[] = { "control/shutdown", "device", 
"device/suspend/event-channel" , "data"};
@@ -298,7 +294,7 @@ int libxl__domain_make(libxl_ctx *ctx, l
 
     assert(!libxl_domid_valid_guest(*domid));
 
-    uuid_string = libxl__uuid2string(&gc, info->uuid);
+    uuid_string = libxl__uuid2string(gc, info->uuid);
     if (!uuid_string) {
         rc = ERROR_NOMEM;
         goto out;
@@ -326,13 +322,13 @@ int libxl__domain_make(libxl_ctx *ctx, l
         goto out;
     }
 
-    dom_path = libxl__xs_get_dompath(&gc, *domid);
+    dom_path = libxl__xs_get_dompath(gc, *domid);
     if (!dom_path) {
         rc = ERROR_FAIL;
         goto out;
     }
 
-    vm_path = libxl__sprintf(&gc, "/vm/%s", uuid_string);
+    vm_path = libxl__sprintf(gc, "/vm/%s", uuid_string);
     if (!vm_path) {
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "cannot allocate create paths");
         rc = ERROR_FAIL;
@@ -357,31 +353,31 @@ retry_transaction:
     xs_mkdir(ctx->xsh, t, vm_path);
     xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
 
-    xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/vm", dom_path), vm_path, 
strlen(vm_path));
+    xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/vm", dom_path), vm_path, 
strlen(vm_path));
     rc = libxl_domain_rename(ctx, *domid, 0, info->name, t);
     if (rc)
         goto out;
 
     for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
-        char *path = libxl__sprintf(&gc, "%s/%s", dom_path, rw_paths[i]);
+        char *path = libxl__sprintf(gc, "%s/%s", dom_path, rw_paths[i]);
         xs_mkdir(ctx->xsh, t, path);
         xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
     }
     for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
-        char *path = libxl__sprintf(&gc, "%s/%s", dom_path, ro_paths[i]);
+        char *path = libxl__sprintf(gc, "%s/%s", dom_path, ro_paths[i]);
         xs_mkdir(ctx->xsh, t, path);
         xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
     }
 
-    xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/uuid", vm_path), 
uuid_string, strlen(uuid_string));
-    xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/name", vm_path), info->name, 
strlen(info->name));
+    xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/uuid", vm_path), uuid_string, 
strlen(uuid_string));
+    xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/name", vm_path), info->name, 
strlen(info->name));
     if (info->poolname)
-        xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/pool_name", vm_path), 
info->poolname, strlen(info->poolname));
+        xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/pool_name", vm_path), 
info->poolname, strlen(info->poolname));
 
-    libxl__xs_writev(&gc, t, dom_path, info->xsdata);
-    libxl__xs_writev(&gc, t, libxl__sprintf(&gc, "%s/platform", dom_path), 
info->platformdata);
+    libxl__xs_writev(gc, t, dom_path, info->xsdata);
+    libxl__xs_writev(gc, t, libxl__sprintf(gc, "%s/platform", dom_path), 
info->platformdata);
 
-    xs_write(ctx->xsh, t, libxl__sprintf(&gc, 
"%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
+    xs_write(ctx->xsh, t, libxl__sprintf(gc, 
"%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
     if (!xs_transaction_end(ctx->xsh, t, 0)) {
         if (errno == EAGAIN) {
             t = 0;
@@ -397,14 +393,14 @@ retry_transaction:
     rc = 0;
  out:
     if (t) xs_transaction_end(ctx->xsh, t, 1);
-    libxl__free_all(&gc); /* fixme: should be done by caller */
     return rc;
 }
 
-static int do_domain_create(libxl_ctx *ctx, libxl_domain_config *d_config,
+static int do_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
                             libxl_console_ready cb, void *priv,
                             uint32_t *domid_out, int restore_fd)
 {
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     libxl__device_model_starting *dm_starting = 0;
     libxl_device_model_info *dm_info = &d_config->dm_info;
     libxl_domain_build_state state;
@@ -413,7 +409,7 @@ static int do_domain_create(libxl_ctx *c
 
     domid = 0;
 
-    ret = libxl__domain_make(ctx, &d_config->c_info, &domid);
+    ret = libxl__domain_make(gc, &d_config->c_info, &domid);
     if (ret) {
         fprintf(stderr, "cannot make domain: %d\n", ret);
         ret = ERROR_FAIL;
@@ -434,13 +430,13 @@ static int do_domain_create(libxl_ctx *c
     }
 
     if ( restore_fd >= 0 ) {
-        ret = domain_restore(ctx, &d_config->b_info, domid, restore_fd, 
&state, dm_info);
+        ret = domain_restore(gc, &d_config->b_info, domid, restore_fd, &state, 
dm_info);
     } else {
         if (dm_info->saved_state) {
             free(dm_info->saved_state);
             dm_info->saved_state = NULL;
         }
-        ret = libxl__domain_build(ctx, &d_config->b_info, domid, &state);
+        ret = libxl__domain_build(gc, &d_config->b_info, domid, &state);
     }
 
     if (ret) {
@@ -489,7 +485,7 @@ static int do_domain_create(libxl_ctx *c
         libxl_device_console_destroy(&console);
 
         dm_info->domid = domid;
-        ret = libxl__create_device_model(ctx, dm_info,
+        ret = libxl__create_device_model(gc, dm_info,
                                         d_config->disks, d_config->num_disks,
                                         d_config->vifs, d_config->num_vifs,
                                         &dm_starting);
@@ -514,7 +510,7 @@ static int do_domain_create(libxl_ctx *c
             goto error_out;
         console.domid = domid;
 
-        need_qemu = libxl__need_xenpv_qemu(ctx, 1, &console,
+        need_qemu = libxl__need_xenpv_qemu(gc, 1, &console,
                 d_config->num_vfbs, d_config->vfbs,
                 d_config->num_disks, &d_config->disks[0]);
 
@@ -525,11 +521,11 @@ static int do_domain_create(libxl_ctx *c
         libxl_device_console_destroy(&console);
 
         if (need_qemu)
-            libxl__create_xenpv_qemu(ctx, domid, d_config->vfbs, &dm_starting);
+            libxl__create_xenpv_qemu(gc, domid, d_config->vfbs, &dm_starting);
     }
 
     if (dm_starting) {
-        ret = libxl__confirm_device_model_startup(ctx, dm_starting);
+        ret = libxl__confirm_device_model_startup(gc, dm_starting);
         if (ret < 0) {
             fprintf(stderr,"xl: fatal error: %s:%d, rc=%d: 
libxl__confirm_device_model_startup\n",
                     __FILE__,__LINE__, ret);
@@ -538,7 +534,7 @@ static int do_domain_create(libxl_ctx *c
     }
 
     for (i = 0; i < d_config->num_pcidevs; i++)
-        libxl__device_pci_add(ctx, domid, &d_config->pcidevs[i], 1);
+        libxl__device_pci_add(gc, domid, &d_config->pcidevs[i], 1);
 
     if ( cb && (d_config->c_info.hvm || d_config->b_info.u.pv.bootloader )) {
         if ( (*cb)(ctx, domid, priv) )
@@ -554,14 +550,23 @@ error_out:
 
     return ret;
 }
+
 int libxl_domain_create_new(libxl_ctx *ctx, libxl_domain_config *d_config,
                             libxl_console_ready cb, void *priv, uint32_t 
*domid)
 {
-    return do_domain_create(ctx, d_config, cb, priv, domid, -1);
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    int rc;
+    rc = do_domain_create(&gc, d_config, cb, priv, domid, -1);
+    libxl__free_all(&gc);
+    return rc;
 }
 
 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)
 {
-    return do_domain_create(ctx, d_config, cb, priv, domid, restore_fd);
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    int rc;
+    rc = do_domain_create(&gc, d_config, cb, priv, domid, restore_fd);
+    libxl__free_all(&gc);
+    return rc;
 }
diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl_device.c
--- a/tools/libxl/libxl_device.c        Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl_device.c        Tue Mar 15 11:22:33 2011 +0000
@@ -61,10 +61,10 @@ char *libxl__device_backend_path(libxl__
                           device->domid, device->devid);
 }
 
-int libxl__device_generic_add(libxl_ctx *ctx, libxl__device *device,
+int libxl__device_generic_add(libxl__gc *gc, libxl__device *device,
                              char **bents, char **fents)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     char *frontend_path, *backend_path;
     xs_transaction_t t;
     struct xs_permissions frontend_perms[2];
@@ -76,8 +76,8 @@ int libxl__device_generic_add(libxl_ctx 
         goto out;
     }
 
-    frontend_path = libxl__device_frontend_path(&gc, device);
-    backend_path = libxl__device_backend_path(&gc, device);
+    frontend_path = libxl__device_frontend_path(gc, device);
+    backend_path = libxl__device_backend_path(gc, device);
 
     frontend_perms[0].id = device->domid;
     frontend_perms[0].perms = XS_PERM_NONE;
@@ -97,16 +97,16 @@ retry_transaction:
         xs_rm(ctx->xsh, t, frontend_path);
         xs_mkdir(ctx->xsh, t, frontend_path);
         xs_set_permissions(ctx->xsh, t, frontend_path, frontend_perms, 
ARRAY_SIZE(frontend_perms));
-        xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/backend", 
frontend_path), backend_path, strlen(backend_path));
-        libxl__xs_writev(&gc, t, frontend_path, fents);
+        xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/backend", frontend_path), 
backend_path, strlen(backend_path));
+        libxl__xs_writev(gc, t, frontend_path, fents);
     }
 
     if (bents) {
         xs_rm(ctx->xsh, t, backend_path);
         xs_mkdir(ctx->xsh, t, backend_path);
         xs_set_permissions(ctx->xsh, t, backend_path, backend_perms, 
ARRAY_SIZE(backend_perms));
-        xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/frontend", 
backend_path), frontend_path, strlen(frontend_path));
-        libxl__xs_writev(&gc, t, backend_path, bents);
+        xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/frontend", backend_path), 
frontend_path, strlen(frontend_path));
+        libxl__xs_writev(gc, t, backend_path, bents);
     }
 
     if (!xs_transaction_end(ctx->xsh, t, 0)) {
@@ -117,7 +117,6 @@ retry_transaction:
     }
     rc = 0;
 out:
-    libxl__free_all(&gc);
     return rc;
 }
 
@@ -239,12 +238,12 @@ int libxl__device_disk_dev_number(char *
     return -1;
 }
 
-int libxl__device_destroy(libxl_ctx *ctx, char *be_path, int force)
+int libxl__device_destroy(libxl__gc *gc, char *be_path, int force)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     xs_transaction_t t;
-    char *state_path = libxl__sprintf(&gc, "%s/state", be_path);
-    char *state = libxl__xs_read(&gc, XBT_NULL, state_path);
+    char *state_path = libxl__sprintf(gc, "%s/state", be_path);
+    char *state = libxl__xs_read(gc, XBT_NULL, state_path);
     int rc = 0;
 
     if (!state)
@@ -256,7 +255,7 @@ int libxl__device_destroy(libxl_ctx *ctx
 
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
-    xs_write(ctx->xsh, t, libxl__sprintf(&gc, "%s/online", be_path), "0", 
strlen("0"));
+    xs_write(ctx->xsh, t, libxl__sprintf(gc, "%s/online", be_path), "0", 
strlen("0"));
     xs_write(ctx->xsh, t, state_path, "5", strlen("5"));
     if (!xs_transaction_end(ctx->xsh, t, 0)) {
         if (errno == EAGAIN)
@@ -271,13 +270,12 @@ retry_transaction:
         rc = 1;
     }
 out:
-    libxl__free_all(&gc);
     return rc;
 }
 
-static int wait_for_dev_destroy(libxl_ctx *ctx, struct timeval *tv)
+static int wait_for_dev_destroy(libxl__gc *gc, struct timeval *tv)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int nfds, rc;
     unsigned int n;
     fd_set rfds;
@@ -290,7 +288,7 @@ static int wait_for_dev_destroy(libxl_ct
     if (select(nfds, &rfds, NULL, NULL, tv) > 0) {
         l1 = xs_read_watch(ctx->xsh, &n);
         if (l1 != NULL) {
-            char *state = libxl__xs_read(&gc, XBT_NULL, l1[XS_WATCH_PATH]);
+            char *state = libxl__xs_read(gc, XBT_NULL, l1[XS_WATCH_PATH]);
             if (!state || atoi(state) == 6) {
                 xs_unwatch(ctx->xsh, l1[0], l1[1]);
                 xs_rm(ctx->xsh, XBT_NULL, l1[XS_WATCH_TOKEN]);
@@ -300,13 +298,12 @@ static int wait_for_dev_destroy(libxl_ct
             free(l1);
         }
     }
-    libxl__free_all(&gc);
     return rc;
 }
 
-int libxl__devices_destroy(libxl_ctx *ctx, uint32_t domid, int force)
+int libxl__devices_destroy(libxl__gc *gc, uint32_t domid, int force)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     char *path, *be_path, *fe_path;
     unsigned int num1, num2;
     char **l1 = NULL, **l2 = NULL;
@@ -314,8 +311,8 @@ int libxl__devices_destroy(libxl_ctx *ct
     flexarray_t *toremove;
 
     toremove = flexarray_make(16, 1);
-    path = libxl__sprintf(&gc, "/local/domain/%d/device", domid);
-    l1 = libxl__xs_directory(&gc, XBT_NULL, path, &num1);
+    path = libxl__sprintf(gc, "/local/domain/%d/device", domid);
+    l1 = libxl__xs_directory(gc, XBT_NULL, path, &num1);
     if (!l1) {
         if (errno != ENOENT) {
             LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "unable to get xenstore"
@@ -327,17 +324,17 @@ int libxl__devices_destroy(libxl_ctx *ct
     for (i = 0; i < num1; i++) {
         if (!strcmp("vfs", l1[i]))
             continue;
-        path = libxl__sprintf(&gc, "/local/domain/%d/device/%s", domid, l1[i]);
-        l2 = libxl__xs_directory(&gc, XBT_NULL, path, &num2);
+        path = libxl__sprintf(gc, "/local/domain/%d/device/%s", domid, l1[i]);
+        l2 = libxl__xs_directory(gc, XBT_NULL, path, &num2);
         if (!l2)
             continue;
         for (j = 0; j < num2; j++) {
-            fe_path = libxl__sprintf(&gc, "/local/domain/%d/device/%s/%s", 
domid, l1[i], l2[j]);
-            be_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"%s/backend", fe_path));
+            fe_path = libxl__sprintf(gc, "/local/domain/%d/device/%s/%s", 
domid, l1[i], l2[j]);
+            be_path = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, 
"%s/backend", fe_path));
             if (be_path != NULL) {
-                if (libxl__device_destroy(ctx, be_path, force) > 0)
+                if (libxl__device_destroy(gc, be_path, force) > 0)
                     n_watches++;
-                flexarray_set(toremove, n++, libxl__dirname(&gc, be_path));
+                flexarray_set(toremove, n++, libxl__dirname(gc, be_path));
             } else {
                 xs_rm(ctx->xsh, XBT_NULL, path);
             }
@@ -345,12 +342,12 @@ int libxl__devices_destroy(libxl_ctx *ct
     }
 
     /* console 0 frontend directory is not under /local/domain/<domid>/device 
*/
-    fe_path = libxl__sprintf(&gc, "/local/domain/%d/console", domid);
-    be_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/backend", 
fe_path));
+    fe_path = libxl__sprintf(gc, "/local/domain/%d/console", domid);
+    be_path = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/backend", 
fe_path));
     if (be_path && strcmp(be_path, "")) {
-        if (libxl__device_destroy(ctx, be_path, force) > 0)
+        if (libxl__device_destroy(gc, be_path, force) > 0)
             n_watches++;
-        flexarray_set(toremove, n++, libxl__dirname(&gc, be_path));
+        flexarray_set(toremove, n++, libxl__dirname(gc, be_path));
     }
 
     if (!force) {
@@ -363,7 +360,7 @@ int libxl__devices_destroy(libxl_ctx *ct
         tv.tv_sec = LIBXL_DESTROY_TIMEOUT;
         tv.tv_usec = 0;
         while (n_watches > 0) {
-            if (wait_for_dev_destroy(ctx, &tv)) {
+            if (wait_for_dev_destroy(gc, &tv)) {
                 break;
             } else {
                 n_watches--;
@@ -376,19 +373,17 @@ int libxl__devices_destroy(libxl_ctx *ct
     }
 out:
     flexarray_free(toremove);
-    libxl__free_all(&gc);
     return 0;
 }
 
-int libxl__device_del(libxl_ctx *ctx, libxl__device *dev, int wait)
+int libxl__device_del(libxl__gc *gc, libxl__device *dev, int wait)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
     char *backend_path;
     int rc;
 
-    backend_path = libxl__device_backend_path(&gc, dev);
+    backend_path = libxl__device_backend_path(gc, dev);
 
-    rc = libxl__device_destroy(ctx, backend_path, !wait);
+    rc = libxl__device_destroy(gc, backend_path, !wait);
     if (rc == -1) {
         rc = ERROR_FAIL;
         goto out;
@@ -398,25 +393,24 @@ int libxl__device_del(libxl_ctx *ctx, li
         struct timeval tv;
         tv.tv_sec = LIBXL_DESTROY_TIMEOUT;
         tv.tv_usec = 0;
-        (void)wait_for_dev_destroy(ctx, &tv);
+        (void)wait_for_dev_destroy(gc, &tv);
     }
 
     rc = 0;
 
 out:
-    libxl__free_all(&gc);
     return rc;
 }
 
-int libxl__wait_for_device_model(libxl_ctx *ctx,
-                                uint32_t domid, char *state,
-                                int (*check_callback)(libxl_ctx *ctx,
-                                                      uint32_t domid,
-                                                      const char *state,
-                                                      void *userdata),
-                                void *check_callback_userdata)
+int libxl__wait_for_device_model(libxl__gc *gc,
+                                 uint32_t domid, char *state,
+                                 int (*check_callback)(libxl__gc *gc,
+                                                       uint32_t domid,
+                                                       const char *state,
+                                                       void *userdata),
+                                 void *check_callback_userdata)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     char *path;
     char *p;
     unsigned int len;
@@ -434,7 +428,7 @@ int libxl__wait_for_device_model(libxl_c
         goto err;
     }
 
-    path = libxl__sprintf(&gc, "/local/domain/0/device-model/%d/state", domid);
+    path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", domid);
     xs_watch(xsh, path, path);
     tv.tv_sec = LIBXL_DEVICE_MODEL_START_TIMEOUT;
     tv.tv_usec = 0;
@@ -448,7 +442,7 @@ int libxl__wait_for_device_model(libxl_c
             goto again;
 
         if ( NULL != check_callback ) {
-            rc = (*check_callback)(ctx, domid, p, check_callback_userdata);
+            rc = (*check_callback)(gc, domid, p, check_callback_userdata);
             if ( rc > 0 )
                 goto again;
         }
@@ -456,7 +450,6 @@ int libxl__wait_for_device_model(libxl_c
         free(p);
         xs_unwatch(xsh, path, path);
         xs_daemon_close(xsh);
-        libxl__free_all(&gc);
         return rc;
 again:
         free(p);
@@ -475,17 +468,16 @@ again:
     xs_daemon_close(xsh);
     LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Device Model not ready");
 err:
-    libxl__free_all(&gc);
     return -1;
 }
 
-int libxl__wait_for_backend(libxl_ctx *ctx, char *be_path, char *state)
+int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int watchdog = 100;
     unsigned int len;
     char *p;
-    char *path = libxl__sprintf(&gc, "%s/state", be_path);
+    char *path = libxl__sprintf(gc, "%s/state", be_path);
     int rc = -1;
 
     while (watchdog > 0) {
@@ -511,7 +503,6 @@ int libxl__wait_for_backend(libxl_ctx *c
     }
     LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Backend %s not ready", be_path);
 out:
-    libxl__free_all(&gc);
     return rc;
 }
 
diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl_dm.c
--- a/tools/libxl/libxl_dm.c    Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl_dm.c    Tue Mar 15 11:22:33 2011 +0000
@@ -38,10 +38,10 @@ static const char *libxl_tapif_script(li
 #endif
 }
 
-static char ** libxl_build_device_model_args_old(libxl__gc *gc,
-                                             libxl_device_model_info *info,
-                                             libxl_device_nic *vifs,
-                                             int num_vifs)
+static char ** libxl__build_device_model_args_old(libxl__gc *gc,
+                                                  libxl_device_model_info 
*info,
+                                                  libxl_device_nic *vifs,
+                                                  int num_vifs)
 {
     int i;
     flexarray_t *dm_args;
@@ -170,10 +170,10 @@ static char ** libxl_build_device_model_
     return (char **) flexarray_contents(dm_args);
 }
 
-static char ** libxl_build_device_model_args_new(libxl__gc *gc,
-                                             libxl_device_model_info *info,
-                                             libxl_device_nic *vifs,
-                                             int num_vifs)
+static char ** libxl__build_device_model_args_new(libxl__gc *gc,
+                                                  libxl_device_model_info 
*info,
+                                                  libxl_device_nic *vifs,
+                                                  int num_vifs)
 {
     flexarray_t *dm_args;
     libxl_device_disk *disks;
@@ -329,10 +329,10 @@ static char ** libxl_build_device_model_
     return (char **) flexarray_contents(dm_args);
 }
 
-static char ** libxl_build_device_model_args(libxl__gc *gc,
-                                             libxl_device_model_info *info,
-                                             libxl_device_nic *vifs,
-                                             int num_vifs)
+static char ** libxl__build_device_model_args(libxl__gc *gc,
+                                              libxl_device_model_info *info,
+                                              libxl_device_nic *vifs,
+                                              int num_vifs)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     int new_qemu;
@@ -340,9 +340,9 @@ static char ** libxl_build_device_model_
     new_qemu = libxl_check_device_model_version(ctx, info->device_model);
 
     if (new_qemu == 1) {
-        return libxl_build_device_model_args_new(gc, info, vifs, num_vifs);
+        return libxl__build_device_model_args_new(gc, info, vifs, num_vifs);
     } else {
-        return libxl_build_device_model_args_old(gc, info, vifs, num_vifs);
+        return libxl__build_device_model_args_old(gc, info, vifs, num_vifs);
     }
 }
 
@@ -371,10 +371,10 @@ out:
     free(pid);
 }
 
-static int libxl_vfb_and_vkb_from_device_model_info(libxl_ctx *ctx,
-                                                    libxl_device_model_info 
*info,
-                                                    libxl_device_vfb *vfb,
-                                                    libxl_device_vkb *vkb)
+static int libxl__vfb_and_vkb_from_device_model_info(libxl__gc *gc,
+                                                     libxl_device_model_info 
*info,
+                                                     libxl_device_vfb *vfb,
+                                                     libxl_device_vkb *vkb)
 {
     memset(vfb, 0x00, sizeof(libxl_device_vfb));
     memset(vkb, 0x00, sizeof(libxl_device_vkb));
@@ -395,9 +395,9 @@ static int libxl_vfb_and_vkb_from_device
     return 0;
 }
 
-static int libxl_write_dmargs(libxl_ctx *ctx, int domid, int guest_domid, char 
**args)
+static int libxl__write_dmargs(libxl__gc *gc, int domid, int guest_domid, char 
**args)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int i;
     char *vm_path;
     char *dmargs, *path;
@@ -410,7 +410,7 @@ static int libxl_write_dmargs(libxl_ctx 
     roperm[1].id = domid;
     roperm[1].perms = XS_PERM_READ;
 
-    vm_path = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"/local/domain/%d/vm", guest_domid));
+    vm_path = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, 
"/local/domain/%d/vm", guest_domid));
 
     i = 0;
     dmargs_size = 0;
@@ -429,30 +429,29 @@ static int libxl_write_dmargs(libxl_ctx 
         }
         i++;
     }
-    path = libxl__sprintf(&gc, "%s/image/dmargs", vm_path);
+    path = libxl__sprintf(gc, "%s/image/dmargs", vm_path);
 
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
     xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
     xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
-    xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, "%s/rtc/timeoffset", 
vm_path), roperm, ARRAY_SIZE(roperm));
+    xs_set_permissions(ctx->xsh, t, libxl__sprintf(gc, "%s/rtc/timeoffset", 
vm_path), roperm, ARRAY_SIZE(roperm));
     if (!xs_transaction_end(ctx->xsh, t, 0))
         if (errno == EAGAIN)
             goto retry_transaction;
     free(dmargs);
-    libxl__free_all(&gc);
     return 0;
 }
 
-static int libxl_create_stubdom(libxl_ctx *ctx,
-                                libxl_device_model_info *info,
-                                libxl_device_disk *disks, int num_disks,
-                                libxl_device_nic *vifs, int num_vifs,
-                                libxl_device_vfb *vfb,
-                                libxl_device_vkb *vkb,
-                                libxl__device_model_starting **starting_r)
+static int libxl__create_stubdom(libxl__gc *gc,
+                                 libxl_device_model_info *info,
+                                 libxl_device_disk *disks, int num_disks,
+                                 libxl_device_nic *vifs, int num_vifs,
+                                 libxl_device_vfb *vfb,
+                                 libxl_device_vkb *vkb,
+                                 libxl__device_model_starting **starting_r)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int i, num_console = STUBDOM_SPECIAL_CONSOLES, ret;
     libxl_device_console *console;
     libxl_domain_create_info c_info;
@@ -464,7 +463,7 @@ static int libxl_create_stubdom(libxl_ct
     xs_transaction_t t;
     libxl__device_model_starting *dm_starting = 0;
 
-    args = libxl_build_device_model_args(&gc, info, vifs, num_vifs);
+    args = libxl__build_device_model_args(gc, info, vifs, num_vifs);
     if (!args) {
         ret = ERROR_FAIL;
         goto out;
@@ -472,7 +471,7 @@ static int libxl_create_stubdom(libxl_ct
 
     memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
     c_info.hvm = 0;
-    c_info.name = libxl__sprintf(&gc, "%s-dm", libxl__domid_to_name(&gc, 
info->domid));
+    c_info.name = libxl__sprintf(gc, "%s-dm", libxl__domid_to_name(gc, 
info->domid));
 
     libxl_uuid_copy(&c_info.uuid, &info->uuid);
 
@@ -480,27 +479,27 @@ static int libxl_create_stubdom(libxl_ct
     b_info.max_vcpus = 1;
     b_info.max_memkb = 32 * 1024;
     b_info.target_memkb = b_info.max_memkb;
-    b_info.kernel.path = libxl__abs_path(&gc, "ioemu-stubdom.gz", 
libxl_xenfirmwaredir_path());
-    b_info.u.pv.cmdline = libxl__sprintf(&gc, " -d %d", info->domid);
+    b_info.kernel.path = libxl__abs_path(gc, "ioemu-stubdom.gz", 
libxl_xenfirmwaredir_path());
+    b_info.u.pv.cmdline = libxl__sprintf(gc, " -d %d", info->domid);
     b_info.u.pv.ramdisk.path = "";
     b_info.u.pv.features = "";
     b_info.hvm = 0;
 
     /* fixme: this function can leak the stubdom if it fails */
 
-    ret = libxl__domain_make(ctx, &c_info, &domid);
+    ret = libxl__domain_make(gc, &c_info, &domid);
     if (ret)
         goto out_free;
-    ret = libxl__domain_build(ctx, &b_info, domid, &state);
+    ret = libxl__domain_build(gc, &b_info, domid, &state);
     if (ret)
         goto out_free;
 
-    libxl_write_dmargs(ctx, domid, info->domid, args);
-    libxl__xs_write(&gc, XBT_NULL,
-                   libxl__sprintf(&gc, "%s/image/device-model-domid", 
libxl__xs_get_dompath(&gc, info->domid)),
+    libxl__write_dmargs(gc, domid, info->domid, args);
+    libxl__xs_write(gc, XBT_NULL,
+                   libxl__sprintf(gc, "%s/image/device-model-domid", 
libxl__xs_get_dompath(gc, info->domid)),
                    "%d", domid);
-    libxl__xs_write(&gc, XBT_NULL,
-                   libxl__sprintf(&gc, "%s/target", libxl__xs_get_dompath(&gc, 
domid)),
+    libxl__xs_write(gc, XBT_NULL,
+                   libxl__sprintf(gc, "%s/target", libxl__xs_get_dompath(gc, 
domid)),
                    "%d", info->domid);
     ret = xc_domain_set_target(ctx->xch, domid, info->domid);
     if (ret<0) {
@@ -516,10 +515,10 @@ static int libxl_create_stubdom(libxl_ct
     perm[1].perms = XS_PERM_READ;
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
-    xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, 
"/local/domain/0/device-model/%d", info->domid));
-    xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, 
"/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
-    xs_mkdir(ctx->xsh, t, libxl__sprintf(&gc, "/local/domain/%d/device/vfs", 
domid));
-    xs_set_permissions(ctx->xsh, t, libxl__sprintf(&gc, 
"/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
+    xs_mkdir(ctx->xsh, t, libxl__sprintf(gc, 
"/local/domain/0/device-model/%d", info->domid));
+    xs_set_permissions(ctx->xsh, t, libxl__sprintf(gc, 
"/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
+    xs_mkdir(ctx->xsh, t, libxl__sprintf(gc, "/local/domain/%d/device/vfs", 
domid));
+    xs_set_permissions(ctx->xsh, t, libxl__sprintf(gc, 
"/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
     if (!xs_transaction_end(ctx->xsh, t, 0))
         if (errno == EAGAIN)
             goto retry_transaction;
@@ -548,7 +547,7 @@ retry_transaction:
     if (info->serial)
         num_console++;
 
-    console = libxl__calloc(&gc, num_console, sizeof(libxl_device_console));
+    console = libxl__calloc(gc, num_console, sizeof(libxl_device_console));
     if (!console) {
         ret = ERROR_NOMEM;
         goto out_free;
@@ -566,18 +565,18 @@ retry_transaction:
             char *filename;
             char *name;
             case STUBDOM_CONSOLE_LOGGING:
-                name = libxl__sprintf(&gc, "qemu-dm-%s", 
libxl_domid_to_name(ctx, info->domid));
+                name = libxl__sprintf(gc, "qemu-dm-%s", 
libxl_domid_to_name(ctx, info->domid));
                 libxl_create_logfile(ctx, name, &filename);
-                console[i].output = libxl__sprintf(&gc, "file:%s", filename);
+                console[i].output = libxl__sprintf(gc, "file:%s", filename);
                 console[i].build_state = &state;
                 free(filename);
                 break;
             case STUBDOM_CONSOLE_SAVE:
-                console[i].output = libxl__sprintf(&gc, "file:"SAVEFILE".%d", 
info->domid);
+                console[i].output = libxl__sprintf(gc, "file:"SAVEFILE".%d", 
info->domid);
                 break;
             case STUBDOM_CONSOLE_RESTORE:
                 if (info->saved_state)
-                    console[i].output = libxl__sprintf(&gc, "pipe:%s", 
info->saved_state);
+                    console[i].output = libxl__sprintf(gc, "pipe:%s", 
info->saved_state);
                 break;
             default:
                 console[i].output = "pty";
@@ -587,11 +586,11 @@ retry_transaction:
         if (ret)
             goto out_free;
     }
-    if (libxl__create_xenpv_qemu(ctx, domid, vfb, &dm_starting) < 0) {
+    if (libxl__create_xenpv_qemu(gc, domid, vfb, &dm_starting) < 0) {
         ret = ERROR_FAIL;
         goto out_free;
     }
-    if (libxl__confirm_device_model_startup(ctx, dm_starting) < 0) {
+    if (libxl__confirm_device_model_startup(gc, dm_starting) < 0) {
         ret = ERROR_FAIL;
         goto out_free;
     }
@@ -601,7 +600,7 @@ retry_transaction:
     if (starting_r) {
         *starting_r = calloc(sizeof(libxl__device_model_starting), 1);
         (*starting_r)->domid = info->domid;
-        (*starting_r)->dom_path = libxl__xs_get_dompath(&gc, info->domid);
+        (*starting_r)->dom_path = libxl__xs_get_dompath(gc, info->domid);
         (*starting_r)->for_spawn = NULL;
     }
 
@@ -610,17 +609,16 @@ out_free:
 out_free:
     free(args);
 out:
-    libxl__free_all(&gc);
     return ret;
 }
 
-int libxl__create_device_model(libxl_ctx *ctx,
+int libxl__create_device_model(libxl__gc *gc,
                               libxl_device_model_info *info,
                               libxl_device_disk *disks, int num_disks,
                               libxl_device_nic *vifs, int num_vifs,
                               libxl__device_model_starting **starting_r)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     char *path, *logfile;
     int logfile_w, null;
     int rc;
@@ -634,22 +632,22 @@ int libxl__create_device_model(libxl_ctx
         libxl_device_vfb vfb;
         libxl_device_vkb vkb;
 
-        libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
-        rc = libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, 
&vfb, &vkb, starting_r);
+        libxl__vfb_and_vkb_from_device_model_info(gc, info, &vfb, &vkb);
+        rc = libxl__create_stubdom(gc, info, disks, num_disks, vifs, num_vifs, 
&vfb, &vkb, starting_r);
         goto out;
     }
 
-    args = libxl_build_device_model_args(&gc, info, vifs, num_vifs);
+    args = libxl__build_device_model_args(gc, info, vifs, num_vifs);
     if (!args) {
         rc = ERROR_FAIL;
         goto out;
     }
 
-    path = libxl__sprintf(&gc, "/local/domain/0/device-model/%d", info->domid);
+    path = libxl__sprintf(gc, "/local/domain/0/device-model/%d", info->domid);
     xs_mkdir(ctx->xsh, XBT_NULL, path);
-    libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, "%s/disable_pf", path), 
"%d", !info->xen_platform_pci);
+    libxl__xs_write(gc, XBT_NULL, libxl__sprintf(gc, "%s/disable_pf", path), 
"%d", !info->xen_platform_pci);
 
-    libxl_create_logfile(ctx, libxl__sprintf(&gc, "qemu-dm-%s", 
info->dom_name), &logfile);
+    libxl_create_logfile(ctx, libxl__sprintf(gc, "qemu-dm-%s", 
info->dom_name), &logfile);
     logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
     free(logfile);
     null = open("/dev/null", O_RDONLY);
@@ -667,7 +665,7 @@ int libxl__create_device_model(libxl_ctx
     }
 
     p->domid = info->domid;
-    p->dom_path = libxl__xs_get_dompath(&gc, info->domid);
+    p->dom_path = libxl__xs_get_dompath(gc, info->domid);
     if (!p->dom_path) {
         rc = ERROR_FAIL;
         goto out_close;
@@ -677,26 +675,26 @@ retry_transaction:
 retry_transaction:
         /* Find uuid and the write the vnc password to xenstore for qemu. */
         t = xs_transaction_start(ctx->xsh);
-        vm_path = libxl__xs_read(&gc,t,libxl__sprintf(&gc, "%s/vm", 
p->dom_path));
+        vm_path = libxl__xs_read(gc,t,libxl__sprintf(gc, "%s/vm", 
p->dom_path));
         if (vm_path) {
             /* Now write the vncpassword into it. */
-            pass_stuff = libxl__calloc(&gc, 3, sizeof(char *));
+            pass_stuff = libxl__calloc(gc, 3, sizeof(char *));
             pass_stuff[0] = "vncpasswd";
             pass_stuff[1] = info->vncpasswd;
-            libxl__xs_writev(&gc,t,vm_path,pass_stuff);
+            libxl__xs_writev(gc,t,vm_path,pass_stuff);
             if (!xs_transaction_end(ctx->xsh, t, 0))
                 if (errno == EAGAIN)
                     goto retry_transaction;
         }
     }
 
-    rc = libxl__spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
+    rc = libxl__spawn_spawn(gc, p, "device model", dm_xenstore_record_pid);
     if (rc < 0)
         goto out_close;
     if (!rc) { /* inner child */
         setsid();
         libxl__exec(null, logfile_w, logfile_w,
-                   libxl__abs_path(&gc, info->device_model, 
libxl_libexec_path()),
+                   libxl__abs_path(gc, info->device_model, 
libxl_libexec_path()),
                    args);
     }
 
@@ -707,15 +705,14 @@ out_close:
     close(logfile_w);
     free(args);
 out:
-    libxl__free_all(&gc);
     return rc;
 }
 
-static int detach_device_model(libxl_ctx *ctx,
-                              libxl__device_model_starting *starting)
+static int detach_device_model(libxl__gc *gc,
+                               libxl__device_model_starting *starting)
 {
     int rc;
-    rc = libxl__spawn_detach(ctx, starting->for_spawn);
+    rc = libxl__spawn_detach(gc, starting->for_spawn);
     if (starting->for_spawn)
         free(starting->for_spawn);
     free(starting);
@@ -723,24 +720,24 @@ static int detach_device_model(libxl_ctx
 }
 
 
-int libxl__confirm_device_model_startup(libxl_ctx *ctx,
+int libxl__confirm_device_model_startup(libxl__gc *gc,
                                        libxl__device_model_starting *starting)
 {
-    int problem = libxl__wait_for_device_model(ctx, starting->domid, 
"running", NULL, NULL);
+    int problem = libxl__wait_for_device_model(gc, starting->domid, "running", 
NULL, NULL);
     int detach;
     if ( !problem )
-        problem = libxl__spawn_check(ctx, starting->for_spawn);
-    detach = detach_device_model(ctx, starting);
+        problem = libxl__spawn_check(gc, starting->for_spawn);
+    detach = detach_device_model(gc, starting);
     return problem ? problem : detach;
 }
 
-int libxl__destroy_device_model(libxl_ctx *ctx, uint32_t domid)
+int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     char *pid;
     int ret;
 
-    pid = libxl__xs_read(&gc, XBT_NULL, libxl__sprintf(&gc, 
"/local/domain/%d/image/device-model-pid", domid));
+    pid = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, 
"/local/domain/%d/image/device-model-pid", domid));
     if (!pid) {
         int stubdomid = libxl_get_stubdom_id(ctx, domid);
         if (!stubdomid) {
@@ -767,17 +764,16 @@ int libxl__destroy_device_model(libxl_ct
             goto out;
         }
     }
-    xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, 
"/local/domain/0/device-model/%d", domid));
+    xs_rm(ctx->xsh, XBT_NULL, libxl__sprintf(gc, 
"/local/domain/0/device-model/%d", domid));
 
 out:
-    libxl__free_all(&gc);
     return ret;
 }
 
-static int libxl_build_xenpv_qemu_args(libxl__gc *gc,
-                                       uint32_t domid,
-                                       libxl_device_vfb *vfb,
-                                       libxl_device_model_info *info)
+static int libxl__build_xenpv_qemu_args(libxl__gc *gc,
+                                        uint32_t domid,
+                                        libxl_device_vfb *vfb,
+                                        libxl_device_model_info *info)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     memset(info, 0x00, sizeof(libxl_device_model_info));
@@ -803,13 +799,12 @@ static int libxl_build_xenpv_qemu_args(l
     return 0;
 }
 
-int libxl__need_xenpv_qemu(libxl_ctx *ctx,
+int libxl__need_xenpv_qemu(libxl__gc *gc,
         int nr_consoles, libxl_device_console *consoles,
         int nr_vfbs, libxl_device_vfb *vfbs,
         int nr_disks, libxl_device_disk *disks)
 {
     int i, ret = 0;
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
 
     if (nr_consoles > 1) {
         ret = 1;
@@ -834,7 +829,7 @@ int libxl__need_xenpv_qemu(libxl_ctx *ct
             switch (disks[i].backend) {
             case DISK_BACKEND_TAP:
                 if (blktap_enabled == -1)
-                    blktap_enabled = libxl__blktap_enabled(&gc);
+                    blktap_enabled = libxl__blktap_enabled(gc);
                 if (!blktap_enabled) {
                     ret = 1;
                     goto out;
@@ -853,18 +848,15 @@ int libxl__need_xenpv_qemu(libxl_ctx *ct
     }
 
 out:
-    libxl__free_all(&gc);
     return ret;
 }
 
-int libxl__create_xenpv_qemu(libxl_ctx *ctx, uint32_t domid, libxl_device_vfb 
*vfb,
-                            libxl__device_model_starting **starting_r)
+int libxl__create_xenpv_qemu(libxl__gc *gc, uint32_t domid, libxl_device_vfb 
*vfb,
+                             libxl__device_model_starting **starting_r)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
     libxl_device_model_info info;
 
-    libxl_build_xenpv_qemu_args(&gc, domid, vfb, &info);
-    libxl__create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
-    libxl__free_all(&gc);
+    libxl__build_xenpv_qemu_args(gc, domid, vfb, &info);
+    libxl__create_device_model(gc, &info, NULL, 0, NULL, 0, starting_r);
     return 0;
 }
diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl_dom.c
--- a/tools/libxl/libxl_dom.c   Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl_dom.c   Tue Mar 15 11:22:33 2011 +0000
@@ -35,8 +35,9 @@
 #include "libxl.h"
 #include "libxl_internal.h"
 
-int libxl__domain_is_hvm(libxl_ctx *ctx, uint32_t domid)
+int libxl__domain_is_hvm(libxl__gc *gc, uint32_t domid)
 {
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     xc_domaininfo_t info;
     int ret;
 
@@ -48,8 +49,9 @@ int libxl__domain_is_hvm(libxl_ctx *ctx,
     return !!(info.flags & XEN_DOMINF_hvm_guest);
 }
 
-int libxl__domain_shutdown_reason(libxl_ctx *ctx, uint32_t domid)
+int libxl__domain_shutdown_reason(libxl__gc *gc, uint32_t domid)
 {
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     xc_domaininfo_t info;
     int ret;
 
@@ -64,9 +66,10 @@ int libxl__domain_shutdown_reason(libxl_
     return (info.flags >> XEN_DOMINF_shutdownshift) & XEN_DOMINF_shutdownmask;
 }
 
-int libxl__build_pre(libxl_ctx *ctx, uint32_t domid,
+int libxl__build_pre(libxl__gc *gc, uint32_t domid,
               libxl_domain_build_info *info, libxl_domain_build_state *state)
 {
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     xc_domain_max_vcpus(ctx->xch, domid, info->max_vcpus);
     xc_domain_setmaxmem(ctx->xch, domid, info->target_memkb + 
LIBXL_MAXMEM_CONSTANT);
     xc_domain_set_memmap_limit(ctx->xch, domid, 
@@ -87,11 +90,11 @@ int libxl__build_pre(libxl_ctx *ctx, uin
     return 0;
 }
 
-int libxl__build_post(libxl_ctx *ctx, uint32_t domid,
+int libxl__build_post(libxl__gc *gc, uint32_t domid,
                libxl_domain_build_info *info, libxl_domain_build_state *state,
                char **vms_ents, char **local_ents)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     char *dom_path, *vm_path;
     xs_transaction_t t;
     char **ents;
@@ -101,51 +104,50 @@ int libxl__build_post(libxl_ctx *ctx, ui
     if (info->cpuid != NULL)
         libxl_cpuid_set(ctx, domid, info->cpuid);
 
-    ents = libxl__calloc(&gc, 12 + (info->max_vcpus * 2) + 2, sizeof(char *));
+    ents = libxl__calloc(gc, 12 + (info->max_vcpus * 2) + 2, sizeof(char *));
     ents[0] = "memory/static-max";
-    ents[1] = libxl__sprintf(&gc, "%d", info->max_memkb);
+    ents[1] = libxl__sprintf(gc, "%d", info->max_memkb);
     ents[2] = "memory/target";
-    ents[3] = libxl__sprintf(&gc, "%d", info->target_memkb - 
info->video_memkb);
+    ents[3] = libxl__sprintf(gc, "%d", info->target_memkb - info->video_memkb);
     ents[4] = "memory/videoram";
-    ents[5] = libxl__sprintf(&gc, "%d", info->video_memkb);
+    ents[5] = libxl__sprintf(gc, "%d", info->video_memkb);
     ents[6] = "domid";
-    ents[7] = libxl__sprintf(&gc, "%d", domid);
+    ents[7] = libxl__sprintf(gc, "%d", domid);
     ents[8] = "store/port";
-    ents[9] = libxl__sprintf(&gc, "%"PRIu32, state->store_port);
+    ents[9] = libxl__sprintf(gc, "%"PRIu32, state->store_port);
     ents[10] = "store/ring-ref";
-    ents[11] = libxl__sprintf(&gc, "%lu", state->store_mfn);
+    ents[11] = libxl__sprintf(gc, "%lu", state->store_mfn);
     for (i = 0; i < info->max_vcpus; i++) {
-        ents[12+(i*2)]   = libxl__sprintf(&gc, "cpu/%d/availability", i);
+        ents[12+(i*2)]   = libxl__sprintf(gc, "cpu/%d/availability", i);
         ents[12+(i*2)+1] = (i && info->cur_vcpus && !(info->cur_vcpus & (1 << 
i)))
                             ? "offline" : "online";
     }
 
-    dom_path = libxl__xs_get_dompath(&gc, domid);
+    dom_path = libxl__xs_get_dompath(gc, domid);
     if (!dom_path) {
-        libxl__free_all(&gc);
         return ERROR_FAIL;
     }
 
-    vm_path = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(&gc, "%s/vm", 
dom_path), NULL);
+    vm_path = xs_read(ctx->xsh, XBT_NULL, libxl__sprintf(gc, "%s/vm", 
dom_path), NULL);
 retry_transaction:
     t = xs_transaction_start(ctx->xsh);
 
-    libxl__xs_writev(&gc, t, dom_path, ents);
-    libxl__xs_writev(&gc, t, dom_path, local_ents);
-    libxl__xs_writev(&gc, t, vm_path, vms_ents);
+    libxl__xs_writev(gc, t, dom_path, ents);
+    libxl__xs_writev(gc, t, dom_path, local_ents);
+    libxl__xs_writev(gc, t, vm_path, vms_ents);
 
     if (!xs_transaction_end(ctx->xsh, t, 0))
         if (errno == EAGAIN)
             goto retry_transaction;
     xs_introduce_domain(ctx->xsh, domid, state->store_mfn, state->store_port);
     free(vm_path);
-    libxl__free_all(&gc);
     return 0;
 }
 
-int libxl__build_pv(libxl_ctx *ctx, uint32_t domid,
+int libxl__build_pv(libxl__gc *gc, uint32_t domid,
              libxl_domain_build_info *info, libxl_domain_build_state *state)
 {
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     struct xc_dom_image *dom;
     int ret;
     int flags = 0;
@@ -261,10 +263,10 @@ static int hvm_build_set_params(xc_inter
     return 0;
 }
 
-int libxl__build_hvm(libxl_ctx *ctx, uint32_t domid,
+int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
               libxl_domain_build_info *info, libxl_domain_build_state *state)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int ret, rc = ERROR_INVAL;
 
     if (info->kernel.mapped) {
@@ -278,7 +280,7 @@ int libxl__build_hvm(libxl_ctx *ctx, uin
         domid,
         (info->max_memkb - info->video_memkb) / 1024,
         (info->target_memkb - info->video_memkb) / 1024,
-        libxl__abs_path(&gc, (char *)info->kernel.path,
+        libxl__abs_path(gc, (char *)info->kernel.path,
                        libxl_xenfirmwaredir_path()));
     if (ret) {
         LIBXL__LOG_ERRNOVAL(ctx, LIBXL__LOG_ERROR, ret, "hvm building failed");
@@ -292,14 +294,14 @@ int libxl__build_hvm(libxl_ctx *ctx, uin
     }
     rc = 0;
 out:
-    libxl__free_all(&gc);
     return 0;
 }
 
-int libxl__domain_restore_common(libxl_ctx *ctx, uint32_t domid,
+int libxl__domain_restore_common(libxl__gc *gc, uint32_t domid,
                    libxl_domain_build_info *info, libxl_domain_build_state 
*state,
                    int fd)
 {
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     /* read signature */
     int rc;
     rc = xc_domain_restore(ctx->xch, fd, domid,
@@ -459,10 +461,10 @@ static int libxl__domain_suspend_common_
     return 0;
 }
 
-int libxl__domain_suspend_common(libxl_ctx *ctx, uint32_t domid, int fd,
+int libxl__domain_suspend_common(libxl__gc *gc, uint32_t domid, int fd,
                int hvm, int live, int debug)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int flags;
     int port;
     struct save_callbacks callbacks;
@@ -476,7 +478,7 @@ int libxl__domain_suspend_common(libxl_c
     si.domid = domid;
     si.flags = flags;
     si.hvm = hvm;
-    si.gc = &gc;
+    si.gc = gc;
     si.suspend_eventchn = -1;
     si.guest_responded = 0;
 
@@ -518,27 +520,25 @@ int libxl__domain_suspend_common(libxl_c
         xc_evtchn_close(si.xce);
 
 out:
-    libxl__free_all(&gc);
     return rc;
 }
 
-int libxl__domain_save_device_model(libxl_ctx *ctx, uint32_t domid, int fd)
+int libxl__domain_save_device_model(libxl__gc *gc, uint32_t domid, int fd)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int fd2, c;
     char buf[1024];
-    char *filename = libxl__sprintf(&gc, "/var/lib/xen/qemu-save.%d", domid);
+    char *filename = libxl__sprintf(gc, "/var/lib/xen/qemu-save.%d", domid);
     struct stat st;
     uint32_t qemu_state_len;
 
     LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "Saving device model state to %s", 
filename);
-    libxl__xs_write(&gc, XBT_NULL, libxl__sprintf(&gc, 
"/local/domain/0/device-model/%d/command", domid), "save");
-    libxl__wait_for_device_model(ctx, domid, "paused", NULL, NULL);
+    libxl__xs_write(gc, XBT_NULL, libxl__sprintf(gc, 
"/local/domain/0/device-model/%d/command", domid), "save");
+    libxl__wait_for_device_model(gc, domid, "paused", NULL, NULL);
 
     if (stat(filename, &st) < 0)
     {
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Unable to stat qemu save file\n");
-        libxl__free_all(&gc);
         return ERROR_FAIL;
     }
 
@@ -547,36 +547,28 @@ int libxl__domain_save_device_model(libx
 
     c = libxl_write_exactly(ctx, fd, QEMU_SIGNATURE, strlen(QEMU_SIGNATURE),
                             "saved-state file", "qemu signature");
-    if (c) {
-        libxl__free_all(&gc);
+    if (c)
         return c;
-    }
 
     c = libxl_write_exactly(ctx, fd, &qemu_state_len, sizeof(qemu_state_len),
                             "saved-state file", "saved-state length");
-    if (c) {
-        libxl__free_all(&gc);
+    if (c)
         return c;
-    }
 
     fd2 = open(filename, O_RDONLY);
     while ((c = read(fd2, buf, sizeof(buf))) != 0) {
         if (c < 0) {
             if (errno == EINTR)
                 continue;
-            libxl__free_all(&gc);
             return errno;
         }
         c = libxl_write_exactly(
             ctx, fd, buf, c, "saved-state file", "qemu state");
-        if (c) {
-            libxl__free_all(&gc);
+        if (c)
             return c;
-        }
     }
     close(fd2);
     unlink(filename);
-    libxl__free_all(&gc);
     return 0;
 }
 
@@ -614,7 +606,9 @@ static const char *userdata_path(libxl__
     return path;
 }
 
-static int userdata_delete(libxl_ctx *ctx, const char *path) {
+static int userdata_delete(libxl__gc *gc, const char *path)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int r;
     r = unlink(path);
     if (r) {
@@ -624,14 +618,14 @@ static int userdata_delete(libxl_ctx *ct
     return 0;
 }
 
-void libxl__userdata_destroyall(libxl_ctx *ctx, uint32_t domid)
+void libxl__userdata_destroyall(libxl__gc *gc, uint32_t domid)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     const char *pattern;
     glob_t gl;
     int r, i;
 
-    pattern = userdata_path(&gc, domid, "*", "?");
+    pattern = userdata_path(gc, domid, "*", "?");
     if (!pattern)
         goto out;
 
@@ -645,11 +639,11 @@ void libxl__userdata_destroyall(libxl_ct
         LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "glob failed for %s", pattern);
 
     for (i=0; i<gl.gl_pathc; i++) {
-        userdata_delete(ctx, gl.gl_pathv[i]);
+        userdata_delete(gc, gl.gl_pathv[i]);
     }
     globfree(&gl);
 out:
-    libxl__free_all(&gc);
+    return;
 }
 
 int libxl_userdata_store(libxl_ctx *ctx, uint32_t domid,
@@ -671,7 +665,7 @@ int libxl_userdata_store(libxl_ctx *ctx,
     }
 
     if (!datalen) {
-        rc = userdata_delete(ctx, filename);
+        rc = userdata_delete(&gc, filename);
         goto out;
     }
 
diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl_exec.c
--- a/tools/libxl/libxl_exec.c  Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl_exec.c  Tue Mar 15 11:22:33 2011 +0000
@@ -91,12 +91,13 @@ void libxl_report_child_exitstatus(libxl
     }
 }
 
-int libxl__spawn_spawn(libxl_ctx *ctx,
+int libxl__spawn_spawn(libxl__gc *gc,
                       libxl__device_model_starting *starting,
                       const char *what,
                       void (*intermediate_hook)(void *for_spawn,
                                                 pid_t innerchild))
 {
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     pid_t child, got;
     int status;
     pid_t intermediate;
@@ -138,11 +139,12 @@ int libxl__spawn_spawn(libxl_ctx *ctx,
           ? WTERMSIG(status)+128 : -1);
 }
 
-static void report_spawn_intermediate_status(libxl_ctx *ctx,
-                                 libxl__spawn_starting *for_spawn,
-                                 int status)
+static void report_spawn_intermediate_status(libxl__gc *gc,
+                                             libxl__spawn_starting *for_spawn,
+                                             int status)
 {
     if (!WIFEXITED(status)) {
+        libxl_ctx *ctx = libxl__gc_owner(gc);
         char *intermediate_what;
         /* intermediate process did the logging itself if it exited */
         if ( asprintf(&intermediate_what,
@@ -154,9 +156,10 @@ static void report_spawn_intermediate_st
     }
 }
 
-int libxl__spawn_detach(libxl_ctx *ctx,
+int libxl__spawn_detach(libxl__gc *gc,
                        libxl__spawn_starting *for_spawn)
 {
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int r, status;
     pid_t got;
     int rc = 0;
@@ -175,7 +178,7 @@ int libxl__spawn_detach(libxl_ctx *ctx,
         got = call_waitpid(ctx->waitpid_instead, for_spawn->intermediate, 
&status, 0);
         assert(got == for_spawn->intermediate);
         if (!(WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)) {
-            report_spawn_intermediate_status(ctx, for_spawn, status);
+            report_spawn_intermediate_status(gc, for_spawn, status);
             rc = ERROR_FAIL;
         }
         for_spawn->intermediate = 0;
@@ -187,8 +190,9 @@ int libxl__spawn_detach(libxl_ctx *ctx,
     return rc;
 }
 
-int libxl__spawn_check(libxl_ctx *ctx, void *for_spawn_void)
+int libxl__spawn_check(libxl__gc *gc, void *for_spawn_void)
 {
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     libxl__spawn_starting *for_spawn = for_spawn_void;
     pid_t got;
     int status;
@@ -200,7 +204,7 @@ int libxl__spawn_check(libxl_ctx *ctx, v
     if (!got) return 0;
 
     assert(got == for_spawn->intermediate);
-    report_spawn_intermediate_status(ctx, for_spawn, status);
+    report_spawn_intermediate_status(gc, for_spawn, status);
 
     for_spawn->intermediate = 0;
     return ERROR_FAIL;
diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl_internal.c
--- a/tools/libxl/libxl_internal.c      Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl_internal.c      Tue Mar 15 11:22:33 2011 +0000
@@ -29,7 +29,7 @@
 #include "libxl_internal.h"
 #include "libxl_utils.h"
 
-int libxl__error_set(libxl_ctx *ctx, int code)
+int libxl__error_set(libxl__gc *gc, int code)
 {
     return 0;
 }
@@ -82,7 +82,7 @@ void *libxl__zalloc(libxl__gc *gc, int b
 {
     void *ptr = calloc(bytes, 1);
     if (!ptr) {
-        libxl__error_set(libxl__gc_owner(gc), ENOMEM);
+        libxl__error_set(gc, ENOMEM);
         return NULL;
     }
 
@@ -94,7 +94,7 @@ void *libxl__calloc(libxl__gc *gc, size_
 {
     void *ptr = calloc(nmemb, size);
     if (!ptr) {
-        libxl__error_set(libxl__gc_owner(gc), ENOMEM);
+        libxl__error_set(gc, ENOMEM);
         return NULL;
     }
 
diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl_internal.h
--- a/tools/libxl/libxl_internal.h      Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl_internal.h      Tue Mar 15 11:22:33 2011 +0000
@@ -157,25 +157,25 @@ _hidden char **libxl__xs_directory(libxl
    /* On error: returns NULL, sets errno (no logging) */
 
 /* from xl_dom */
-_hidden int libxl__domain_is_hvm(libxl_ctx *ctx, uint32_t domid);
-_hidden int libxl__domain_shutdown_reason(libxl_ctx *ctx, uint32_t domid);
+_hidden int libxl__domain_is_hvm(libxl__gc *gc, uint32_t domid);
+_hidden int libxl__domain_shutdown_reason(libxl__gc *gc, uint32_t domid);
 
-_hidden int libxl__build_pre(libxl_ctx *ctx, uint32_t domid,
+_hidden int libxl__build_pre(libxl__gc *gc, uint32_t domid,
               libxl_domain_build_info *info, libxl_domain_build_state *state);
-_hidden int libxl__build_post(libxl_ctx *ctx, uint32_t domid,
+_hidden int libxl__build_post(libxl__gc *gc, uint32_t domid,
                libxl_domain_build_info *info, libxl_domain_build_state *state,
                char **vms_ents, char **local_ents);
 
-_hidden int libxl__build_pv(libxl_ctx *ctx, uint32_t domid,
+_hidden int libxl__build_pv(libxl__gc *gc, uint32_t domid,
              libxl_domain_build_info *info, libxl_domain_build_state *state);
-_hidden int libxl__build_hvm(libxl_ctx *ctx, uint32_t domid,
+_hidden int libxl__build_hvm(libxl__gc *gc, uint32_t domid,
               libxl_domain_build_info *info, libxl_domain_build_state *state);
 
-_hidden int libxl__domain_restore_common(libxl_ctx *ctx, uint32_t domid,
+_hidden int libxl__domain_restore_common(libxl__gc *gc, uint32_t domid,
                    libxl_domain_build_info *info, libxl_domain_build_state 
*state, int fd);
-_hidden int libxl__domain_suspend_common(libxl_ctx *ctx, uint32_t domid, int 
fd, int hvm, int live, int debug);
-_hidden int libxl__domain_save_device_model(libxl_ctx *ctx, uint32_t domid, 
int fd);
-_hidden void libxl__userdata_destroyall(libxl_ctx *ctx, uint32_t domid);
+_hidden int libxl__domain_suspend_common(libxl__gc *gc, uint32_t domid, int 
fd, int hvm, int live, int debug);
+_hidden int libxl__domain_save_device_model(libxl__gc *gc, uint32_t domid, int 
fd);
+_hidden void libxl__userdata_destroyall(libxl__gc *gc, uint32_t domid);
 
 /* from xl_device */
 _hidden char *libxl__device_disk_string_of_backend(libxl_disk_backend backend);
@@ -184,25 +184,18 @@ _hidden int libxl__device_physdisk_major
 _hidden int libxl__device_physdisk_major_minor(const char *physpath, int 
*major, int *minor);
 _hidden int libxl__device_disk_dev_number(char *virtpath);
 
-_hidden int libxl__device_generic_add(libxl_ctx *ctx, libxl__device *device,
+_hidden int libxl__device_generic_add(libxl__gc *gc, libxl__device *device,
                              char **bents, char **fents);
 _hidden char *libxl__device_backend_path(libxl__gc *gc, libxl__device *device);
 _hidden char *libxl__device_frontend_path(libxl__gc *gc, libxl__device 
*device);
-_hidden int libxl__device_del(libxl_ctx *ctx, libxl__device *dev, int wait);
-_hidden int libxl__device_destroy(libxl_ctx *ctx, char *be_path, int force);
-_hidden int libxl__devices_destroy(libxl_ctx *ctx, uint32_t domid, int force);
-_hidden int libxl__wait_for_device_model(libxl_ctx *ctx,
-                                uint32_t domid, char *state,
-                                int (*check_callback)(libxl_ctx *ctx,
-                                                      uint32_t domid,
-                                                      const char *state,
-                                                      void *userdata),
-                                void *check_callback_userdata);
-_hidden int libxl__wait_for_backend(libxl_ctx *ctx, char *be_path, char 
*state);
+_hidden int libxl__device_del(libxl__gc *gc, libxl__device *dev, int wait);
+_hidden int libxl__device_destroy(libxl__gc *gc, char *be_path, int force);
+_hidden int libxl__devices_destroy(libxl__gc *gc, uint32_t domid, int force);
+_hidden int libxl__wait_for_backend(libxl__gc *gc, char *be_path, char *state);
 
 /* from libxl_pci */
 
-_hidden int libxl__device_pci_add(libxl_ctx *ctx, uint32_t domid, 
libxl_device_pci *pcidev, int starting);
+_hidden int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, 
libxl_device_pci *pcidev, int starting);
 
 /* xl_exec */
 
@@ -222,33 +215,40 @@ typedef struct {
 } libxl__device_model_starting;
 
 /* from xl_create */
-_hidden int libxl__domain_make(libxl_ctx *ctx, libxl_domain_create_info *info, 
uint32_t *domid);
-_hidden int libxl__domain_build(libxl_ctx *ctx, libxl_domain_build_info *info, 
uint32_t domid, /* out */ libxl_domain_build_state *state);
+_hidden int libxl__domain_make(libxl__gc *gc, libxl_domain_create_info *info, 
uint32_t *domid);
+_hidden int libxl__domain_build(libxl__gc *gc, libxl_domain_build_info *info, 
uint32_t domid, /* out */ libxl_domain_build_state *state);
 
 /* for device model creation */
-_hidden int libxl__create_device_model(libxl_ctx *ctx,
+_hidden int libxl__create_device_model(libxl__gc *gc,
                               libxl_device_model_info *info,
                               libxl_device_disk *disk, int num_disks,
                               libxl_device_nic *vifs, int num_vifs,
                               libxl__device_model_starting **starting_r);
-_hidden int libxl__create_xenpv_qemu(libxl_ctx *ctx, uint32_t domid, 
libxl_device_vfb *vfb,
+_hidden int libxl__create_xenpv_qemu(libxl__gc *gc, uint32_t domid, 
libxl_device_vfb *vfb,
                             libxl__device_model_starting **starting_r);
-_hidden int libxl__need_xenpv_qemu(libxl_ctx *ctx,
+_hidden int libxl__need_xenpv_qemu(libxl__gc *gc,
         int nr_consoles, libxl_device_console *consoles,
         int nr_vfbs, libxl_device_vfb *vfbs,
         int nr_disks, libxl_device_disk *disks);
-
   /* Caller must either: pass starting_r==0, or on successful
    * return pass *starting_r (which will be non-0) to
    * libxl_confirm_device_model or libxl_detach_device_model. */
-_hidden int libxl__confirm_device_model_startup(libxl_ctx *ctx,
+_hidden int libxl__confirm_device_model_startup(libxl__gc *gc,
                               libxl__device_model_starting *starting);
+_hidden int libxl__detach_device_model(libxl__gc *gc, 
libxl__device_model_starting *starting);
+_hidden int libxl__wait_for_device_model(libxl__gc *gc,
+                                uint32_t domid, char *state,
+                                int (*check_callback)(libxl__gc *gc,
+                                                      uint32_t domid,
+                                                      const char *state,
+                                                      void *userdata),
+                                void *check_callback_userdata);
 
-_hidden int libxl__spawn_spawn(libxl_ctx *ctx,
+_hidden int libxl__spawn_spawn(libxl__gc *gc,
                       libxl__device_model_starting *starting,
                       const char *what,
                       void (*intermediate_hook)(void *for_spawn, pid_t 
innerchild));
-_hidden int libxl__destroy_device_model(libxl_ctx *ctx, uint32_t domid);
+_hidden int libxl__destroy_device_model(libxl__gc *gc, uint32_t domid);
 
   /* Logs errors.  A copy of "what" is taken.  Return values:
    *  < 0   error, for_spawn need not be detached
@@ -256,11 +256,11 @@ _hidden int libxl__destroy_device_model(
    *    0   caller is now the inner child, should probably call libxl__exec
    * Caller, may pass 0 for for_spawn, in which case no need to detach.
    */
-_hidden int libxl__spawn_detach(libxl_ctx *ctx,
+_hidden int libxl__spawn_detach(libxl__gc *gc,
                        libxl__spawn_starting *for_spawn);
   /* Logs errors.  Idempotent, but only permitted after successful
    * call to libxl__spawn_spawn, and no point calling it again if it fails. */
-_hidden int libxl__spawn_check(libxl_ctx *ctx,
+_hidden int libxl__spawn_check(libxl__gc *gc,
                       void *for_spawn);
   /* Logs errors but also returns them.
    * for_spawn must actually be a  libxl__spawn_starting*  but
@@ -325,7 +325,7 @@ struct libxl__xen_console_reader {
     unsigned int index;
 };
 
-_hidden int libxl__error_set(libxl_ctx *ctx, int code);
+_hidden int libxl__error_set(libxl__gc *gc, int code);
 
 _hidden int libxl__file_reference_map(libxl_file_reference *f);
 _hidden int libxl__file_reference_unmap(libxl_file_reference *f);
diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl_pci.c
--- a/tools/libxl/libxl_pci.c   Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl_pci.c   Tue Mar 15 11:22:33 2011 +0000
@@ -234,7 +234,7 @@ static void libxl_create_pci_backend_dev
     flexarray_append_pair(back, libxl__sprintf(gc, "state-%d", num), 
libxl__sprintf(gc, "%d", 1));
 }
 
-static int libxl_create_pci_backend(libxl__gc *gc, uint32_t domid, 
libxl_device_pci *pcidev, int num)
+static int libxl__create_pci_backend(libxl__gc *gc, uint32_t domid, 
libxl_device_pci *pcidev, int num)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     flexarray_t *front = NULL;
@@ -273,9 +273,9 @@ static int libxl_create_pci_backend(libx
     flexarray_append_pair(front, "backend-id", libxl__sprintf(gc, "%d", 0));
     flexarray_append_pair(front, "state", libxl__sprintf(gc, "%d", 1));
 
-    libxl__device_generic_add(ctx, &device,
-                             libxl__xs_kvs_of_flexarray(gc, back, back->count),
-                             libxl__xs_kvs_of_flexarray(gc, front, 
front->count));
+    libxl__device_generic_add(gc, &device,
+                              libxl__xs_kvs_of_flexarray(gc, back, 
back->count),
+                              libxl__xs_kvs_of_flexarray(gc, front, 
front->count));
 
 out:
     if (back)
@@ -285,7 +285,7 @@ out:
     return 0;
 }
 
-static int libxl_device_pci_add_xenstore(libxl__gc *gc, uint32_t domid, 
libxl_device_pci *pcidev, int starting)
+static int libxl__device_pci_add_xenstore(libxl__gc *gc, uint32_t domid, 
libxl_device_pci *pcidev, int starting)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     flexarray_t *back;
@@ -296,10 +296,10 @@ static int libxl_device_pci_add_xenstore
     be_path = libxl__sprintf(gc, "%s/backend/pci/%d/0", 
libxl__xs_get_dompath(gc, 0), domid);
     num_devs = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, "%s/num_devs", 
be_path));
     if (!num_devs)
-        return libxl_create_pci_backend(gc, domid, pcidev, 1);
+        return libxl__create_pci_backend(gc, domid, pcidev, 1);
 
-    if (!starting && !libxl__domain_is_hvm(ctx, domid)) {
-        if (libxl__wait_for_backend(ctx, be_path, "4") < 0)
+    if (!starting && !libxl__domain_is_hvm(gc, domid)) {
+        if (libxl__wait_for_backend(gc, be_path, "4") < 0)
             return ERROR_FAIL;
     }
 
@@ -326,7 +326,7 @@ retry_transaction:
     return 0;
 }
 
-static int libxl_device_pci_remove_xenstore(libxl__gc *gc, uint32_t domid, 
libxl_device_pci *pcidev)
+static int libxl__device_pci_remove_xenstore(libxl__gc *gc, uint32_t domid, 
libxl_device_pci *pcidev)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     char *be_path, *num_devs_path, *num_devs, *xsdev, *tmp, *tmppath;
@@ -341,8 +341,8 @@ static int libxl_device_pci_remove_xenst
         return ERROR_INVAL;
     num = atoi(num_devs);
 
-    if (!libxl__domain_is_hvm(ctx, domid)) {
-        if (libxl__wait_for_backend(ctx, be_path, "4") < 0) {
+    if (!libxl__domain_is_hvm(gc, domid)) {
+        if (libxl__wait_for_backend(gc, be_path, "4") < 0) {
             LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "pci backend at %s is not 
ready", be_path);
             return ERROR_FAIL;
         }
@@ -369,8 +369,8 @@ retry_transaction:
         if (errno == EAGAIN)
             goto retry_transaction;
 
-    if (!libxl__domain_is_hvm(ctx, domid)) {
-        if (libxl__wait_for_backend(ctx, be_path, "4") < 0) {
+    if (!libxl__domain_is_hvm(gc, domid)) {
+        if (libxl__wait_for_backend(gc, be_path, "4") < 0) {
             LIBXL__LOG(ctx, LIBXL__LOG_DEBUG, "pci backend at %s is not 
ready", be_path);
             return ERROR_FAIL;
         }
@@ -423,7 +423,7 @@ retry_transaction2:
 
     if (num == 1) {
         char *fe_path = libxl__xs_read(gc, XBT_NULL, libxl__sprintf(gc, 
"%s/frontend", be_path));
-        libxl__device_destroy(ctx, be_path, 1);
+        libxl__device_destroy(gc, be_path, 1);
         xs_rm(ctx->xsh, XBT_NULL, be_path);
         xs_rm(ctx->xsh, XBT_NULL, fe_path);
         return 0;
@@ -602,7 +602,7 @@ static int pci_multifunction_check(libxl
     return 0;
 }
 
-static int pci_ins_check(libxl_ctx *ctx, uint32_t domid, const char *state, 
void *priv)
+static int pci_ins_check(libxl__gc *gc, uint32_t domid, const char *state, 
void *priv)
 {
     char *orig_state = priv;
 
@@ -623,9 +623,9 @@ static int do_pci_add(libxl__gc *gc, uin
     char *state, *vdevfn;
     int rc, hvm;
 
-    hvm = libxl__domain_is_hvm(ctx, domid);
+    hvm = libxl__domain_is_hvm(gc, domid);
     if (hvm) {
-        if (libxl__wait_for_device_model(ctx, domid, "running", NULL, NULL) < 
0) {
+        if (libxl__wait_for_device_model(gc, domid, "running", NULL, NULL) < 
0) {
             return ERROR_FAIL;
         }
         path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", 
domid);
@@ -639,7 +639,7 @@ static int do_pci_add(libxl__gc *gc, uin
                            pcidev->bus, pcidev->dev, pcidev->func);
         path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/command", 
domid);
         xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
-        rc = libxl__wait_for_device_model(ctx, domid, NULL, pci_ins_check, 
state);
+        rc = libxl__wait_for_device_model(gc, domid, NULL, pci_ins_check, 
state);
         path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/parameter", 
domid);
         vdevfn = libxl__xs_read(gc, XBT_NULL, path);
         path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", 
domid);
@@ -718,12 +718,12 @@ out:
         }
     }
 
-    rc = libxl_device_pci_add_xenstore(gc, domid, pcidev, starting);
+    rc = libxl__device_pci_add_xenstore(gc, domid, pcidev, starting);
     return rc;
 }
 
-static int libxl_device_pci_reset(libxl__gc *gc, unsigned int domain, unsigned 
int bus,
-                         unsigned int dev, unsigned int func)
+static int libxl__device_pci_reset(libxl__gc *gc, unsigned int domain, 
unsigned int bus,
+                                   unsigned int dev, unsigned int func)
 {
     libxl_ctx *ctx = libxl__gc_owner(gc);
     char *reset;
@@ -760,18 +760,22 @@ static int libxl_device_pci_reset(libxl_
 
 int libxl_device_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci 
*pcidev)
 {
-    return libxl__device_pci_add(ctx, domid, pcidev, 0);
+    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    int rc;
+    rc = libxl__device_pci_add(&gc, domid, pcidev, 0);
+    libxl__free_all(&gc);
+    return rc;
 }
 
-int libxl__device_pci_add(libxl_ctx *ctx, uint32_t domid, libxl_device_pci 
*pcidev, int starting)
+int libxl__device_pci_add(libxl__gc *gc, uint32_t domid, libxl_device_pci 
*pcidev, int starting)
 {
-    libxl__gc gc = LIBXL_INIT_GC(ctx);
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     unsigned int orig_vdev, pfunc_mask;
     libxl_device_pci *assigned;
     int num_assigned, i, rc;
     int stubdomid = 0;
 
-    rc = get_all_assigned_devices(&gc, &assigned, &num_assigned);
+    rc = get_all_assigned_devices(gc, &assigned, &num_assigned);
     if ( rc ) {
         LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "cannot determine if device is 
assigned, refusing to continue");
         goto out;
@@ -783,12 +787,12 @@ int libxl__device_pci_add(libxl_ctx *ctx
         goto out;
     }
 
-    libxl_device_pci_reset(&gc, pcidev->domain, pcidev->bus, pcidev->dev, 
pcidev->func);
+    libxl__device_pci_reset(gc, pcidev->domain, pcidev->bus, pcidev->dev, 
pcidev->func);
 
     stubdomid = libxl_get_stubdom_id(ctx, domid);
     if (stubdomid != 0) {
         libxl_device_pci pcidev_s = *pcidev;
-        rc = do_pci_add(&gc, stubdomid, &pcidev_s, starting);
+        rc = do_pci_add(gc, stubdomid, &pcidev_s, starting);
         if ( rc )
             goto out;
     }
@@ -801,7 +805,7 @@ int libxl__device_pci_add(libxl_ctx *ctx
             rc = ERROR_INVAL;
             goto out;
         }
-        if ( pci_multifunction_check(&gc, pcidev, &pfunc_mask) ) {
+        if ( pci_multifunction_check(gc, pcidev, &pfunc_mask) ) {
             rc = ERROR_FAIL;
             goto out;
         }
@@ -823,13 +827,12 @@ int libxl__device_pci_add(libxl_ctx *ctx
                  */
                 pcidev->vdevfn = orig_vdev;
             }
-            if ( do_pci_add(&gc, domid, pcidev, starting) )
+            if ( do_pci_add(gc, domid, pcidev, starting) )
                 rc = ERROR_FAIL;
         }
     }
 
 out:
-    libxl__free_all(&gc);
     return rc;
 }
 
@@ -851,9 +854,9 @@ static int do_pci_remove(libxl__gc *gc, 
         }
     }
 
-    hvm = libxl__domain_is_hvm(ctx, domid);
+    hvm = libxl__domain_is_hvm(gc, domid);
     if (hvm) {
-        if (libxl__wait_for_device_model(ctx, domid, "running", NULL, NULL) < 
0) {
+        if (libxl__wait_for_device_model(gc, domid, "running", NULL, NULL) < 
0) {
             return ERROR_FAIL;
         }
         path = libxl__sprintf(gc, "/local/domain/0/device-model/%d/state", 
domid);
@@ -867,7 +870,7 @@ static int do_pci_remove(libxl__gc *gc, 
          * device-model for function 0 */
         if ( !force && (pcidev->vdevfn & 0x7) == 0 ) {
             xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
-            if (libxl__wait_for_device_model(ctx, domid, "pci-removed", NULL, 
NULL) < 0) {
+            if (libxl__wait_for_device_model(gc, domid, "pci-removed", NULL, 
NULL) < 0) {
                 LIBXL__LOG(ctx, LIBXL__LOG_ERROR, "Device Model didn't respond 
in time");
                 /* This depends on guest operating system acknowledging the
                  * SCI, if it doesn't respond in time then we may wish to 
@@ -931,7 +934,7 @@ out:
 out:
     /* don't do multiple resets while some functions are still passed through 
*/
     if ( (pcidev->vdevfn & 0x7) == 0 ) {
-        libxl_device_pci_reset(gc, pcidev->domain, pcidev->bus, pcidev->dev, 
pcidev->func);
+        libxl__device_pci_reset(gc, pcidev->domain, pcidev->bus, pcidev->dev, 
pcidev->func);
     }
 
     if (!libxl_is_stubdom(ctx, domid, NULL)) {
@@ -946,7 +949,7 @@ out:
         libxl_device_pci_remove(ctx, stubdomid, &pcidev_s, force);
     }
 
-    libxl_device_pci_remove_xenstore(gc, domid, pcidev);
+    libxl__device_pci_remove_xenstore(gc, domid, pcidev);
 
     return 0;
 }
diff -r 541961e0e156 -r c0fef8dca729 tools/libxl/libxl_utils.c
--- a/tools/libxl/libxl_utils.c Tue Mar 15 10:22:30 2011 +0000
+++ b/tools/libxl/libxl_utils.c Tue Mar 15 11:22:33 2011 +0000
@@ -223,7 +223,9 @@ out:
     return ret;
 }
 
-static int logrename(libxl_ctx *ctx, const char *old, const char *new) {
+static int logrename(libxl__gc *gc, const char *old, const char *new)
+{
+    libxl_ctx *ctx = libxl__gc_owner(gc);
     int r;
 
     r = rename(old, new);
@@ -252,14 +254,14 @@ int libxl_create_logfile(libxl_ctx *ctx,
         for (i = 9; i > 0; i--) {
             logfile = libxl__sprintf(&gc, "/var/log/xen/%s.log.%d", name, i);
             logfile_new = libxl__sprintf(&gc, "/var/log/xen/%s.log.%d", name, 
i + 1);
-            rc = logrename(ctx, logfile, logfile_new);
+            rc = logrename(&gc, logfile, logfile_new);
             if (rc)
                 goto out;
         }
         logfile = libxl__sprintf(&gc, "/var/log/xen/%s.log", name);
         logfile_new = libxl__sprintf(&gc, "/var/log/xen/%s.log.1", name);
 
-        rc = logrename(ctx, logfile, logfile_new);
+        rc = logrename(&gc, logfile, logfile_new);
         if (rc)
             goto out;
     } else {

_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel

<Prev in Thread] Current Thread [Next in Thread>