Skip to content
Snippets Groups Projects
Commit 0fac5bcb authored by Sean Bright's avatar Sean Bright
Browse files

vector: Add AST_VECTOR_COMPACT() to reclaim wasted space

This might be useful in situations where you are loading an undetermined number
of items into a vector and don't want to keep (potentially) 2x the necessary
memory around indefinitely.

Change-Id: I9711daa0fe01783fc6f04c5710eba84f2676d7b9
parent 45a8892e
No related branches found
No related tags found
No related merge requests found
......@@ -619,6 +619,34 @@ int ast_vector_string_split(struct ast_vector_string *dest,
(vec)->current = 0; \
})
/*!
* \brief Resize a vector so that its capacity is the same as its size.
*
* \param vec Vector to compact.
*
* \return 0 on success.
* \return Non-zero on failure.
*/
#define AST_VECTOR_COMPACT(vec) ({ \
int res = 0; \
do { \
if ((vec)->max > (vec)->current) { \
size_t new_max = (vec)->current; \
typeof((vec)->elems) new_elems = ast_realloc( \
(vec)->elems, \
new_max * sizeof(*new_elems)); \
if (new_elems || (vec)->current == 0) { \
(vec)->elems = new_elems; \
(vec)->max = new_max; \
} else { \
res = -1; \
break; \
} \
} \
} while(0); \
res; \
})
/*!
* \brief Get an address of element in a vector.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment