add convenience macros for using the default allocator - relates to #669 default tip

Thu, 15 May 2025 16:12:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 May 2025 16:12:09 +0200
changeset 1319
aa1f580f8f59
parent 1318
12fa1d37fe48

add convenience macros for using the default allocator - relates to #669

docs/Writerside/topics/allocator.h.md file | annotate | diff | comparison | revisions
src/Makefile file | annotate | diff | comparison | revisions
src/array_list.c file | annotate | diff | comparison | revisions
src/buffer.c file | annotate | diff | comparison | revisions
src/cx/allocator.h file | annotate | diff | comparison | revisions
src/cx/array_list.h file | annotate | diff | comparison | revisions
src/cx/tree.h file | annotate | diff | comparison | revisions
src/json.c file | annotate | diff | comparison | revisions
src/linked_list.c file | annotate | diff | comparison | revisions
src/list.c file | annotate | diff | comparison | revisions
src/mempool.c file | annotate | diff | comparison | revisions
src/printf.c file | annotate | diff | comparison | revisions
src/properties.c file | annotate | diff | comparison | revisions
src/streams.c file | annotate | diff | comparison | revisions
src/string.c file | annotate | diff | comparison | revisions
src/tree.c file | annotate | diff | comparison | revisions
tests/Makefile file | annotate | diff | comparison | revisions
tests/test_allocator.c file | annotate | diff | comparison | revisions
tests/test_hash_map.c file | annotate | diff | comparison | revisions
tests/test_list.c file | annotate | diff | comparison | revisions
tests/test_tree.c file | annotate | diff | comparison | revisions
--- a/docs/Writerside/topics/allocator.h.md	Thu May 15 16:02:54 2025 +0200
+++ b/docs/Writerside/topics/allocator.h.md	Thu May 15 16:12:09 2025 +0200
@@ -47,6 +47,15 @@
 
 // default allocator that can be changed
 CxAllocator *cxDefaultAllocator = cxStdlibAllocator;
