diff --git a/include/lib/util_base.h b/include/lib/util_base.h index 0f4b12c5..e09abd87 100644 --- a/include/lib/util_base.h +++ b/include/lib/util_base.h @@ -14,7 +14,10 @@ #include #include +#include + #include "zt_common.h" +#include "lib/util_libc.h" void util_hexdump(FILE *fh, const char *tag, const void *data, int cnt); void util_hexdump_grp(FILE *fh, const char *tag, const void *data, int group, @@ -37,4 +40,30 @@ static inline void util_ptr_vec_free(void **ptr_vec, int count) free(ptr_vec); } +/* + * Expand size of dynamic array (element_t *) by one element + * + * @param[in,out] array Pointer to array (element_t **) + * @param[in,out] num Pointer to integer containing number of elements + */ +#define util_expand_array(array, num) \ + do { \ + unsigned int __size = sizeof(*(*(array))); \ + *(array) = util_realloc(*(array), ++(*(num)) * __size); \ + memset(&((*(array))[*(num) - 1]), 0, __size); \ + } while (0) + +/* + * Append one element to dynamic array (element_t *) + * + * @param[in,out] array Pointer to array (element_t **) + * @param[in,out] num Pointer to integer containing number of elements + * @param[in] element Element to add (element_t) + */ +#define util_add_array(array, num, element) \ + do { \ + util_expand_array(array, num); \ + (*(array))[*(num) - 1] = (element) ; \ + } while (0) + #endif /* LIB_UTIL_BASE_H */