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

[Xen-devel] [PATCH 2 of 3] libxl: avoid multiple allocations in libxl_uuid2string



# HG changeset patch
# User Ian Campbell <ian.campbell@xxxxxxxxxx>
# Date 1281709509 -3600
# Node ID cf21a4ad0e798f76adb91453769a36e0856ccd12
# Parent  72d92574410da1d388dd33e5fe10e955fd1a93ec
libxl: avoid multiple allocations in libxl_uuid2string

The pointer returned by libxl_uuid2string is the callers
responsibility but the function currently allocates the string into
the current context and then duplicates the result.

Instead of allocating the memory twice (and immediately throwing one
away) just allocate the memory ourselves.

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

diff -r 72d92574410d -r cf21a4ad0e79 tools/libxl/libxl_dom.c
--- a/tools/libxl/libxl_dom.c   Fri Aug 13 15:25:08 2010 +0100
+++ b/tools/libxl/libxl_dom.c   Fri Aug 13 15:25:09 2010 +0100
@@ -444,17 +444,22 @@ int save_device_model(libxl_ctx *ctx, ui
 
 char *libxl_uuid2string(libxl_ctx *ctx, const libxl_uuid uuid)
 {
-    libxl_gc gc = LIBXL_INIT_GC(ctx);
-    char *s = string_of_uuid(&gc, uuid);
-    char *ret;
-    if (!s) {
-        XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate for uuid");
-        ret = NULL;
-    }else{
-        ret = strdup(s);
-    }
-    libxl_free_all(&gc);
-    return ret;
+    char *s;
+    int ret;
+
+    ret = snprintf(NULL, 0, UUID_FMT,
+                   uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], 
uuid[6], uuid[7],
+                   uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], 
uuid[14], uuid[15]);
+
+    if (ret < 0)
+        return NULL;
+
+    s = malloc(ret + 1);
+    snprintf(s, ret + 1, UUID_FMT,
+                   uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5], 
uuid[6], uuid[7],
+                   uuid[8], uuid[9], uuid[10], uuid[11], uuid[12], uuid[13], 
uuid[14], uuid[15]);
+
+    return s;
 }
 
 static const char *userdata_path(libxl_gc *gc, uint32_t domid,

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


 


Rackspace

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