Commit e8b112325c4acd54c4ea49933322a06e4c79c8ca

Ran Benita 2014-09-12T00:31:40

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>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)
 {