aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorQiang Yu <[email protected]>2018-02-19 21:44:44 +0800
committerQiang Yu <[email protected]>2019-04-11 09:57:53 +0800
commitdc37942c4e1dff8b9bba96b482a6b4fae766df6f (patch)
treef7d6280f01b6b42f78372a17f1b23ec883cbe45a
parent509dd6e20b1bfa8f42966da1ea421a0b7057485c (diff)
u_dynarray: add util_dynarray_grow_cap
This is for the case that user only know a max size it wants to append to the array and enlarge the array capacity before writing into it. v2: - rename newsize to newcap - rename util_dynarray_enlarge to util_dynarray_grow_cap Signed-off-by: Qiang Yu <[email protected]> Reviewed-by: Caio Marcelo de Oliveira Filho <[email protected]>
-rw-r--r--src/util/u_dynarray.h23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/util/u_dynarray.h b/src/util/u_dynarray.h
index 9bed2b9c25c..b30fd7b1154 100644
--- a/src/util/u_dynarray.h
+++ b/src/util/u_dynarray.h
@@ -77,16 +77,14 @@ util_dynarray_clear(struct util_dynarray *buf)
#define DYN_ARRAY_INITIAL_SIZE 64
-/* use util_dynarray_trim to reduce the allocated storage */
static inline void *
-util_dynarray_resize(struct util_dynarray *buf, unsigned newsize)
+util_dynarray_ensure_cap(struct util_dynarray *buf, unsigned newcap)
{
- void *p;
- if (newsize > buf->capacity) {
+ if (newcap > buf->capacity) {
if (buf->capacity == 0)
buf->capacity = DYN_ARRAY_INITIAL_SIZE;
- while (newsize > buf->capacity)
+ while (newcap > buf->capacity)
buf->capacity *= 2;
if (buf->mem_ctx) {
@@ -96,7 +94,20 @@ util_dynarray_resize(struct util_dynarray *buf, unsigned newsize)
}
}
- p = (void *)((char *)buf->data + buf->size);
+ return (void *)((char *)buf->data + buf->size);
+}
+
+static inline void *
+util_dynarray_grow_cap(struct util_dynarray *buf, int diff)
+{
+ return util_dynarray_ensure_cap(buf, buf->size + diff);
+}
+
+/* use util_dynarray_trim to reduce the allocated storage */
+static inline void *
+util_dynarray_resize(struct util_dynarray *buf, unsigned newsize)
+{
+ void *p = util_dynarray_ensure_cap(buf, newsize);
buf->size = newsize;
return p;