+
+// Convenience macros that invokes above functions with the cxDefaultAllocator.
+#define cxMallocDefault(...)
+#define cxCallocDefault(...)
+#define cxReallocDefault(...)
+#define cxReallocateDefault(...)
+#define cxReallocateArrayDefault(...)
+#define cxReallocArrayDefault(...)
+#define cxFreeDefault(...)
 ```
 
 > All UCX functions that are not _explicitly_ designed for taking an allocator argument
--- a/src/Makefile	Thu May 15 16:02:54 2025 +0200
+++ b/src/Makefile	Thu May 15 16:12:09 2025 +0200
@@ -141,7 +141,8 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS)  -c $<
 
-$(build_dir)/streams$(OBJ_EXT): streams.c cx/streams.h cx/common.h
+$(build_dir)/streams$(OBJ_EXT): streams.c cx/streams.h cx/common.h \
+ cx/allocator.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS)  -c $<
 
--- a/src/array_list.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/array_list.c	Thu May 15 16:12:09 2025 +0200
@@ -45,7 +45,7 @@
         errno = EOVERFLOW;
         return NULL;
     }
-    return cxRealloc(cxDefaultAllocator, array, n);
+    return cxReallocDefault(array, n);
 }
 
 CxArrayReallocator cx_array_default_reallocator_impl = {
@@ -572,7 +572,7 @@
 
     // decide if we can use the local buffer
     if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) {
-        tmp = cxMalloc(cxDefaultAllocator, elem_size);
+        tmp = cxMallocDefault(elem_size);
         // we don't want to enforce error handling
         if (tmp == NULL) abort();
     } else {
@@ -591,7 +591,7 @@
 
     // free dynamic memory, if it was needed
     if (tmp != sbo_mem) {
-        cxFree(cxDefaultAllocator, tmp);
+        cxFreeDefault(tmp);
     }
 }
 
--- a/src/buffer.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/buffer.c	Thu May 15 16:12:09 2025 +0200
@@ -98,7 +98,7 @@
     CxBuffer *buffer,
     CxBufferFlushConfig config
 ) {
-    buffer->flush = cxMalloc(cxDefaultAllocator, sizeof(CxBufferFlushConfig));
+    buffer->flush = cxMallocDefault(sizeof(CxBufferFlushConfig));
     if (buffer->flush == NULL) return -1; // LCOV_EXCL_LINE
     memcpy(buffer->flush, &config, sizeof(CxBufferFlushConfig));
     return 0;
@@ -108,7 +108,7 @@
     if (buffer->flags & CX_BUFFER_FREE_CONTENTS) {
         cxFree(buffer->allocator, buffer->bytes);
     }
-    cxFree(cxDefaultAllocator, buffer->flush);
+    cxFreeDefault(buffer->flush);
     memset(buffer, 0, sizeof(CxBuffer));
 }
 
--- a/src/cx/allocator.h	Thu May 15 16:02:54 2025 +0200
+++ b/src/cx/allocator.h	Thu May 15 16:12:09 2025 +0200
@@ -429,6 +429,37 @@
         size_t size
 );
 
+
+
+/**
+ * Convenience macro that invokes cxMalloc() with the cxDefaultAllocator.
+ */
+#define cxMallocDefault(...) cxMalloc(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxCalloc() with the cxDefaultAllocator.
+ */
+#define cxCallocDefault(...) cxCalloc(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxRealloc() with the cxDefaultAllocator.
+ */
+#define cxReallocDefault(...) cxRealloc(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxReallocate() with the cxDefaultAllocator.
+ */
+#define cxReallocateDefault(...) cxReallocate(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxReallocateArray() with the cxDefaultAllocator.
+ */
+#define cxReallocateArrayDefault(...) cxReallocateArray(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxReallocArray() with the cxDefaultAllocator.
+ */
+#define cxReallocArrayDefault(...) cxReallocArray(cxDefaultAllocator, __VA_ARGS__)
+/**
+ * Convenience macro that invokes cxFree() with the cxDefaultAllocator.
+ */
+#define cxFreeDefault(...) cxFree(cxDefaultAllocator, __VA_ARGS__)
+
 #ifdef __cplusplus
 } // extern "C"
 #endif
--- a/src/cx/array_list.h	Thu May 15 16:02:54 2025 +0200
+++ b/src/cx/array_list.h	Thu May 15 16:12:09 2025 +0200
@@ -134,7 +134,7 @@
 #define cx_array_initialize(array, capacity) \
         array##_capacity = capacity; \
         array##_size = 0; \
-        array = cxMalloc(cxDefaultAllocator, sizeof(array[0]) * capacity)
+        array = cxMallocDefault(sizeof(array[0]) * capacity)
 
 /**
  * Initializes an array with the given capacity using the specified allocator.
--- a/src/cx/tree.h	Thu May 15 16:02:54 2025 +0200
+++ b/src/cx/tree.h	Thu May 15 16:12:09 2025 +0200
@@ -213,7 +213,7 @@
  */
 cx_attr_nonnull
 static inline void cxTreeIteratorDispose(CxTreeIterator *iter) {
-    cxFree(cxDefaultAllocator, iter->stack);
+    cxFreeDefault(iter->stack);
     iter->stack = NULL;
 }
 
@@ -226,7 +226,7 @@
     struct cx_tree_visitor_queue_s *q = visitor->queue_next;
     while (q != NULL) {
         struct cx_tree_visitor_queue_s *next = q->next;
-        cxFree(cxDefaultAllocator, q);
+        cxFreeDefault(q);
         q = next;
     }
 }
--- a/src/json.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/json.c	Thu May 15 16:12:09 2025 +0200
@@ -500,7 +500,7 @@
 
         if (all_printable && escape) {
             size_t capa = str.length + 32;
-            char *space = cxMalloc(cxDefaultAllocator, capa);
+            char *space = cxMallocDefault(capa);
             if (space == NULL) return cx_mutstrn(NULL, 0);
             cxBufferInit(&buf, space, capa, NULL, CX_BUFFER_AUTO_EXTEND);
             cxBufferWrite(str.ptr, 1, i, &buf);
@@ -631,10 +631,10 @@
 void cxJsonDestroy(CxJson *json) {
     cxBufferDestroy(&json->buffer);
     if (json->states != json->states_internal) {
-        cxFree(cxDefaultAllocator, json->states);
+        cxFreeDefault(json->states);
     }
     if (json->vbuf != json->vbuf_internal) {
-        cxFree(cxDefaultAllocator, json->vbuf);
+        cxFreeDefault(json->vbuf);
     }
     cxJsonValueFree(json->parsed);
     json->parsed = NULL;
@@ -984,67 +984,67 @@
         if (values[i] == NULL) break;
         cxJsonValueFree(values[i]);
     }
-    cxFree(cxDefaultAllocator, values);
+    cxFreeDefault(values);
 }
 // LCOV_EXCL_STOP
 
 int cxJsonArrAddNumbers(CxJsonValue* arr, const double* num, size_t count) {
-    CxJsonValue** values = cxCalloc(cxDefaultAllocator, count, sizeof(CxJsonValue*));
+    CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
     if (values == NULL) return -1;
     for (size_t i = 0; i < count; i++) {
         values[i] = cxJsonCreateNumber(arr->allocator, num[i]);
         if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
     }
     int ret = cxJsonArrAddValues(arr, values, count);
-    cxFree(cxDefaultAllocator, values);
+    cxFreeDefault(values);
     return ret;
 }
 
 int cxJsonArrAddIntegers(CxJsonValue* arr, const int64_t* num, size_t count) {
-    CxJsonValue** values = cxCalloc(cxDefaultAllocator, count, sizeof(CxJsonValue*));
+    CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
     if (values == NULL) return -1;
     for (size_t i = 0; i < count; i++) {
         values[i] = cxJsonCreateInteger(arr->allocator, num[i]);
         if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
     }
     int ret = cxJsonArrAddValues(arr, values, count);
-    cxFree(cxDefaultAllocator, values);
+    cxFreeDefault(values);
     return ret;
 }
 
 int cxJsonArrAddStrings(CxJsonValue* arr, const char* const* str, size_t count) {
-    CxJsonValue** values = cxCalloc(cxDefaultAllocator, count, sizeof(CxJsonValue*));
+    CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
     if (values == NULL) return -1;
     for (size_t i = 0; i < count; i++) {
         values[i] = cxJsonCreateString(arr->allocator, str[i]);
         if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
     }
     int ret = cxJsonArrAddValues(arr, values, count);
-    cxFree(cxDefaultAllocator, values);
+    cxFreeDefault(values);
     return ret;
 }
 
 int cxJsonArrAddCxStrings(CxJsonValue* arr, const cxstring* str, size_t count) {
-    CxJsonValue** values = cxCalloc(cxDefaultAllocator, count, sizeof(CxJsonValue*));
+    CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
     if (values == NULL) return -1;
     for (size_t i = 0; i < count; i++) {
         values[i] = cxJsonCreateCxString(arr->allocator, str[i]);
         if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
     }
     int ret = cxJsonArrAddValues(arr, values, count);
-    cxFree(cxDefaultAllocator, values);
+    cxFreeDefault(values);
     return ret;
 }
 
 int cxJsonArrAddLiterals(CxJsonValue* arr, const CxJsonLiteral* lit, size_t count) {
-    CxJsonValue** values = cxCalloc(cxDefaultAllocator, count, sizeof(CxJsonValue*));
+    CxJsonValue** values = cxCallocDefault(count, sizeof(CxJsonValue*));
     if (values == NULL) return -1;
     for (size_t i = 0; i < count; i++) {
         values[i] = cxJsonCreateLiteral(arr->allocator, lit[i]);
         if (values[i] == NULL) { json_arr_free_temp(values, count); return -1; }
     }
     int ret = cxJsonArrAddValues(arr, values, count);
-    cxFree(cxDefaultAllocator, values);
+    cxFreeDefault(values);
     return ret;
 }
 
--- a/src/linked_list.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/linked_list.c	Thu May 15 16:12:09 2025 +0200
@@ -401,7 +401,7 @@
 ) {
     void *sbo[CX_LINKED_LIST_SORT_SBO_SIZE];
     void **sorted = length >= CX_LINKED_LIST_SORT_SBO_SIZE ?
-                    cxMalloc(cxDefaultAllocator, sizeof(void *) * length) : sbo;
+                    cxMallocDefault(sizeof(void *) * length) : sbo;
     if (sorted == NULL) abort();
     void *rc, *lc;
 
@@ -439,7 +439,7 @@
     *begin = sorted[0];
     *end = sorted[length - 1];
     if (sorted != sbo) {
-        cxFree(cxDefaultAllocator, sorted);
+        cxFreeDefault(sorted);
     }
 }
 
--- a/src/list.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/list.c	Thu May 15 16:12:09 2025 +0200
@@ -354,7 +354,7 @@
 void cx_list_default_sort(struct cx_list_s *list) {
     size_t elem_size = list->collection.elem_size;
     size_t list_size = list->collection.size;
-    void *tmp = cxMalloc(cxDefaultAllocator, elem_size * list_size);
+    void *tmp = cxMallocDefault(elem_size * list_size);
     if (tmp == NULL) abort();
 
     // copy elements from source array
@@ -377,7 +377,7 @@
         loc += elem_size;
     }
 
-    cxFree(cxDefaultAllocator, tmp);
+    cxFreeDefault(tmp);
 }
 
 int cx_list_default_swap(struct cx_list_s *list, size_t i, size_t j) {
@@ -387,7 +387,7 @@
 
     size_t elem_size = list->collection.elem_size;
 
-    void *tmp = cxMalloc(cxDefaultAllocator, elem_size);
+    void *tmp = cxMallocDefault(elem_size);
     if (tmp == NULL) return 1;
 
     void *ip = invoke_list_func(at, list, i);
@@ -397,7 +397,7 @@
     memcpy(ip, jp, elem_size);
     memcpy(jp, tmp, elem_size);
 
-    cxFree(cxDefaultAllocator, tmp);
+    cxFreeDefault(tmp);
 
     return 0;
 }
--- a/src/mempool.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/mempool.c	Thu May 15 16:12:09 2025 +0200
@@ -105,7 +105,7 @@
 
     struct cx_mempool_memory_s *mem, *newm;
     mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func));
-    newm = cxRealloc(cxDefaultAllocator, mem, n + sizeof(cx_destructor_func));
+    newm = cxReallocDefault(mem, n + sizeof(cx_destructor_func));
 
     if (newm == NULL) return NULL;
     if (mem != newm) {
@@ -136,7 +136,7 @@
             if (mem->destructor) {
                 mem->destructor(mem->c);
             }
-            cxFree(cxDefaultAllocator, mem);
+            cxFreeDefault(mem);
             size_t last_index = pool->size - 1;
             if (i != last_index) {
                 pool->data[i] = pool->data[last_index];
@@ -157,11 +157,11 @@
         if (mem->destructor) {
             mem->destructor(mem->c);
         }
-        cxFree(cxDefaultAllocator, mem);
+        cxFreeDefault(mem);
     }
-    cxFree(cxDefaultAllocator, pool->data);
-    cxFree(cxDefaultAllocator, (void*) pool->allocator);
-    cxFree(cxDefaultAllocator, pool);
+    cxFreeDefault(pool->data);
+    cxFreeDefault((void*) pool->allocator);
+    cxFreeDefault(pool);
 }
 
 void cxMempoolSetDestructor(
@@ -221,12 +221,12 @@
     }
 
     struct cx_mempool_s *pool =
-            cxMalloc(cxDefaultAllocator, sizeof(struct cx_mempool_s));
+            cxMallocDefault(sizeof(struct cx_mempool_s));
     if (pool == NULL) return NULL;
 
-    CxAllocator *provided_allocator = cxMalloc(cxDefaultAllocator, sizeof(CxAllocator));
+    CxAllocator *provided_allocator = cxMallocDefault(sizeof(CxAllocator));
     if (provided_allocator == NULL) { // LCOV_EXCL_START
-        cxFree(cxDefaultAllocator, pool);
+        cxFreeDefault(pool);
         return NULL;
     } // LCOV_EXCL_STOP
     provided_allocator->cl = &cx_mempool_allocator_class;
@@ -234,10 +234,10 @@
 
     pool->allocator = provided_allocator;
 
-    pool->data = cxMalloc(cxDefaultAllocator, poolsize);
+    pool->data = cxMallocDefault(poolsize);
     if (pool->data == NULL) { // LCOV_EXCL_START
-        cxFree(cxDefaultAllocator, provided_allocator);
-        cxFree(cxDefaultAllocator, pool);
+        cxFreeDefault(provided_allocator);
+        cxFreeDefault(pool);
         return NULL;
     } // LCOV_EXCL_STOP
 
@@ -249,7 +249,7 @@
 }
 
 static void cx_mempool_free_transferred_allocator(void *al) {
-    cxFree(cxDefaultAllocator, al);
+    cxFreeDefault(al);
 }
 
 int cxMempoolTransfer(
@@ -265,7 +265,7 @@
     }
 
     // allocate a replacement allocator for the source pool
-    CxAllocator *new_source_allocator = cxMalloc(cxDefaultAllocator, sizeof(CxAllocator));
+    CxAllocator *new_source_allocator = cxMallocDefault(sizeof(CxAllocator));
     if (new_source_allocator == NULL) { // LCOV_EXCL_START
         return 1;
     } // LCOV_EXCL_STOP
--- a/src/printf.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/printf.c	Thu May 15 16:12:09 2025 +0200
@@ -68,7 +68,7 @@
         return (int) wfc(buf, 1, ret, stream);
     } else {
         int len = ret + 1;
-        char *newbuf = cxMalloc(cxDefaultAllocator, len);
+        char *newbuf = cxMallocDefault(len);
         if (!newbuf) { // LCOV_EXCL_START
             va_end(ap2);
             return -1;
@@ -79,7 +79,7 @@
         if (ret > 0) {
             ret = (int) wfc(newbuf, 1, ret, stream);
         }
-        cxFree(cxDefaultAllocator, newbuf);
+        cxFreeDefault(newbuf);
     }
     return ret;
 }
--- a/src/properties.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/properties.c	Thu May 15 16:12:09 2025 +0200
@@ -287,7 +287,7 @@
         cx_attr_unused CxProperties *prop,
         CxPropertiesSource *src
 ) {
-    src->data_ptr = cxMalloc(cxDefaultAllocator, src->data_size);
+    src->data_ptr = cxMallocDefault(src->data_size);
     if (src->data_ptr == NULL) return 1;
     return 0;
 }
@@ -296,7 +296,7 @@
         cx_attr_unused CxProperties *prop,
         CxPropertiesSource *src
 ) {
-    cxFree(cxDefaultAllocator, src->data_ptr);
+    cxFreeDefault(src->data_ptr);
 }
 
 CxPropertiesSource cxPropertiesStringSource(cxstring str) {
--- a/src/streams.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/streams.c	Thu May 15 16:12:09 2025 +0200
@@ -58,7 +58,7 @@
         lbuf = buf;
     } else {
         if (bufsize == 0) bufsize = CX_STREAM_BCOPY_BUF_SIZE;
-        lbuf = cxMalloc(cxDefaultAllocator, bufsize);
+        lbuf = cxMallocDefault(bufsize);
         if (lbuf == NULL) return 0;
     }
 
@@ -75,7 +75,7 @@
     }
 
     if (lbuf != buf) {
-        cxFree(cxDefaultAllocator, lbuf);
+        cxFreeDefault(lbuf);
     }
 
     return ncp;
--- a/src/string.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/string.c	Thu May 15 16:12:09 2025 +0200
@@ -65,7 +65,7 @@
 
 void cx_strfree(cxmutstr *str) {
     if (str == NULL) return;
-    cxFree(cxDefaultAllocator, str->ptr);
+    cxFreeDefault(str->ptr);
     str->ptr = NULL;
     str->length = 0;
 }
@@ -287,7 +287,7 @@
     // if the pattern exceeds static prefix table, allocate on the heap
     const bool useheap = needle.length >= CX_STRSTR_SBO_SIZE;
     register size_t *ptable = useheap
-        ? cxCalloc(cxDefaultAllocator, needle.length + 1, sizeof(size_t))
+        ? cxCallocDefault(needle.length + 1, sizeof(size_t))
         : s_prefix_table;
 
     // keep counter in registers
@@ -326,7 +326,7 @@
 
     // if prefix table was allocated on the heap, free it
     if (useheap) {
-        cxFree(cxDefaultAllocator, ptable);
+        cxFreeDefault(ptable);
     }
 
     return result;
--- a/src/tree.c	Thu May 15 16:02:54 2025 +0200
+++ b/src/tree.c	Thu May 15 16:12:09 2025 +0200
@@ -326,7 +326,7 @@
                     // invalidate the iterator and free the node stack
                     iter->node = iter->node_next = NULL;
                     iter->stack_capacity = iter->depth = 0;
-                    cxFree(cxDefaultAllocator, iter->stack);
+                    cxFreeDefault(iter->stack);
                     iter->stack = NULL;
                 } else {
                     // the parent node can be obtained from the top of stack
@@ -386,7 +386,7 @@
     iter.node = root;
     if (root != NULL) {
         iter.stack_capacity = 16;
-        iter.stack = cxMalloc(cxDefaultAllocator, sizeof(void *) * 16);
+        iter.stack = cxMallocDefault(sizeof(void *) * 16);
         iter.stack[0] = root;
         iter.counter = 1;
         iter.depth = 1;
@@ -416,7 +416,7 @@
     node = tree_next(node);
     while (node != NULL) {
         struct cx_tree_visitor_queue_s *q;
-        q = cxMalloc(cxDefaultAllocator, sizeof(struct cx_tree_visitor_queue_s));
+        q = cxMallocDefault(sizeof(struct cx_tree_visitor_queue_s));
         q->depth = iter->queue_last->depth;
         q->node = node;
         iter->queue_last->next = q;
@@ -445,7 +445,7 @@
     }
     if (children != NULL) {
         struct cx_tree_visitor_queue_s *q;
-        q = cxMalloc(cxDefaultAllocator, sizeof(struct cx_tree_visitor_queue_s));
+        q = cxMallocDefault(sizeof(struct cx_tree_visitor_queue_s));
         q->depth = iter->depth + 1;
         q->node = children;
         if (iter->queue_last == NULL) {
@@ -474,7 +474,7 @@
             assert(iter->queue_last == q);
             iter->queue_last = NULL;
         }
-        cxFree(cxDefaultAllocator, q);
+        cxFreeDefault(q);
     }
 
     // increment the node counter
--- a/tests/Makefile	Thu May 15 16:02:54 2025 +0200
+++ b/tests/Makefile	Thu May 15 16:12:09 2025 +0200
@@ -150,8 +150,9 @@
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -I../src -c $<
 
-$(TEST_DIR)/ucxtest$(OBJ_EXT): ucxtest.c ../src/cx/common.h \
- ../src/cx/test.h ../src/cx/common.h
+$(TEST_DIR)/ucxtest$(OBJ_EXT): ucxtest.c util_allocator.h \
+ ../src/cx/allocator.h ../src/cx/common.h ../src/cx/common.h \
+ ../src/cx/test.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -I../src -c $<
 
--- a/tests/test_allocator.c	Thu May 15 16:02:54 2025 +0200
+++ b/tests/test_allocator.c	Thu May 15 16:12:09 2025 +0200
@@ -37,79 +37,79 @@
 #include <errno.h>
 
 CX_TEST(test_allocator_default_malloc) {
-    void *test = cxMalloc(cxDefaultAllocator, 16);
+    void *test = cxMallocDefault(16);
     CX_TEST_DO {
         CX_TEST_ASSERT(test != NULL);
         // we cannot assert sth. but valgrind will detect an error
         memcpy(test, "0123456789ABCDEF", 16);
     }
-    cxFree(cxDefaultAllocator, test);
+    cxFreeDefault(test);
 }
 
 CX_TEST(test_allocator_default_calloc) {
-    char *test = cxCalloc(cxDefaultAllocator, 8, 2);
+    char *test = cxCallocDefault(8, 2);
     CX_TEST_DO {
         CX_TEST_ASSERT(test != NULL);
         for (int i = 0; i < 16; i++) {
             CX_TEST_ASSERT(test[i] == 0);
         }
     }
-    cxFree(cxDefaultAllocator, test);
+    cxFreeDefault(test);
 }
 
 CX_TEST(test_allocator_default_realloc) {
-    char *test = cxCalloc(cxDefaultAllocator, 8, 1);
+    char *test = cxCallocDefault(8, 1);
     memcpy(test, "Test", 5);
     CX_TEST_DO {
-        test = cxRealloc(cxDefaultAllocator, test, 16);
+        test = cxReallocDefault(test, 16);
         CX_TEST_ASSERT(test != NULL);
         CX_TEST_ASSERT(0 == strcmp(test, "Test"));
     }
-    cxFree(cxDefaultAllocator, test);
+    cxFreeDefault(test);
 }
 
 CX_TEST(test_allocator_default_reallocarray) {
-    char *test = cxCalloc(cxDefaultAllocator, 8, 1);
+    char *test = cxCallocDefault(8, 1);
     memcpy(test, "Test", 5);
     CX_TEST_DO {
-        test = cxReallocArray(cxDefaultAllocator, test, 16, 2);
+        test = cxReallocArrayDefault(test, 16, 2);
         CX_TEST_ASSERT(test != NULL);
         CX_TEST_ASSERT(0 == strcmp(test, "Test"));
     }
-    cxFree(cxDefaultAllocator, test);
+    cxFreeDefault(test);
 }
 
 CX_TEST(test_allocator_default_reallocarray_overflow) {
-    char *test = cxCalloc(cxDefaultAllocator, 8, 1);
+    char *test = cxCallocDefault(8, 1);
     memcpy(test, "Test", 5);
     CX_TEST_DO {
-        void *fail = cxReallocArray(cxDefaultAllocator, test, SIZE_MAX/2, 4);
+        void *fail = cxReallocArrayDefault(test, SIZE_MAX/2, 4);
         CX_TEST_ASSERT(errno == EOVERFLOW);
         CX_TEST_ASSERT(fail == NULL);
         CX_TEST_ASSERT(0 == strcmp(test, "Test"));
     }
-    cxFree(cxDefaultAllocator, test);
+    cxFreeDefault(test);
 }
 
 CX_TEST(test_allocator_default_free) {
-    void *test = cxMalloc(cxDefaultAllocator, 16);
+    void *test = cxMallocDefault(16);
     CX_TEST_DO {
         // we cannot assert sth. but valgrind will detect an error
-        cxFree(cxDefaultAllocator, test);
+        cxFreeDefault(test);
         CX_TEST_ASSERT(true);
     }
 }
 
 CX_TEST(test_allocator_reallocate) {
-    char *test = cxCalloc(cxDefaultAllocator, 8, 1);
+    char *test = cxCallocDefault(8, 1);
     memcpy(test, "Test", 5);
     CX_TEST_DO {
-        int ret = cxReallocate(cxDefaultAllocator, &test, 16);
+        int ret = cxReallocateDefault(&test, 16);
         CX_TEST_ASSERT(ret == 0);
         CX_TEST_ASSERT(test != NULL);
         CX_TEST_ASSERT(0 == strcmp(test, "Test"));
     }
-    cxFree(cxDefaultAllocator, test);
+    cxFreeDefault(test);
 }
 
 CX_TEST(test_allocator_reallocate_low_level) {
@@ -125,28 +125,28 @@
 }
 
 CX_TEST(test_allocator_reallocatearray) {
-    char *test = cxCalloc(cxDefaultAllocator, 8, 1);
+    char *test = cxCallocDefault(8, 1);
     memcpy(test, "Test", 5);
     CX_TEST_DO {
-        int ret = cxReallocateArray(cxDefaultAllocator, &test, 16, 2);
+        int ret = cxReallocateArrayDefault(&test, 16, 2);
         CX_TEST_ASSERT(ret == 0);
         CX_TEST_ASSERT(test != NULL);
         CX_TEST_ASSERT(0 == strcmp(test, "Test"));
     }
-    cxFree(cxDefaultAllocator, test);
+    cxFreeDefault(test);
 }
 
 CX_TEST(test_allocator_reallocatearray_overflow) {
-    char *test = cxCalloc(cxDefaultAllocator, 8, 1);
+    char *test = cxCallocDefault(8, 1);
     memcpy(test, "Test", 5);
     CX_TEST_DO {
-        int ret = cxReallocateArray(cxDefaultAllocator, &test, SIZE_MAX/2, 4);
+        int ret = cxReallocateArrayDefault(&test, SIZE_MAX/2, 4);
         CX_TEST_ASSERT(ret != 0);
         CX_TEST_ASSERT(errno == EOVERFLOW);
         CX_TEST_ASSERT(test != NULL);
         CX_TEST_ASSERT(0 == strcmp(test, "Test"));
     }
-    cxFree(cxDefaultAllocator, test);
+    cxFreeDefault(test);
 }
 
 CX_TEST(test_allocator_reallocatearray_low_level) {
--- a/tests/test_hash_map.c	Thu May 15 16:02:54 2025 +0200
+++ b/tests/test_hash_map.c	Thu May 15 16:12:09 2025 +0200
@@ -666,7 +666,7 @@
         for (size_t i = 0 ; i < map->collection.size ; i++) {
             CX_TEST_ASSERT(test_map_reference_get(pairs[i].key) == pairs[i].value);
             // this was cx_strdup'ed
-            cxFree(cxDefaultAllocator, (void*)pairs[i].key);
+            cxFreeDefault((void*)pairs[i].key);
         }
         free(pairs);
     }
--- a/tests/test_list.c	Thu May 15 16:02:54 2025 +0200
+++ b/tests/test_list.c	Thu May 15 16:12:09 2025 +0200
@@ -38,7 +38,7 @@
 
 CX_TEST(test_array_add) {
     CX_ARRAY_DECLARE(int, arr);
-    arr = cxCalloc(cxDefaultAllocator, 5, sizeof(int));
+    arr = cxCallocDefault(5, sizeof(int));
     arr[0] = 2;
     arr[1] = 3;
     arr[2] = 5;
@@ -71,12 +71,12 @@
         CX_TEST_ASSERT(arr_size == 6);
         CX_TEST_ASSERT(arr_capacity >= 6);
     }
-    cxFree(cxDefaultAllocator, arr);
+    cxFreeDefault(arr);
 }
 
 CX_TEST(test_array_add8) {
     CX_ARRAY_DECLARE_SIZED(int, arr, uint8_t);
-    arr = cxCalloc(cxDefaultAllocator, 5, sizeof(int));
+    arr = cxCallocDefault(5, sizeof(int));
     arr[0] = 2;
     arr[1] = 3;
     arr[2] = 5;
@@ -115,7 +115,7 @@
         CX_TEST_ASSERT(arr_size == 6);
         CX_TEST_ASSERT(arr_capacity < 128);
     }
-    cxFree(cxDefaultAllocator, arr);
+    cxFreeDefault(arr);
 }
 
 CX_TEST(test_array_copy_unsupported_width) {
@@ -139,7 +139,7 @@
         CX_TEST_ASSERT(arr_size == 0);
         CX_TEST_ASSERT(arr_capacity == 16);
     }
-    cxFree(cxDefaultAllocator, arr);
+    cxFreeDefault(arr);
 }
 
 CX_TEST(test_array_reserve) {
@@ -158,7 +158,7 @@
         CX_TEST_ASSERT(arr_size == 5);
         CX_TEST_ASSERT(arr_capacity >= 25);
     }
-    cxFree(cxDefaultAllocator, arr);
+    cxFreeDefault(arr);
 }
 
 CX_TEST(test_array_insert_sorted) {
@@ -206,7 +206,7 @@
 
         CX_TEST_ASSERT(0 == memcmp(array, expected, 18 * sizeof(int)));
     }
-    cxFree(cxDefaultAllocator, array);
+    cxFreeDefault(array);
 }
 
 CX_TEST(test_array_binary_search) {
--- a/tests/test_tree.c	Thu May 15 16:02:54 2025 +0200
+++ b/tests/test_tree.c	Thu May 15 16:12:09 2025 +0200
@@ -1503,7 +1503,7 @@
         CX_TEST_ASSERT(node != NULL);
         CX_TEST_ASSERT(node->parent == NULL);
         CX_TEST_ASSERT(node->children == NULL);
-        cxFree(cxDefaultAllocator, node);
+        cxFreeDefault(node);
         node = NULL;
         size_t added = cx_tree_add_array(
                 "/", 1, sizeof(const char *),
@@ -1516,7 +1516,7 @@
         CX_TEST_ASSERT(node != NULL);
         CX_TEST_ASSERT(node->parent == NULL);
         CX_TEST_ASSERT(node->children == NULL);
-        cxFree(cxDefaultAllocator, node);
+        cxFreeDefault(node);
     }
 }
 
@@ -1535,7 +1535,7 @@
         CX_TEST_ASSERT(result == 0);
         CX_TEST_ASSERT(root.children == node);
         CX_TEST_ASSERT(node->parent == &root);
-        cxFree(cxDefaultAllocator, node);
+        cxFreeDefault(node);
     }
 }
 

mercurial