remove flags to disable SBO in tests - fix #343 fix #358

Mon, 15 Jan 2024 20:59:18 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 15 Jan 2024 20:59:18 +0100
changeset 807
c8d692131b1e
parent 806
e06249e09f99
child 808
f7f193893894

remove flags to disable SBO in tests - fix #343 fix #358

docs/src/install.md file | annotate | diff | comparison | revisions
src/array_list.c file | annotate | diff | comparison | revisions
src/cx/array_list.h file | annotate | diff | comparison | revisions
src/cx/linked_list.h file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
tests/test_list.c file | annotate | diff | comparison | revisions
--- a/docs/src/install.md	Sun Jan 14 13:50:17 2024 +0100
+++ b/docs/src/install.md	Mon Jan 15 20:59:18 2024 +0100
@@ -26,9 +26,9 @@
 --------------------------------- --------------------------------------------------------------------- ----------
 CX_ARRAY_SWAP_SBO_SIZE            The maximum item size in an array list that uses SBO.                 128
 
-CX_LINKED_LIST_SORT_SBO_SIZE      The maximum list size that uses SBO during sort.                      1024
+CX_LINKED_LIST_SWAP_SBO_SIZE      The maximum item size that uses SBO swap instead of relinking.        128
 
-CX_LINKED_LIST_SWAP_SBO_SIZE      The maximum item size that uses SBO swap instead of relinking.        128
+CX_LINKED_LIST_SORT_SBO_SIZE      The maximum list size that uses SBO during sort.                      1024
 
 CX_PRINTF_SBO_SIZE                The maximum string length printf.h uses stack memory for.             512
 
--- a/src/array_list.c	Sun Jan 14 13:50:17 2024 +0100
+++ b/src/array_list.c	Mon Jan 15 20:59:18 2024 +0100
@@ -106,8 +106,7 @@
 #ifndef CX_ARRAY_SWAP_SBO_SIZE
 #define CX_ARRAY_SWAP_SBO_SIZE 128
 #endif
