mirror of
https://github.com/neovim/neovim.git
synced 2025-10-03 00:18:33 +00:00
GA_DEEP_CLEAR macro for garray memory deallocation
Used to free garrays of `salitem_T` and `fromto_T` in spell.c, and garray `wcmd_T` in ex_docmd.c. Helped-by: Jiaqi Li
This commit is contained in:
@@ -43,4 +43,22 @@ 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)
|
||||
|
||||
#endif // NVIM_GARRAY_H
|
||||
|
Reference in New Issue
Block a user