fix initial storage allocation for array lists created with CX_STORE_POINTERS

Fri, 07 Apr 2023 11:30:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 07 Apr 2023 11:30:28 +0200
changeset 676
d0680a23d850
parent 675
765cf785b7fa
child 677
b09aae58bba4

fix initial storage allocation for array lists created with CX_STORE_POINTERS

src/array_list.c file | annotate | diff | comparison | revisions
--- a/src/array_list.c	Mon Apr 03 19:48:00 2023 +0200
+++ b/src/array_list.c	Fri Apr 07 11:30:28 2023 +0200
@@ -511,12 +511,6 @@
     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
     if (list == NULL) return NULL;
 
-    list->data = cxCalloc(allocator, initial_capacity, item_size);
-    if (list->data == NULL) {
-        cxFree(allocator, list);
-        return NULL;
-    }
-
     list->base.cl = &cx_array_list_class;
     list->base.allocator = allocator;
     list->base.cmpfunc = comparator;
@@ -525,9 +519,17 @@
     if (item_size > 0) {
         list->base.itemsize = item_size;
     } else {
+        item_size = sizeof(void*);
         cxListStorePointers((CxList *) list);
     }
 
+    // allocate the array after the real item_size is known
+    list->data = cxCalloc(allocator, initial_capacity, item_size);
+    if (list->data == NULL) {
+        cxFree(allocator, list);
+        return NULL;
+    }
+
     // configure the reallocator
     list->reallocator.realloc = cx_arl_realloc;
     list->reallocator.ptr1 = (void *) allocator;

mercurial