summaryrefslogtreecommitdiff
path: root/src/darray.h
diff options
context:
space:
mode:
authorRan Benita <ran234@gmail.com>2012-06-05 18:46:24 +0300
committerRan Benita <ran234@gmail.com>2012-06-09 12:34:57 +0300
commitbc50cdd460ba1485c84f2dfffec4fbb1ca3b4606 (patch)
tree51093b71399c39eff7c1116bc46abeab9d4b6144 /src/darray.h
parent57f184e21889e7ec72d0eb39dae6972773b706c5 (diff)
downloadxorg-lib-libxkbcommon-bc50cdd460ba1485c84f2dfffec4fbb1ca3b4606.tar.gz
darray: some changes for convenience
- Make darray_free also initialize the array back to an empty state, and stop worrying about it everywhere. - Add darray_mem, to access the underlying memory, which we do manually now using &darray_item(arr, 0). This makes a bit more clear when we actually mean to take the address of a specific item. - Add darray_copy, to make a deep copy of a darray. - Add darray_same, to test whether two darrays have the same underlying memory (e.g. if the struct itself was value copied). This should used where previously two arrays were compared for pointer equality. Signed-off-by: Ran Benita <ran234@gmail.com>
Diffstat (limited to 'src/darray.h')
-rw-r--r--src/darray.h12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/darray.h b/src/darray.h
index 2547b47..3185193 100644
--- a/src/darray.h
+++ b/src/darray.h
@@ -58,6 +58,10 @@
* size_t darray_alloc(darray(T) arr);
* bool darray_empty(darray(T) arr);
*
+ * // Access raw memory, starting from the item in offset.
+ * // Not safe, be careful, etc.
+ * T* darray_mem(darray(T) arr, size_t offset);
+ *
* Insertion (single item):
*
* void darray_append(darray(T) arr, T item);
@@ -121,10 +125,11 @@
#define darray(type) struct {type *item; size_t size; size_t alloc;}
#define darray_new() {0,0,0}
-#define darray_lit(c_array) {(c_array), sizeof(c_array) / sizeof(*(c_array)), 0}
#define darray_init(arr) do {(arr).item=0; (arr).size=0; (arr).alloc=0;} while(0)
-#define darray_free(arr) do {free((arr).item);} while(0)
+#define darray_free(arr) do {free((arr).item); darray_init(arr);} while(0)
+/* Only use for immutable darray - e.g. for static const initialzers. */
+#define darray_lit(c_array) {(c_array), sizeof(c_array) / sizeof(*(c_array)), 0}
/*
* Typedefs for darrays of common types. These are useful
@@ -163,6 +168,8 @@ typedef darray(unsigned long) darray_ulong;
#define darray_alloc(arr) ((arr).alloc)
#define darray_empty(arr) ((arr).size == 0)
+#define darray_mem(arr, offset) ((arr).item + (offset))
+#define darray_same(arr1, arr2) ((arr1).item == (arr2).item)
/*** Insertion (single item) ***/
@@ -234,6 +241,7 @@ typedef darray(unsigned long) darray_ulong;
#define darray_from_items(arr, items, count) do {size_t __count = (count); darray_resize(arr, __count); memcpy((arr).item, items, __count*sizeof(*(arr).item));} while(0)
#define darray_from_c(arr, c_array) darray_from_items(arr, c_array, sizeof(c_array)/sizeof(*(c_array)))
+#define darray_copy(arr_to, arr_from) darray_from_items(arr_to, (arr_from).item, (arr_from).size)
/*** String buffer ***/