aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/garray.h
diff options
context:
space:
mode:
authorFelipe Oliveira Carvalho <felipekde@gmail.com>2014-12-11 12:23:02 -0300
committerFelipe Oliveira Carvalho <felipekde@gmail.com>2014-12-11 20:22:25 -0300
commitb603404487e0daa101deacbe346aa2fc9ee255c7 (patch)
treeb0bc4000de3c1754a400e43665fbcd7731df4dab /src/nvim/garray.h
parent951d00a492c58449d3c241fa710a83051f45dcb7 (diff)
downloadrneovim-b603404487e0daa101deacbe346aa2fc9ee255c7.tar.gz
rneovim-b603404487e0daa101deacbe346aa2fc9ee255c7.tar.bz2
rneovim-b603404487e0daa101deacbe346aa2fc9ee255c7.zip
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
Diffstat (limited to 'src/nvim/garray.h')
-rw-r--r--src/nvim/garray.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/nvim/garray.h b/src/nvim/garray.h
index b32bab52f7..52493d6391 100644
--- a/src/nvim/garray.h
+++ b/src/nvim/garray.h
@@ -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