A bit of cleanup of the array API.
[apps/pfixtools.git] / common / array.h
index dadce33..d32eeef 100644 (file)
 #define PRIV_ARRAY(Type)                                                       \
     struct {                                                                   \
         Type    *data;                                                         \
-        ssize_t len;                                                           \
-        ssize_t size;                                                          \
+        uint32_t len;                                                          \
+        uint32_t size;                                                         \
+        unsigned locked : 1;                                                   \
     }
 
+/** Declare type PA(Type).
+ */
 #define PARRAY(Type)                                                           \
     typedef PRIV_ARRAY(Type*) Type ## _ptr_array_t;                            \
     static inline Type ## _ptr_array_t *Type ## _ptr_array_new(void)           \
     static inline void Type ## _ptr_array_delete(Type ## _ptr_array_t **array) \
     {                                                                          \
         if (*array) {                                                          \
+            if ((*array)->locked) {                                            \
+                array_unlock(**array);                                         \
+            }                                                                  \
             array_wipe(**array);                                               \
             p_delete(array);                                                   \
         }                                                                      \
     }
 
+/** Declare types A(Type) and PA(Type).
+ */
 #define ARRAY(Type)                                                            \
     typedef PRIV_ARRAY(Type) Type ## _array_t;                                 \
                                                                                \
