allow NULL for allocator and comparator

Tue, 28 Mar 2023 21:00:33 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 28 Mar 2023 21:00:33 +0200
changeset 670
4ad8ea3aee49
parent 669
dce9b8450656
child 671
d7a67375a7ac

allow NULL for allocator and comparator

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
--- a/src/array_list.c	Tue Mar 28 19:13:33 2023 +0200
+++ b/src/array_list.c	Tue Mar 28 21:00:33 2023 +0200
@@ -498,12 +498,16 @@
         cx_arl_iterator,
 };
 
-static CxList *cx_array_list_create(
+CxList *cxArrayListCreate(
         CxAllocator const *allocator,
         CxListComparator comparator,
         size_t item_size,
         size_t initial_capacity
 ) {
+    if (allocator == NULL) {
+        allocator = cxDefaultAllocator;
+    }
+
     cx_array_list *list = cxCalloc(allocator, 1, sizeof(cx_array_list));
     if (list == NULL) return NULL;
 
@@ -530,21 +534,3 @@
 
     return (CxList *) list;
 }
-
-CxList *cxArrayListCreate(
-        CxAllocator const *allocator,
-        CxListComparator comparator,
-        size_t item_size,
-        size_t initial_capacity
-) {
-    return cx_array_list_create(allocator, comparator,
-                                item_size, initial_capacity);
-}
-
-CxList *cxArrayListCreateSimple(
-        size_t item_size,
-        size_t initial_capacity
-) {
-    return cx_array_list_create(cxDefaultAllocator, NULL,
-                                item_size, initial_capacity);
-}
--- a/src/cx/array_list.h	Tue Mar 28 19:13:33 2023 +0200
+++ b/src/cx/array_list.h	Tue Mar 28 21:00:33 2023 +0200
@@ -156,7 +156,9 @@
  * cxListStorePointers() was called immediately after creation.
  *
  * @param allocator the allocator for allocating the list memory
+ * (if \c NULL the cxDefaultAllocator will be used)
  * @param comparator the comparator for the elements
+ * (if \c NULL sort and find functions will not work)
  * @param item_size the size of each element in bytes
  * @param initial_capacity the initial number of elements the array can store
  * @return the created list
@@ -166,7 +168,7 @@
         CxListComparator comparator,
         size_t item_size,
         size_t initial_capacity
-) __attribute__((__nonnull__));
+);
 
 /**
  * Allocates an array list for storing elements with \p item_size bytes each.
@@ -182,11 +184,8 @@
  * @param initial_capacity the initial number of elements the array can store
  * @return the created list
  */
-CxList *cxArrayListCreateSimple(
-        size_t item_size,
-        size_t initial_capacity
-);
-
+#define cxArrayListCreateSimple(item_size, initial_capacity) \
+    cxArrayListCreate(NULL, NULL, item_size, initial_capacity)
 
 #ifdef __cplusplus
 } // extern "C"
--- a/src/cx/linked_list.h	Tue Mar 28 19:13:33 2023 +0200
+++ b/src/cx/linked_list.h	Tue Mar 28 21:00:33 2023 +0200
@@ -58,7 +58,9 @@
  * cxListStorePointers() was called immediately after creation.
  *
  * @param allocator the allocator for allocating the list nodes
+ * (if \c NULL the cxDefaultAllocator will be used)
  * @param comparator the comparator for the elements
+ * (if \c NULL sort and find functions will not work)
  * @param item_size the size of each element in bytes
  * @return the created list
  */
@@ -66,7 +68,7 @@
         CxAllocator const *allocator,
         CxListComparator comparator,
         size_t item_size
-) __attribute__((__nonnull__));
+);
 
 /**
  * Allocates a linked list for storing elements with \p item_size bytes each.
@@ -81,7 +83,8 @@
  * @param item_size the size of each element in bytes
  * @return the created list
  */
-CxList *cxLinkedListCreateSimple(size_t item_size);
+#define cxLinkedListCreateSimple(item_size) \
+    cxLinkedListCreate(NULL, NULL, item_size)
 
 /**
  * Finds the node at a certain index.
--- a/src/linked_list.c	Tue Mar 28 19:13:33 2023 +0200
+++ b/src/linked_list.c	Tue Mar 28 21:00:33 2023 +0200
@@ -925,11 +925,15 @@
         cx_ll_iterator,
 };
 
-static CxList *cx_linked_list_create(
+CxList *cxLinkedListCreate(
         CxAllocator const *allocator,
         CxListComparator comparator,
         size_t item_size
 ) {
+    if (allocator == NULL) {
+        allocator = cxDefaultAllocator;
+    }
+
     cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list));
     if (list == NULL) return NULL;
 
@@ -946,15 +950,3 @@
 
     return (CxList *) list;
 }
-
-CxList *cxLinkedListCreate(
-        CxAllocator const *allocator,
-        CxListComparator comparator,
-        size_t item_size
-) {
-    return cx_linked_list_create(allocator, comparator, item_size);
-}
-
-CxList *cxLinkedListCreateSimple(size_t item_size) {
-    return cx_linked_list_create(cxDefaultAllocator, NULL, item_size);
-}

mercurial