src/cx/array_list.h

changeset 998
bb196054f3fd
parent 993
b642eca4b956
child 999
84fc42b04d3b
--- a/src/cx/array_list.h	Thu Nov 28 20:59:11 2024 +0100
+++ b/src/cx/array_list.h	Mon Dec 02 20:58:17 2024 +0100
@@ -52,16 +52,37 @@
 /**
  * Declares variables for an array that can be used with the convenience macros.
  *
+ * @param type the type of the data
+ * @param name the name of the array
+ * @param size_type the type of the size (should be uint8_t, uint16_t, uint32_t, or size_t)
+ *
  * @see cx_array_simple_add()
  * @see cx_array_simple_copy()
  * @see cx_array_initialize()
  * @see cx_array_simple_add_sorted()
  * @see cx_array_simple_insert_sorted()
  */
-#define CX_ARRAY_DECLARE(type, name) \
+#define CX_ARRAY_DECLARE_SIZED(type, name, size_type) \
     type * name;                     \
-    size_t name##_size;              \
-    size_t name##_capacity
+    size_type name##_size;           \
+    size_type name##_capacity
+
+/**
+ * Declares variables for an array that can be used with the convenience macros.
+ *
+ * The size and capacity variables will have `size_t` type.
+ * Use #CX_ARRAY_DECLARE_SIZED() to specify a different type.
+ *
+ * @param type the type of the data
+ * @param name the name of the array
+ *
+ * @see cx_array_simple_add()
+ * @see cx_array_simple_copy()
+ * @see cx_array_initialize()
+ * @see cx_array_simple_add_sorted()
+ * @see cx_array_simple_insert_sorted()
+ */
+#define CX_ARRAY_DECLARE(type, name) CX_ARRAY_DECLARE_SIZED(type, name, size_t)
 
 /**
  * Initializes an array declared with CX_ARRAY_DECLARE().
@@ -163,9 +184,14 @@
  * If the \p capacity is also insufficient to hold the new data, a reallocation
  * attempt is made with the specified \p reallocator.
  *
+ * The \p width refers to the size and capacity. Both must have the same width.
+ * Supported are 0, 8, 16, and 32, as well as 64 if running on a 64 bit
+ * architecture. If set to zero, the native word width is used.
+ *
  * @param target a pointer to the target array
  * @param size a pointer to the size of the target array
  * @param capacity a pointer to the capacity of the target array
+ * @param width the width in bytes for the \p size and \p capacity or zero for default
  * @param index the index where the copied elements shall be placed
  * @param src the source array
  * @param elem_size the size of one element
@@ -176,8 +202,9 @@
 cx_attr_nonnull
 int cx_array_copy(
         void **target,
-        size_t *size,
-        size_t *capacity,
+        void *size,
+        void *capacity,
+        unsigned width,
         size_t index,
         const void *src,
         size_t elem_size,
@@ -198,7 +225,8 @@
  */
 #define cx_array_simple_copy(array, index, src, count) \
     cx_array_copy((void**)&(array), &(array##_size), &(array##_capacity), \
-    index, src, sizeof((array)[0]), count, cx_array_default_reallocator)
+    8*sizeof(array##_size), index, src, sizeof((array)[0]), count, \
+    cx_array_default_reallocator)
 
 /**
  * Adds an element to an array with the possibility of allocating more space.
@@ -219,7 +247,8 @@
  * @return zero on success, non-zero on failure
  */
 #define cx_array_add(target, size, capacity, elem_size, elem, reallocator) \
-    cx_array_copy((void**)(target), size, capacity, *(size), elem, elem_size, 1, reallocator)
+    cx_array_copy((void**)(target), size, capacity, 8*sizeof(*(size)), \
+    *(size), elem, elem_size, 1, reallocator)
 
 /**
  * Convenience macro that uses cx_array_add() with a default layout and

mercurial