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

[Xen-devel] [PATCH 3/5] libxl: add @count to libxl_gc



Signed-off-by: Lai Jiangshan <laijs@xxxxxxxxxxxxxx>
---
 tools/libxl/libxl_internal.c |   22 ++++++++--------------
 tools/libxl/libxl_internal.h |    2 ++
 2 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c
index 1db48b6..a79d841 100644
--- a/tools/libxl/libxl_internal.c
+++ b/tools/libxl/libxl_internal.c
@@ -32,8 +32,6 @@ void libxl__alloc_failed(libxl_ctx *ctx, const char *func,
 
 void libxl__ptr_add(libxl__gc *gc, void *ptr)
 {
-    int i;
-
     if (!libxl__gc_is_real(gc))
         return;
 
@@ -41,22 +39,17 @@ void libxl__ptr_add(libxl__gc *gc, void *ptr)
         return;
 
     /* fast case: we have space in the array for storing the pointer */
-    for (i = 0; i < gc->alloc_maxsize; i++) {
-        if (!gc->alloc_ptrs[i]) {
-            gc->alloc_ptrs[i] = ptr;
-            return;
-        }
-    }
+    if (gc->count < gc->alloc_maxsize)
+        gc->alloc_ptrs[gc->count++] = ptr;
+
     int new_maxsize = gc->alloc_maxsize * 2 + 25;
     assert(new_maxsize < INT_MAX / sizeof(void*) / 2);
     gc->alloc_ptrs = realloc(gc->alloc_ptrs, new_maxsize * sizeof(void *));
     if (!gc->alloc_ptrs)
         libxl__alloc_failed(CTX, __func__, new_maxsize, sizeof(void*));
 
-    gc->alloc_ptrs[gc->alloc_maxsize++] = ptr;
-
-    while (gc->alloc_maxsize < new_maxsize)
-        gc->alloc_ptrs[gc->alloc_maxsize++] = 0;
+    gc->alloc_ptrs[gc->count++] = ptr;
+    gc->alloc_maxsize = new_maxsize;
 
     return;
 }
@@ -68,7 +61,7 @@ void libxl__free_all(libxl__gc *gc)
 
     assert(libxl__gc_is_real(gc));
 
-    for (i = 0; i < gc->alloc_maxsize; i++) {
+    for (i = 0; i < gc->count; i++) {
         ptr = gc->alloc_ptrs[i];
         gc->alloc_ptrs[i] = NULL;
         free(ptr);
@@ -76,6 +69,7 @@ void libxl__free_all(libxl__gc *gc)
     free(gc->alloc_ptrs);
     gc->alloc_ptrs = 0;
     gc->alloc_maxsize = 0;
+    gc->count = 0;
 }
 
 void *libxl__zalloc(libxl__gc *gc, int bytes)
@@ -107,7 +101,7 @@ void *libxl__realloc(libxl__gc *gc, void *ptr, size_t 
new_size)
     if (ptr == NULL) {
         libxl__ptr_add(gc, new_ptr);
     } else if (new_ptr != ptr && libxl__gc_is_real(gc)) {
-        for (i = 0; i < gc->alloc_maxsize; i++) {
+        for (i = 0; i < gc->count; i++) {
             if (gc->alloc_ptrs[i] == ptr) {
                 gc->alloc_ptrs[i] = new_ptr;
                 break;
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 1bd23ff..d2100d8 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -294,6 +294,7 @@ struct libxl__poller {
 
 struct libxl__gc {
     /* mini-GC */
+    int count;
     int alloc_maxsize; /* -1 means this is the dummy non-gc gc */
     void **alloc_ptrs;
     libxl_ctx *owner;
@@ -438,6 +439,7 @@ struct libxl__ao {
 };
 
 #define LIBXL_INIT_GC(gc,ctx) do{               \
+        (gc).count = 0;                         \
         (gc).alloc_maxsize = 0;                 \
         (gc).alloc_ptrs = 0;                    \
         (gc).owner = (ctx);                     \
-- 
1.7.1


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel


 


Rackspace

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