From 4adae0e1a832f88f5737a13c6fe2537d9e47835c Mon Sep 17 00:00:00 2001 From: Phil Carmody Date: Wed, 29 Aug 2018 15:28:31 +0300 Subject: [PATCH] lib/array - give arrays the non-freeing deinit that strings and buffers have Buffers provide the ability to free the control block but preserve and return the previously controlled data to the caller: buffer_free_without_data(). As wrapped buffers, strings also have this functionality: str_free_without_data(). (They have to do that, as their buffer implementation is encapsulated away out of sight to the str.h user.) Arrays, also wrapped buffers, are missing this capability under the 'array' name, you have to use the buffer function call, which involves diving into the guts of the array implementation (because arrays do not hide their implementation like strings do), and also sacrifices array's type safety. With this inline helper, it should be simple, obvious, clean and safe. Signed-off-by: Phil Carmody --- src/lib/array.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib/array.h b/src/lib/array.h index 3a0e179801..ca37fcfbf4 100644 --- a/src/lib/array.h +++ b/src/lib/array.h @@ -140,6 +140,14 @@ array_free_i(struct array *array) #define array_free(array) \ array_free_i(&(array)->arr) +static inline void * ATTR_WARN_UNUSED_RESULT +array_free_without_data_i(struct array *array) +{ + return buffer_free_without_data(&array->buffer); +} +#define array_free_without_data(array) \ + ARRAY_TYPE_CAST_MODIFIABLE(array)array_free_without_data_i(&(array)->arr) + static inline bool array_is_created_i(const struct array *array) {