Merge "vector: Add AST_VECTOR_COMPACT() to reclaim wasted space" into 16

This commit is contained in:
Friendly Automation 2019-03-18 06:29:10 -05:00 committed by Gerrit Code Review
commit f151f72870
1 changed files with 28 additions and 0 deletions

View File

@ -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.
*