@@ -73,6 +81,9 @@
     static inline void Type ## _array_delete(Type ## _array_t **array)         \
     {                                                                          \
         if (*array) {                                                          \
+            if ((*array)->locked) {                                            \
+                array_unlock(**array);                                         \
+            }                                                                  \
             array_wipe(**array);                                               \
             p_delete(array);                                                   \
         }                                                                      \
                                                                                \
     PARRAY(Type)
 
+/** Type A(Type) is a dynamic array of elements of type @c Type.
+ */
 #define A(Type) Type ## _array_t
+
+/** Type PA(Type) is a dynamic array of pointers to type @c Type.
+ */
 #define PA(Type) Type ## _ptr_array_t
 
-#define ARRAY_INIT { NULL, 0, 0 }
+#define ARRAY_INIT { NULL, 0, 0, false }
 
 #define array_init(array) (array) = ARRAY_INIT
+
+#define array_can_edit(array) (!(array).locked)
+
+#define array_ensure_can_edit(array)                                           \
+    assert(array_can_edit(array) && "Trying to edit array while it is locked")
+
 #define array_wipe(array)                                                      \
     do {                                                                       \
+        array_ensure_can_edit(array);                                          \
         p_delete(&(array).data);                                               \
         (array).len  = 0;                                                      \
         (array).size = 0;                                                      \
     } while (0)
-#define array_add(array, obj)                                                  \
-    do {                                                                       \
-        array_ensure_capacity_delta(array, 1);                                 \
-        (array).data[(array).len++] = (obj);                                   \
-    } while (0)
-#define array_append(array, objs, len)                                         \
-    do {                                                                       \
-        const ssize_t __len = (len);                                           \
-        array_ensure_capacity_delta(array, __len);                             \
-        memcpy((array).data + (array).len, objs,                               \
-               __len * sizeof(*(array).data));                                 \
-        (array).len += __len;                                                  \
-    } while (0)
+
+
+/******* MEMORY MANAGEMENT *******/
+
+/** Return the len of the array (number of elements contained in the array).
+ */
+#define array_len(array) ((array).len)
+
+/** Return the capacity of the array (number of elements the array can contain
+ * without growing its internal buffer).
+ */
+#define array_size(array) ((array).size)
+
+/** Return the number of free places in the array.
+ */
+#define array_free_space(array) (array_size(array) - array_len(array))
+
+/** Return the size of an element of the array.
+ */
+#define array_elt_len(array) (sizeof(*(array).data))
+
+/** Return the number of bytes used by the content of the array.
+ */
+#define array_byte_len(array) ((array).len * array_elt_len(array))
+
+/** Ensure the capacity of the array if *at least* @c goal *elements*.
+ */
 #define array_ensure_capacity(array, goal)                                     \
     do {                                                                       \
+        array_ensure_can_edit(array);                                          \
         if ((array).size < (goal)) {                                           \
-            const ssize_t required_size = (goal);                              \
-            ssize_t next_size = (array).size;                                  \
+            const typeof((array).size) required_size = (goal);                 \
+            typeof((array).size) next_size = (array).size;                     \
             do {                                                               \
                 next_size = p_alloc_nr(next_size);                             \
             } while (next_size < required_size);                               \
             p_allocgrow(&(array).data, next_size, &(array).size);              \
         }                                                                      \
     } while (0)
+
+/** Ensure the array contains place for *at least* @c delta more elements.
+ */
 #define array_ensure_capacity_delta(array, delta)                              \
     array_ensure_capacity(array, (array).len + (delta))
+
+/** Ensure the array can contain @c goal elements.
+ */
 #define array_ensure_exact_capacity(array, goal)                               \
-    if (array_size(array) < (goal)) {                                          \
+    if ((array).size < (goal)) {                                               \
+        array_ensure_can_edit(array);                                          \
         p_allocgrow(&(array).data, (goal), &(array).size);                     \
     }
-#define array_adjust(array)                                                    \
+
+/** Shrink capacity of the array to MAX(len, @c cap).
+ */
+#define array_shrink(array, cap)                                               \
+    do {                                                                       \
+        array_ensure_can_edit(array);                                          \
+        if ((cap) < (array).size && (array).size != (array).len) {             \
+            p_shrink(&(array).data, MAX((array).len, (cap)), &(array).size);   \
+        }                                                                      \
+    } while (0)
+
+/** Ensure the capacity of the array does not exceed its len.
+ */
+#define array_adjust(array) array_shrink(array, 0)
+
+#define array_lock(array)                                                      \
+    ((array).locked                                                            \
+     || (mlock((array).data, array_byte_len(array)) == 0                       \
+         && ((array).locked = true)))
+
+#define array_unlock(array)                                                    \
+    if ((array).locked) {                                                      \
+        (void)munlock((array).data, array_byte_len(array));                    \
+        (array).locked = false;                                                \
+    }
+
+
+/******* ADDING ELEMENTS *******/
+
+#define array_add(array, obj)                                                  \
+    do {                                                                       \
+        array_ensure_capacity_delta(array, 1);                                 \
+        (array).data[(array).len++] = (obj);                                   \
+    } while (0)
+
+#define array_append(array, objs, Len)                                         \
     do {                                                                       \
-        p_shrink(&(array).data, (array).len, &(array).size);                   \
+        const typeof((array).len) __len = (Len);                               \
+        array_ensure_capacity_delta(array, __len);                             \
+        memcpy((array).data + (array).len, objs,                               \
+               __len * sizeof(*(array).data));                                 \
+        (array).len += __len;                                                  \
     } while (0)
-#define array_elt(array, n) (array).data[(n)]
-#define array_ptr(array, n) (array).data + (n)
+
+
+/******* ACCESSSING ELEMENTS ********/
+
+/** Getting the n'th element of the array.
+ */
+#define array_elt(array, n) ((array).data[(n)])
+
+#define array_first(array) array_elt(array, 0)
+#define array_last(array) array_elt(array, (array).len - 1)
+
+#define array_pop_last(array) array_elt(array, --((array).len))
+
+/** Getting a pointer to the n'th element of the array.
+ */
+#define array_ptr(array, n) ((array).data + (n))
+
+#define array_start(array) array_ptr((array), 0)
+#define array_end(array) array_ptr((array), array_len(array))
+
+/****** TRAVERSING AN ARRAY *******/
+
+/** Gives the position of pointer ptr in the array.
+ * This macro may only be used withing a loop. @ref foreach.
+ */
+#define array_pos(array, ptr) ((ptr) - array_start(array))
+
+/** Build a loop over the elements of an array.
+ *
+ * <code>
+ * A(MyType) array;
+ * ...
+ * foreach (MyType* element, array) {
+ *    do_something(element);
+ * }}
+ * </code>
+ *
+ * Warnings:
+ *  * remember the loop must be ended with }} (the foreach macro contains 
+ *    a not-matched opening brace).
+ *  * the macro uses a counter name __Ai, so you cannot build imbricated
+ *    array enumerations. You SHOULD NOT use this counter in your code since
+ *    it is part of the internal API and may change in the future.
+ */
 
 #define foreach(var, array)                                                    \
-    for (int __Ai = 0 ; __Ai < (array).len ; ++__Ai) {                         \
+    for (uint32_t __Ai = 0 ; __Ai < (array).len ; ++__Ai) {                    \
         var = array_ptr(array, __Ai);
 
+/** Execute @c action for each element of the array.
+ *
+ * <code>
+ * static void do_something(MyType *element) { ... }
+ *
+ * A(MyType) array;
+ * ...
+ * array_foreach(array, do_something);
+ * </code>
+ */
 #define array_foreach(array, action)                                           \
-    for (int __Ai = 0 ; __Ai < (array).len ; ++__Ai) {                         \
+    for (uint32_t __Ai = 0 ; __Ai < (array).len ; ++__Ai) {                    \
         action(array_ptr(array, __Ai));                                        \
     }
+
+/** Wipe each element of the array using @c wipe, then wipe the array.
+ */
 #define array_deep_wipe(array, wipe)                                           \
     do {                                                                       \
         array_foreach(array, wipe);                                            \
         array_wipe(array);                                                     \
     } while (0)
 
-#define array_lock(array)                                                      \
-    (mlock((array).data, (array).len * sizeof(*(array).data)) == 0)
-#define array_unlock(array)                                                    \
-    (void)munlock((array).data, (array).len * sizeof(*(array).data))
+
 
 ARRAY(char)
 ARRAY(int)
 ARRAY(bool)
+ARRAY(uint16_t)
 ARRAY(uint32_t)
 
 PARRAY(void)