From 1c4c07586f0591e7a6d8e46e91a4a6ba90a95a5f Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Wed, 13 Jun 2012 18:11:23 +0100 Subject: [PATCH] libc3: c_array tweaks 'free' no longer clears the entire struct, only generic array fields 'realloc' frees the array if passed zero elements Signed-off-by: Michel Pollet --- examples/shared/libc3/src/c_array.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/shared/libc3/src/c_array.h b/examples/shared/libc3/src/c_array.h index bb1aa22..ea974fc 100644 --- a/examples/shared/libc3/src/c_array.h +++ b/examples/shared/libc3/src/c_array.h @@ -55,7 +55,8 @@ static C_ARRAY_INLINE \ {\ if (!a) return;\ if (a->e) free(a->e);\ - *a = __name##_zero;\ + a->count = a->size = 0;\ + a->e = NULL;\ }\ static C_ARRAY_INLINE \ void __name##_clear(\ @@ -69,7 +70,8 @@ static C_ARRAY_INLINE \ __name##_p a, __name##_count_t size) \ {\ if (!a || a->size == size) return; \ - a->e = realloc(a->e, size * sizeof(__name##_element_t));\ + if (size == 0) { if (a->e) free(a->e); a->e = NULL; } \ + else a->e = realloc(a->e, size * sizeof(__name##_element_t));\ a->size = size; \ }\ static C_ARRAY_INLINE \ -- 2.39.5