aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/garray.h
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2014-12-11 19:05:58 -0500
committerJustin M. Keyes <justinkz@gmail.com>2014-12-11 19:05:58 -0500
commitab1f0bd119a7adf36c59ee389dc7115d0204eacf (patch)
treefa092586a053b42d83052f51b9baac8f36b29817 /src/nvim/garray.h
parent951d00a492c58449d3c241fa710a83051f45dcb7 (diff)
parente11a5699be83684294c8c235a5f5030e12666206 (diff)
downloadrneovim-ab1f0bd119a7adf36c59ee389dc7115d0204eacf.tar.gz
rneovim-ab1f0bd119a7adf36c59ee389dc7115d0204eacf.tar.bz2
rneovim-ab1f0bd119a7adf36c59ee389dc7115d0204eacf.zip
Merge pull request #1643 from philix/ga_deep_clear
GA_DEEP_CLEAR macro for garray memory deallocation
Diffstat (limited to 'src/nvim/garray.h')
-rw-r--r--src/nvim/garray.h26
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