darray: add darray_shrink() If we have a big array which can be finalized, on average we can give back 1/4 of its size, which the allocator might be able to use. Signed-off-by: Ran Benita <ran234@gmail.com>
diff --git a/src/darray.h b/src/darray.h
index d85edea..be0319f 100644
--- a/src/darray.h
+++ b/src/darray.h
@@ -166,6 +166,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)
{