Skip to content

Commit

Permalink
lib/array - give arrays the non-freeing deinit that strings and buffe…
Browse files Browse the repository at this point in the history
…rs 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 <phil@dovecot.fi>
  • Loading branch information
Phil Carmody authored and sirainen committed Aug 30, 2018
1 parent 52c1c99 commit 4adae0e
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lib/array.h
Expand Up @@ -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)
{
Expand Down

0 comments on commit 4adae0e

Please sign in to comment.