src/cx/array_list.h

changeset 1606
f5883f6e42e7
parent 1605
55b13f583356
child 1607
0ecb13118cac
--- a/src/cx/array_list.h	Sun Dec 14 17:30:17 2025 +0100
+++ b/src/cx/array_list.h	Sun Dec 14 21:14:34 2025 +0100
@@ -49,6 +49,40 @@
  */
 CX_EXPORT extern const unsigned cx_array_swap_sbo_size;
 
+#define CX_ARRAY(type, name) \
+    struct { \
+        type *data; \
+        size_t size; \
+        size_t capacity; \
+    } name
+
+typedef struct cx_array_s {
+    void *data;
+    size_t size;
+    size_t capacity;
+} CxArray;
+
+cx_attr_nonnull
+CX_EXPORT int cx_array_init_(CxArray *array, const CxAllocator *allocator, size_t elem_size, size_t capacity);
+
+#define cx_array_init(array, capacity) cx_array_init_((CxArray*)&array, cxDefaultAllocator, sizeof((array).data[0]), capacity)
+
+#define cx_array_init_a(array, allocator, capacity) cx_array_init_((CxArray*)&array, allocator, sizeof((array).data[0]), capacity)
+
+cx_attr_nonnull
+CX_EXPORT int cx_array_add_(CxArray *array, const CxAllocator *allocator, size_t elem_size, void *element);
+
+#define cx_array_add(array, element) cx_array_add_((CxArray*)&array, cxDefaultAllocator, sizeof((array).data[0]), element)
+
+#define cx_array_add_a(array, allocator, element) cx_array_add_((CxArray*)&array, cxDefaultAllocator, sizeof((array).data[0]), element)
+
+cx_attr_nonnull
+CX_EXPORT void cx_array_free_(const CxAllocator *allocator, CxArray *array);
+
+#define cx_array_free(array) cx_array_free_(cxDefaultAllocator, (CxArray*)&array)
+
+#define cx_array_free_a(allocator, array) cx_array_free_(allocator, (CxArray*)&array)
+
 /**
  * Declares variables for an array that can be used with the convenience macros.
  *
@@ -372,32 +406,6 @@
 #define cx_array_simple_reserve(array, count) \
     cx_array_simple_reserve_a(NULL, array, count)
 
-/**
- * Adds an element to an array with the possibility of allocating more space.
- *
- * The element @p elem is added to the end of the @p target array which contains
- * @p size elements, already. The @p capacity must point to a variable denoting
- * the current maximum number of elements the array can hold.
- *
- * If the capacity is insufficient to hold the new element, an attempt to
- * increase the @p capacity is made and the new capacity is written back.
- *
- * The \@ SIZE_TYPE is flexible and can be any unsigned integer type.
- * It is important, however, that @p size and @p capacity are pointers to
- * variables of the same type.
- *
- * @param target (@c void**) a pointer to the target array
- * @param size (@c SIZE_TYPE*) a pointer to the size of the target array
- * @param capacity (@c SIZE_TYPE*) a pointer to the capacity of the target array
- * @param elem_size (@c size_t) the size of one element
- * @param elem (@c void*) a pointer to the element to add
- * @param reallocator (@c CxArrayReallocator*) the array reallocator to use
- * @retval zero success
- * @retval non-zero failure
- */
-#define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \
-    cx_array_copy((void**)(target), size, capacity, sizeof(*(size)), \
-    *(size), elem, elem_size, 1, reallocator)
 
 /**
  * Convenience macro that uses cx_array_add() with a default layout and

mercurial