aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl/flexarray.c
diff options
context:
space:
mode:
authorAnthony PERARD <anthony.perard@citrix.com>2012-10-05 14:34:30 +0100
committerAnthony PERARD <anthony.perard@citrix.com>2012-10-05 14:34:30 +0100
commit5fe37ba9c6b6658ed7980f81f8374acd6e2f3bbd (patch)
tree74ef7377424cc78bfe25429e3a9c7b047a5cd3b7 /tools/libxl/flexarray.c
parent3c8ec17b26e686a6601e1a678c090f44464ad514 (diff)
downloadxen-5fe37ba9c6b6658ed7980f81f8374acd6e2f3bbd.tar.gz
xen-5fe37ba9c6b6658ed7980f81f8374acd6e2f3bbd.tar.bz2
xen-5fe37ba9c6b6658ed7980f81f8374acd6e2f3bbd.zip
libxl: Have flexarray using the GC
This patch makes the flexarray function libxl__gc aware. It also updates every function that use a flexarray to pass the gc and removes every memory allocation check and free. Signed-off-by: Anthony PERARD <anthony.perard@citrix.com> Acked-by: Ian Jackson <ian.jackson@eu.citrix.com> Committed-by: Ian Campbell <ian.campbell@citrix.com>
Diffstat (limited to 'tools/libxl/flexarray.c')
-rw-r--r--tools/libxl/flexarray.c43
1 files changed, 25 insertions, 18 deletions
diff --git a/tools/libxl/flexarray.c b/tools/libxl/flexarray.c
index edf616ca9a..fe40e762e4 100644
--- a/tools/libxl/flexarray.c
+++ b/tools/libxl/flexarray.c
@@ -16,36 +16,43 @@
#include "libxl_internal.h"
#include <stdarg.h>
-flexarray_t *flexarray_make(int size, int autogrow)
+/*
+ * It is safe to store gc in the struct because:
+ * - If it an actual gc, then the flexarray should not be used after the gc
+ * have been freed.
+ * - If it is a NOGC, then this point to a structure embedded in libxl_ctx,
+ * therefore will survive across several libxl calls.
+ */
+
+flexarray_t *flexarray_make(libxl__gc *gc, int size, int autogrow)
{
- flexarray_t *array = malloc(sizeof(struct flexarray));
- if (array) {
- array->size = size;
- array->autogrow = autogrow;
- array->count = 0;
- array->data = calloc(size, sizeof(void *));
- }
+ flexarray_t *array;
+
+ GCNEW(array);
+ array->size = size;
+ array->autogrow = autogrow;
+ array->count = 0;
+ array->gc = gc;
+ GCNEW_ARRAY(array->data, size);
+
return array;
}
void flexarray_free(flexarray_t *array)
{
+ assert(!libxl__gc_is_real(array->gc));
free(array->data);
free(array);
}
-int flexarray_grow(flexarray_t *array, int extents)
+void flexarray_grow(flexarray_t *array, int extents)
{
- void **data;
int newsize;
+ libxl__gc *gc = array->gc;
newsize = array->size + extents;
- data = realloc(array->data, sizeof(void *) * newsize);
- if (!data)
- return 1;
+ GCREALLOC_ARRAY(array->data, newsize);
array->size += extents;
- array->data = data;
- return 0;
}
int flexarray_set(flexarray_t *array, unsigned int idx, void *ptr)
@@ -55,8 +62,7 @@ int flexarray_set(flexarray_t *array, unsigned int idx, void *ptr)
if (!array->autogrow)
return 1;
newsize = (array->size * 2 < idx) ? idx + 1 : array->size * 2;
- if (flexarray_grow(array, newsize - array->size))
- return 2;
+ flexarray_grow(array, newsize - array->size);
}
if ( idx + 1 > array->count )
array->count = idx + 1;
@@ -104,7 +110,8 @@ void **flexarray_contents(flexarray_t *array)
{
void **data;
data = array->data;
- free(array);
+ if (!libxl__gc_is_real(array->gc))
+ free(array);
return data;
}