Fri, 07 Apr 2023 11:30:28 +0200
fix initial storage allocation for array lists created with CX_STORE_POINTERS
src/array_list.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/array_list.c Mon Apr 03 19:48:00 2023 +0200 1.2 +++ b/src/array_list.c Fri Apr 07 11:30:28 2023 +0200 1.3 @@ -511,12 +511,6 @@ 1.4 cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list)); 1.5 if (list == NULL) return NULL; 1.6 1.7 - list->data = cxCalloc(allocator, initial_capacity, item_size); 1.8 - if (list->data == NULL) { 1.9 - cxFree(allocator, list); 1.10 - return NULL; 1.11 - } 1.12 - 1.13 list->base.cl = &cx_array_list_class; 1.14 list->base.allocator = allocator; 1.15 list->base.cmpfunc = comparator; 1.16 @@ -525,9 +519,17 @@ 1.17 if (item_size > 0) { 1.18 list->base.itemsize = item_size; 1.19 } else { 1.20 + item_size = sizeof(void*); 1.21 cxListStorePointers((CxList *) list); 1.22 } 1.23 1.24 + // allocate the array after the real item_size is known 1.25 + list->data = cxCalloc(allocator, initial_capacity, item_size); 1.26 + if (list->data == NULL) { 1.27 + cxFree(allocator, list); 1.28 + return NULL; 1.29 + } 1.30 + 1.31 // configure the reallocator 1.32 list->reallocator.realloc = cx_arl_realloc; 1.33 list->reallocator.ptr1 = (void *) allocator;