-
-bool CX_DISABLE_ARRAY_LIST_SWAP_SBO = false;
+unsigned cx_array_swap_sbo_size = CX_ARRAY_SWAP_SBO_SIZE;
 
 void cx_array_swap(
         void *arr,
@@ -124,7 +123,7 @@
     void *tmp;
 
     // decide if we can use the local buffer
-    if (elem_size > CX_ARRAY_SWAP_SBO_SIZE || CX_DISABLE_ARRAY_LIST_SWAP_SBO) {
+    if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
         tmp = malloc(elem_size);
         // we don't want to enforce error handling
         if (tmp == NULL) abort();
--- a/src/cx/array_list.h	Sun Jan 14 13:50:17 2024 +0100
+++ b/src/cx/array_list.h	Mon Jan 15 20:59:18 2024 +0100
@@ -45,10 +45,9 @@
 #endif
 
 /**
- * Set this flag to true, if you want to disable the use of SBO for
- * array list swap operations.
+ * The maximum item size in an array list that fits into stack buffer when swapped.
  */
-extern bool CX_DISABLE_ARRAY_LIST_SWAP_SBO;
+extern unsigned cx_array_swap_sbo_size;
 
 /**
  * Defines a reallocation mechanism for arrays.
--- a/src/cx/linked_list.h	Sun Jan 14 13:50:17 2024 +0100
+++ b/src/cx/linked_list.h	Mon Jan 15 20:59:18 2024 +0100
@@ -45,10 +45,9 @@
 #endif
 
 /**
- * Set this flag to true, if you want to disable the use of SBO for
- * linked list swap operations.
+ * The maximum item size that uses SBO swap instead of relinking.
  */
-extern bool CX_DISABLE_LINKED_LIST_SWAP_SBO;
+extern unsigned cx_linked_list_swap_sbo_size;
 
 /**
  * Allocates a linked list for storing elements with \p item_size bytes each.
--- a/src/linked_list.c	Sun Jan 14 13:50:17 2024 +0100
+++ b/src/linked_list.c	Mon Jan 15 20:59:18 2024 +0100
@@ -480,8 +480,6 @@
 
 // HIGH LEVEL LINKED LIST IMPLEMENTATION
 
-bool CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
-
 typedef struct cx_linked_list_node cx_linked_list_node;
 struct cx_linked_list_node {
     cx_linked_list_node *prev;
@@ -629,6 +627,7 @@
 #ifndef CX_LINKED_LIST_SWAP_SBO_SIZE
 #define CX_LINKED_LIST_SWAP_SBO_SIZE 128
 #endif
+unsigned cx_linked_list_swap_sbo_size = CX_LINKED_LIST_SWAP_SBO_SIZE;
 
 static int cx_ll_swap(
         struct cx_list_s *list,
@@ -653,6 +652,7 @@
     if (left < mid && right < mid) {
         // case 1: both items left from mid
         nleft = cx_ll_node_at(ll, left);
+        assert(nleft != NULL);
         nright = nleft;
         for (size_t c = left; c < right; c++) {
             nright = nright->next;
@@ -660,6 +660,7 @@
     } else if (left >= mid && right >= mid) {
         // case 2: both items right from mid
         nright = cx_ll_node_at(ll, right);
+        assert(nright != NULL);
         nleft = nright;
         for (size_t c = right; c > left; c--) {
             nleft = nleft->prev;
@@ -706,7 +707,7 @@
         }
     }
 
-    if (list->item_size > CX_LINKED_LIST_SWAP_SBO_SIZE || CX_DISABLE_LINKED_LIST_SWAP_SBO) {
+    if (list->item_size > CX_LINKED_LIST_SWAP_SBO_SIZE) {
         cx_linked_list_node *prev = nleft->prev;
         cx_linked_list_node *next = nright->next;
         cx_linked_list_node *midstart = nleft->next;
--- a/tests/test_list.c	Sun Jan 14 13:50:17 2024 +0100
+++ b/tests/test_list.c	Mon Jan 15 20:59:18 2024 +0100
@@ -867,30 +867,29 @@
     cx_testing_allocator_destroy(&talloc);
 #define roll_out_test_combos(name, body) \
 static CX_TEST_SUBROUTINE(test_list_verify_##name, CxList *list, \
-    __attribute__((__unused__)) bool isptrlist, \
-    __attribute__((__unused__)) bool islinkedlist) body \
+    __attribute__((__unused__)) bool isptrlist) body \
 CX_TEST(test_list_ll_##name) { \
     set_up_combo \
         CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, sizeof(int)); \
-        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false, true); \
+        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \
     tear_down_combo \
 } \
 CX_TEST(test_list_arl_##name) { \
     set_up_combo \
         CxList *list = cxArrayListCreate(alloc, cx_cmp_int, sizeof(int), 8); \
-        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false, false); \
+        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, false); \
     tear_down_combo \
 } \
 CX_TEST(test_list_pll_##name) { \
     set_up_combo \
         CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS); \
-        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true, true); \
+        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \
     tear_down_combo \
 } \
 CX_TEST(test_list_parl_##name) { \
     set_up_combo \
         CxList *list = cxArrayListCreate(alloc, cx_cmp_int, CX_STORE_POINTERS, 8); \
-        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true, false); \
+        CX_TEST_CALL_SUBROUTINE(test_list_verify_##name, list, true); \
     tear_down_combo \
 }
 #define array_init(...) {__VA_ARGS__}
@@ -1080,17 +1079,18 @@
     }
 })
 
-roll_out_test_combos(swap_no_sbo, {
-    if (islinkedlist) {
-        CX_DISABLE_LINKED_LIST_SWAP_SBO = true;
-        CX_TEST_CALL_SUBROUTINE(test_list_verify_swap, list, isptrlist, true);
-        CX_DISABLE_LINKED_LIST_SWAP_SBO = false;
-    } else {
-        CX_DISABLE_ARRAY_LIST_SWAP_SBO = true;
-        CX_TEST_CALL_SUBROUTINE(test_list_verify_swap, list, isptrlist, false);
-        CX_DISABLE_ARRAY_LIST_SWAP_SBO = false;
-    }
-})
+CX_TEST(test_list_ll_swap_no_sbo) {
+    set_up_combo
+        CxList *list = cxLinkedListCreate(alloc, cx_cmp_int, 2*cx_linked_list_swap_sbo_size);
+        CX_TEST_CALL_SUBROUTINE(test_list_verify_swap, list, false);
+    tear_down_combo
+}
+CX_TEST(test_list_arl_swap_no_sbo) {
+    set_up_combo
+        CxList *list = cxArrayListCreate(alloc, cx_cmp_int, 2*cx_array_swap_sbo_size, 8);
+        CX_TEST_CALL_SUBROUTINE(test_list_verify_swap, list, false);
+    tear_down_combo
+}
 
 roll_out_test_combos(find, {
     const size_t testdata_len = 500;
@@ -1368,7 +1368,6 @@
     cx_test_register(suite, test_list_arl_swap);
     cx_test_register(suite, test_list_parl_swap);
     cx_test_register(suite, test_list_arl_swap_no_sbo);
-    cx_test_register(suite, test_list_parl_swap_no_sbo);
     cx_test_register(suite, test_list_arl_find);
     cx_test_register(suite, test_list_parl_find);
     cx_test_register(suite, test_list_arl_sort);
@@ -1440,7 +1439,6 @@
     cx_test_register(suite, test_list_ll_swap);
     cx_test_register(suite, test_list_pll_swap);
     cx_test_register(suite, test_list_ll_swap_no_sbo);
-    cx_test_register(suite, test_list_pll_swap_no_sbo);
     cx_test_register(suite, test_list_ll_find);
     cx_test_register(suite, test_list_pll_find);
     cx_test_register(suite, test_list_ll_sort);

mercurial