add CX_STORE_POINTERS special "item size" for lists

Tue, 21 Mar 2023 17:18:29 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 21 Mar 2023 17:18:29 +0100
changeset 667
2f88a7c13a28
parent 666
b5dd654deb3b
child 668
d7129285ac32

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
--- a/src/array_list.c	Mon Mar 20 19:09:08 2023 +0100
+++ b/src/array_list.c	Tue Mar 21 17:18:29 2023 +0100
@@ -516,9 +516,14 @@
     list->base.cl = &cx_array_list_class;
     list->base.allocator = allocator;
     list->base.cmpfunc = comparator;
-    list->base.itemsize = item_size;
     list->base.capacity = initial_capacity;
 
+    if (item_size > 0) {
+        list->base.itemsize = item_size;
+    } else {
+        cxListStorePointers((CxList *) list);
+    }
+
     // configure the reallocator
     list->reallocator.realloc = cx_arl_realloc;
     list->reallocator.ptr1 = (void *) allocator;
--- a/src/cx/list.h	Mon Mar 20 19:09:08 2023 +0100
+++ b/src/cx/list.h	Tue Mar 21 17:18:29 2023 +0100
@@ -45,6 +45,10 @@
 extern "C" {
 #endif
 
+#ifndef CX_STORE_POINTERS
+#define CX_STORE_POINTERS 0
+#endif
+
 /**
  * A comparator function comparing two list elements.
  */
--- a/src/linked_list.c	Mon Mar 20 19:09:08 2023 +0100
+++ b/src/linked_list.c	Tue Mar 21 17:18:29 2023 +0100
@@ -936,9 +936,14 @@
     list->base.cl = &cx_linked_list_class;
     list->base.allocator = allocator;
     list->base.cmpfunc = comparator;
-    list->base.itemsize = item_size;
     list->base.capacity = SIZE_MAX;
 
+    if (item_size > 0) {
+        list->base.itemsize = item_size;
+    } else {
+        cxListStorePointers((CxList *) list);
+    }
+
     return (CxList *) list;
 }
 
--- a/tests/test_list.cpp	Mon Mar 20 19:09:08 2023 +0100
+++ b/tests/test_list.cpp	Tue Mar 21 17:18:29 2023 +0100
@@ -587,8 +587,7 @@
     }
 
     auto pointerLinkedListFromTestData() const -> CxList * {
-        auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
-        cxListStorePointers(list);
+        auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
         // note: cannot use cxListAddArray() because we don't have a list of pointers
         cx_for_n(i, testdata_len) cxListAdd(list, &testdata.data[i]);
         return list;
@@ -933,6 +932,12 @@
     EXPECT_NE(list->cl, nullptr);
     EXPECT_EQ(list->climpl, nullptr);
     EXPECT_FALSE(cxListIsStoringPointers(list));
+
+    list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
+    EXPECT_EQ(list->itemsize, sizeof(void *));
+    EXPECT_NE(list->cl, nullptr);
+    EXPECT_NE(list->climpl, nullptr);
+    EXPECT_TRUE(cxListIsStoringPointers(list));
 }
 
 TEST_F(LinkedList, cxLinkedListCreate) {
@@ -975,8 +980,7 @@
 }
 
 TEST_F(PointerLinkedList, cxListAdd) {
-    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
-    cxListStorePointers(list);
+    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
     verifyAdd(list, true);
 }
 
@@ -990,9 +994,7 @@
 }
 
 TEST_F(PointerLinkedList, cxListInsert) {
-    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
-    cxListStorePointers(list);
-    verifyInsert(list);
+    verifyInsert(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)));
 }
 
 TEST_F(ArrayList, cxListInsert) {
@@ -1004,9 +1006,7 @@
 }
 
 TEST_F(PointerLinkedList, cxListInsertArray) {
-    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
-    cxListStorePointers(list);
-    verifyInsertArray(list, true);
+    verifyInsertArray(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)), true);
 }
 
 TEST_F(ArrayList, cxListInsertArray) {
@@ -1030,9 +1030,7 @@
 }
 
 TEST_F(PointerLinkedList, cxListClear) {
-    auto l = cxLinkedListCreateSimple(sizeof(testdatastruct));
-    cxListStorePointers(l);
-    verifyClear(autofree(l));
+    verifyClear(cxLinkedListCreateSimple(CX_STORE_POINTERS));
 }
 
 TEST_F(ArrayList, cxListClear) {
@@ -1044,9 +1042,7 @@
 }
 
 TEST_F(PointerLinkedList, cxListSwap) {
-    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
-    cxListStorePointers(list);
-    verifySwap(list);
+    verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)));
 }
 
 TEST_F(ArrayList, cxListSwap) {
@@ -1060,10 +1056,8 @@
 }
 
 TEST_F(PointerLinkedList, cxListSwapNoSBO) {
-    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
-    cxListStorePointers(list);
     CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
-    verifySwap(list);
+    verifySwap(autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS)));
     CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
 }
 
@@ -1130,8 +1124,7 @@
 
 TEST_F(PointerLinkedList, InsertViaIterator) {
     int fivenums[] = {0, 1, 2, 3, 4, 5};
-    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, sizeof(int *)));
-    cxListStorePointers(list);
+    auto list = autofree(cxLinkedListCreate(&testingAllocator, cx_cmp_int, CX_STORE_POINTERS));
     // note: cannot use cxListAddArray() because we don't have a list of pointers
     cx_for_n(i, 5) cxListAdd(list, &fivenums[i]);
     verifyInsertViaIterator(list);
@@ -1139,7 +1132,7 @@
 
 TEST_F(ArrayList, InsertViaIterator) {
     int fivenums[] = {0, 1, 2, 3, 4, 5};
-    CxList *list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
+    auto list = autofree(cxArrayListCreate(&testingAllocator, cx_cmp_int, sizeof(int), 4));
     cxListAddArray(list, fivenums, 5);
     verifyInsertViaIterator(list);
 }
@@ -1212,8 +1205,7 @@
 
 TEST_F(PointerLinkedList, NoDestructor) {
     void *item = cxMalloc(&testingAllocator, sizeof(int));
-    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
-    cxListStorePointers(list);
+    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
     cxListAdd(list, item);
     ASSERT_FALSE(testingAllocator.verify());
     cxListDestroy(list);
@@ -1224,8 +1216,7 @@
 
 TEST_F(PointerLinkedList, SimpleDestructor) {
     int item = 0;
-    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
-    cxListStorePointers(list);
+    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
     list->content_destructor_type = CX_DESTRUCTOR_SIMPLE;
     list->simple_destructor = [](void *elem) { *(int *) elem = 42; };
     cxListAdd(list, &item);
@@ -1235,8 +1226,7 @@
 
 TEST_F(PointerLinkedList, AdvancedDestructor) {
     void *item = cxMalloc(&testingAllocator, sizeof(int));
-    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, sizeof(int *));
-    cxListStorePointers(list);
+    auto list = cxLinkedListCreate(cxDefaultAllocator, cx_cmp_int, CX_STORE_POINTERS);
     list->content_destructor_type = CX_DESTRUCTOR_ADVANCED;
     list->advanced_destructor.data = &testingAllocator;
     list->advanced_destructor.func = (cx_destructor_func2) cxFree;

mercurial