diff options
Diffstat (limited to 'src/nvim/garray.h')
-rw-r--r-- | src/nvim/garray.h | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/nvim/garray.h b/src/nvim/garray.h index b32bab52f7..b758fce5da 100644 --- a/src/nvim/garray.h +++ b/src/nvim/garray.h @@ -43,4 +43,30 @@ static inline void *ga_append_via_ptr(garray_T *gap, size_t item_size) return ((char *)gap->ga_data) + (item_size * (size_t)gap->ga_len++); } +/// Deep free a garray of specific type using a custom free function. +/// Items in the array as well as the array itself are freed. +/// +/// @param gap the garray to be freed +/// @param item_type type of the item in the garray +/// @param free_item_fn free function that takes (*item_type) as parameter +#define GA_DEEP_CLEAR(gap, item_type, free_item_fn) \ + do { \ + garray_T *_gap = (gap); \ + if (_gap->ga_data != NULL) { \ + for (int i = 0; i < _gap->ga_len; i++) { \ + item_type *_item = &(((item_type *)_gap->ga_data)[i]); \ + free_item_fn(_item); \ + } \ + } \ + ga_clear(_gap); \ + } while (false) + +#define FREE_PTR_PTR(ptr) free(*(ptr)) + +/// Call `free` for every pointer stored in the garray and then frees the +/// garray. +/// +/// @param gap the garray to be freed +#define GA_DEEP_CLEAR_PTR(gap) GA_DEEP_CLEAR(gap, void*, FREE_PTR_PTR) + #endif // NVIM_GARRAY_H |