Tue, 21 Mar 2023 17:18:29 +0100
add CX_STORE_POINTERS special "item size" for lists
src/array_list.c | file | annotate | diff | comparison | revisions | |
src/cx/list.h | file | annotate | diff | comparison | revisions | |
src/linked_list.c | file | annotate | diff | comparison | revisions | |
tests/test_list.cpp | file | annotate | diff | comparison | revisions |
1.1 --- a/src/array_list.c Mon Mar 20 19:09:08 2023 +0100 1.2 +++ b/src/array_list.c Tue Mar 21 17:18:29 2023 +0100 1.3 @@ -516,9 +516,14 @@ 1.4 list->base.cl = &cx_array_list_class; 1.5 list->base.allocator = allocator; 1.6 list->base.cmpfunc = comparator; 1.7 - list->base.itemsize = item_size; 1.8 list->base.capacity = initial_capacity; 1.9 1.10 + if (item_size > 0) { 1.11 + list->base.itemsize = item_size; 1.12 + } else { 1.13 + cxListStorePointers((CxList *) list); 1.14 + } 1.15 + 1.16 // configure the reallocator 1.17 list->reallocator.realloc = cx_arl_realloc; 1.18 list->reallocator.ptr1 = (void *) allocator;
2.1 --- a/src/cx/list.h Mon Mar 20 19:09:08 2023 +0100 2.2 +++ b/src/cx/list.h Tue Mar 21 17:18:29 2023 +0100 2.3 @@ -45,6 +45,10 @@ 2.4 extern "C" { 2.5 #endif 2.6 2.7 +#ifndef CX_STORE_POINTERS 2.8 +#define CX_STORE_POINTERS 0 2.9 +#endif 2.10 + 2.11 /** 2.12 * A comparator function comparing two list elements. 2.13 */
3.1 --- a/src/linked_list.c Mon Mar 20 19:09:08 2023 +0100 3.2 +++ b/src/linked_list.c Tue Mar 21 17:18:29 2023 +0100 3.3 @@ -936,9 +936,14 @@ 3.4 list->base.cl = &cx_linked_list_class; 3.5 list->base.allocator = allocator; 3.6 list->base.cmpfunc = comparator; 3.7 - list->base.itemsize = item_size; 3.8 list->base.capacity = SIZE_MAX; 3.9 3.10 + if (item_size > 0) { 3.11 + list->base.itemsize = item_size; 3.12 + } else { 3.13 + cxListStorePointers((CxList *) list); 3.14 + } 3.15 + 3.16 return (CxList *) list; 3.17 } 3.18
4.1 --- a/tests/test_list.cpp Mon Mar 20 19:09:08 2023 +0100 4.2 +++ b/tests/test_list.cpp Tue Mar 21 17:18:29 2023 +0100 4.3 @@ -587,8 +587,7 @@ 4.4 } 4.5 4.6 auto pointerLinkedListFromTestData() const -> CxList * { 4.7 - auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 4.8 - cxListStorePointers(list); 4.9 + auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)); 4.10 // note: cannot use cxListAddArray() because we don't have a list of pointers 4.11 cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]); 4.12 return list; 4.13 @@ -933,6 +932,12 @@ 4.14 EXPECT_NE(list->cl, nullptr); 4.15 EXPECT_EQ(list->climpl, nullptr); 4.16 EXPECT_FALSE(cxListIsStoringPointers(list)); 4.17 + 4.18 + list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)); 4.19 + EXPECT_EQ(list->itemsize, sizeof(void *)); 4.20 + EXPECT_NE(list->cl, nullptr); 4.21 + EXPECT_NE(list->climpl, nullptr); 4.22 + EXPECT_TRUE(cxListIsStoringPointers(list)); 4.23 } 4.24 4.25 TEST_F(LinkedList, cxLinkedListCreate) { 4.26 @@ -975,8 +980,7 @@ 4.27 } 4.28 4.29 TEST_F(PointerLinkedList, cxListAdd) { 4.30 - auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 4.31 - cxListStorePointers(list); 4.32 + auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)); 4.33 verifyAdd(list, true); 4.34 } 4.35 4.36 @@ -990,9 +994,7 @@ 4.37 } 4.38 4.39 TEST_F(PointerLinkedList, cxListInsert) { 4.40 - auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 4.41 - cxListStorePointers(list); 4.42 - verifyInsert(list); 4.43 + verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS))); 4.44 } 4.45 4.46 TEST_F(ArrayList, cxListInsert) { 4.47 @@ -1004,9 +1006,7 @@ 4.48 } 4.49 4.50 TEST_F(PointerLinkedList, cxListInsertArray) { 4.51 - auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 4.52 - cxListStorePointers(list); 4.53 - verifyInsertArray(list, true); 4.54 + verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)), true); 4.55 } 4.56 4.57 TEST_F(ArrayList, cxListInsertArray) { 4.58 @@ -1030,9 +1030,7 @@ 4.59 } 4.60 4.61 TEST_F(PointerLinkedList, cxListClear) { 4.62 - auto l = cxLinkedListCreateSimple(sizeof(testdatastruct)); 4.63 - cxListStorePointers(l); 4.64 - verifyClear(autofree(l)); 4.65 + verifyClear(cxLinkedListCreateSimple(CX_STORE_POINTERS)); 4.66 } 4.67 4.68 TEST_F(ArrayList, cxListClear) { 4.69 @@ -1044,9 +1042,7 @@ 4.70 } 4.71 4.72 TEST_F(PointerLinkedList, cxListSwap) { 4.73 - auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 4.74 - cxListStorePointers(list); 4.75 - verifySwap(list); 4.76 + verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS))); 4.77 } 4.78 4.79 TEST_F(ArrayList, cxListSwap) { 4.80 @@ -1060,10 +1056,8 @@ 4.81 } 4.82 4.83 TEST_F(PointerLinkedList, cxListSwapNoSBO) { 4.84 - auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 4.85 - cxListStorePointers(list); 4.86 CX_DISABLE_LINKED_LIST_SWAP_SBO = true; 4.87 - verifySwap(list); 4.88 + verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS))); 4.89 CX_DISABLE_LINKED_LIST_SWAP_SBO = false; 4.90 } 4.91 4.92 @@ -1130,8 +1124,7 @@ 4.93 4.94 TEST_F(PointerLinkedList, InsertViaIterator) { 4.95 int fivenums[] = {0, 1, 2, 3, 4, 5}; 4.96 - auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *))); 4.97 - cxListStorePointers(list); 4.98 + auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)); 4.99 // note: cannot use cxListAddArray() because we don't have a list of pointers 4.100 cx_for_n(i, 5) cxListAdd(list, &fivenums[i]); 4.101 verifyInsertViaIterator(list); 4.102 @@ -1139,7 +1132,7 @@ 4.103 4.104 TEST_F(ArrayList, InsertViaIterator) { 4.105 int fivenums[] = {0, 1, 2, 3, 4, 5}; 4.106 - CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)); 4.107 + auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4)); 4.108 cxListAddArray(list, fivenums, 5); 4.109 verifyInsertViaIterator(list); 4.110 } 4.111 @@ -1212,8 +1205,7 @@ 4.112 4.113 TEST_F(PointerLinkedList, NoDestructor) { 4.114 void *item = cxMalloc(&testingAllocator, sizeof(int)); 4.115 - auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *)); 4.116 - cxListStorePointers(list); 4.117 + auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 4.118 cxListAdd(list, item); 4.119 ASSERT_FALSE(testingAllocator.verify()); 4.120 cxListDestroy(list); 4.121 @@ -1224,8 +1216,7 @@ 4.122 4.123 TEST_F(PointerLinkedList, SimpleDestructor) { 4.124 int item = 0; 4.125 - auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *)); 4.126 - cxListStorePointers(list); 4.127 + auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 4.128 list->content_destructor_type = CX_DESTRUCTOR_SIMPLE; 4.129 list->simple_destructor = [](void *elem) { *(int *) elem = 42; }; 4.130 cxListAdd(list, &item); 4.131 @@ -1235,8 +1226,7 @@ 4.132 4.133 TEST_F(PointerLinkedList, AdvancedDestructor) { 4.134 void *item = cxMalloc(&testingAllocator, sizeof(int)); 4.135 - auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *)); 4.136 - cxListStorePointers(list); 4.137 + auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS); 4.138 list->content_destructor_type = CX_DESTRUCTOR_ADVANCED; 4.139 list->advanced_destructor.data = &testingAllocator; 4.140 list->advanced_destructor.func = (cx_destructor_func2) cxFree;