atom: describe how this odd data structure works
[platform/upstream/libxkbcommon.git] / src / darray.h
index d85edea..8e87c94 100644 (file)
@@ -23,7 +23,7 @@
 #ifndef CCAN_DARRAY_H
 #define CCAN_DARRAY_H
 
-/* Originally taken from: http://ccodearchive.net/info/darray.html
+/* Originally taken from: https://ccodearchive.net/info/darray.html
  * But modified for libxkbcommon. */
 
 #include <stdlib.h>
@@ -104,12 +104,16 @@ typedef darray (unsigned long)  darray_ulong;
 #define darray_from_items(arr, items, count) do { \
     unsigned __count = (count); \
     darray_resize(arr, __count); \
-    memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
+    if (__count != 0) \
+        memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
 } while (0)
 
 #define darray_copy(arr_to, arr_from) \
     darray_from_items((arr_to), (arr_from).item, (arr_from).size)
 
+#define darray_concat(arr_to, arr_from) \
+    darray_append_items((arr_to), (arr_from).item, (arr_from).size)
+
 /*** String buffer ***/
 
 #define darray_append_string(arr, str) do { \
@@ -166,6 +170,12 @@ typedef darray (unsigned long)  darray_ulong;
                                               sizeof(*(arr).item))); \
 } while (0)
 
+#define darray_shrink(arr) do { \
+    if ((arr).size > 0) \
+        (arr).item = realloc((arr).item, \
+                             ((arr).alloc = (arr).size) * sizeof(*(arr).item)); \
+} while (0)
+
 static inline unsigned
 darray_next_alloc(unsigned alloc, unsigned need, unsigned itemSize)
 {