garray.h (1472B)
1 #pragma once 2 3 #include <stdbool.h> 4 #include <stddef.h> 5 6 #include "nvim/garray_defs.h" // IWYU pragma: keep 7 #include "nvim/macros_defs.h" // IWYU pragma: keep 8 #include "nvim/memory.h" 9 10 #define GA_EMPTY(ga_ptr) ((ga_ptr)->ga_len <= 0) 11 12 #define GA_APPEND(item_type, gap, item) \ 13 do { \ 14 ga_grow(gap, 1); \ 15 ((item_type *)(gap)->ga_data)[(gap)->ga_len] = (item); \ 16 (gap)->ga_len++; \ 17 } while (0) 18 19 #define GA_APPEND_VIA_PTR(item_type, gap) \ 20 ga_append_via_ptr(gap, sizeof(item_type)) 21 22 #include "garray.h.generated.h" 23 24 /// Deep free a garray of specific type using a custom free function. 25 /// Items in the array as well as the array itself are freed. 26 /// 27 /// @param gap the garray to be freed 28 /// @param item_type type of the item in the garray 29 /// @param free_item_fn free function that takes (item_type *) as parameter 30 #define GA_DEEP_CLEAR(gap, item_type, free_item_fn) \ 31 do { \ 32 garray_T *_gap = (gap); \ 33 if (_gap->ga_data != NULL) { \ 34 for (int i = 0; i < _gap->ga_len; i++) { \ 35 item_type *_item = &(((item_type *)_gap->ga_data)[i]); \ 36 free_item_fn(_item); \ 37 } \ 38 } \ 39 ga_clear(_gap); \ 40 } while (false) 41 42 #define FREE_PTR_PTR(ptr) xfree(*(ptr)) 43 44 /// Call `free` for every pointer stored in the garray and then frees the 45 /// garray. 46 /// 47 /// @param gap the garray to be freed 48 #define GA_DEEP_CLEAR_PTR(gap) GA_DEEP_CLEAR(gap, void *, FREE_PTR_PTR) 49 50 #define GA_CONCAT_LITERAL(gap, s) ga_concat_len((gap), S_LEN(s))