libc3: c_array tweaks
authorMichel Pollet <buserror@gmail.com>
Wed, 13 Jun 2012 17:11:23 +0000 (18:11 +0100)
committerMichel Pollet <buserror@gmail.com>
Wed, 13 Jun 2012 17:11:23 +0000 (18:11 +0100)
'free' no longer clears the entire struct, only generic
array fields
'realloc' frees the array if passed zero elements

Signed-off-by: Michel Pollet <buserror@gmail.com>
examples/shared/libc3/src/c_array.h

index bb1aa22..ea974fc 100644 (file)
@@ -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 \