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

[Xen-devel] [PATCH 4/5] use new gc functions to deep copy structure and only free once the gc.



Signed-off-by: Vincent Hanquez <vincent.hanquez@xxxxxxxxxxxxx>
---
 tools/ocaml/libs/xl/xl_stubs.c |   25 +++++++++++++++++++++----
 1 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/tools/ocaml/libs/xl/xl_stubs.c b/tools/ocaml/libs/xl/xl_stubs.c
index 7ba9272..8134e8e 100644
--- a/tools/ocaml/libs/xl/xl_stubs.c
+++ b/tools/ocaml/libs/xl/xl_stubs.c
@@ -63,18 +63,38 @@ void log_destroy(struct xentoollog_logger *logger)
                failwith_xl("cannot init context", &lg);
 
 #define FREE_CTX()  \
+       gc_free(&gc); \
        libxl_ctx_free(&ctx)
 
 static void * gc_calloc(caml_gc *gc, size_t nmemb, size_t size)
 {
        void *ptr;
        ptr = calloc(nmemb, size);
+       if (!ptr)
+               caml_raise_out_of_memory();
+       gc->ptrs[gc->offset++] = ptr;
        return ptr;
 }
 
 static char * dup_String_val(caml_gc *gc, value s)
 {
-       return String_val(s);
+       int len;
+       char *c;
+       len = caml_string_length(s);
+       c = calloc(len + 1, sizeof(char));
+       if (!c)
+               caml_raise_out_of_memory();
+       gc->ptrs[gc->offset++] = c;
+       memcpy(c, String_val(s), len);
+       return c;
+}
+
+static void gc_free(caml_gc *gc)
+{
+       int i;
+       for (i = 0; i < gc->offset; i++) {
+               free(gc->ptrs[i]);
+       }
 }
 
 void failwith_xl(char *fname, struct caml_logger *lg)
@@ -349,9 +369,6 @@ value stub_xl_domain_make(value info)
 
        FREE_CTX();
 
-       free(c_info.xsdata);
-       free(c_info.platformdata);
-
        CAMLreturn(Val_int(domid));
 }
 
-- 
1.7.1


_______________________________________________
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®.