garray.h
Go to the documentation of this file.
1 #ifndef NVIM_GARRAY_H
2 #define NVIM_GARRAY_H
3 
4 #include <stddef.h> // for size_t
5 
6 #include "nvim/types.h" // for char_u
7 #include "nvim/log.h"
8 
12 typedef struct growarray {
13  int ga_len; // current number of items used
14  int ga_maxlen; // maximum number of items possible
15  int ga_itemsize; // sizeof(item)
16  int ga_growsize; // number of items to grow each time
17  void *ga_data; // pointer to the first item
18 } garray_T;
19 
20 #define GA_EMPTY_INIT_VALUE { 0, 0, 0, 1, NULL }
21 
22 #define GA_EMPTY(ga_ptr) ((ga_ptr)->ga_len <= 0)
23 
24 #define GA_APPEND(item_type, gap, item) \
25  do { \
26  ga_grow(gap, 1); \
27  ((item_type *)(gap)->ga_data)[(gap)->ga_len++] = (item); \
28  } while (0)
29 
30 #define GA_APPEND_VIA_PTR(item_type, gap) \
31  ga_append_via_ptr(gap, sizeof(item_type))
32 
33 #ifdef INCLUDE_GENERATED_DECLARATIONS
34 # include "garray.h.generated.h"
35 #endif
36 
37 static inline void *ga_append_via_ptr(garray_T *gap, size_t item_size)
38 {
39  if ((int)item_size != gap->ga_itemsize) {
40  WLOG("wrong item size (%zu), should be %d", item_size, gap->ga_itemsize);
41  }
42  ga_grow(gap, 1);
43  return ((char *)gap->ga_data) + (item_size * (size_t)gap->ga_len++);
44 }
45 
52 #define GA_DEEP_CLEAR(gap, item_type, free_item_fn) \
53  do { \
54  garray_T *_gap = (gap); \
55  if (_gap->ga_data != NULL) { \
56  for (int i = 0; i < _gap->ga_len; i++) { \
57  item_type *_item = &(((item_type *)_gap->ga_data)[i]); \
58  free_item_fn(_item); \
59  } \
60  } \
61  ga_clear(_gap); \
62  } while (false)
63 
64 #define FREE_PTR_PTR(ptr) xfree(*(ptr))
65 
70 #define GA_DEEP_CLEAR_PTR(gap) GA_DEEP_CLEAR(gap, void*, FREE_PTR_PTR)
71 
72 #endif // NVIM_GARRAY_H
#define WLOG(...)
Definition: log.h:49
int ga_maxlen
Definition: garray.h:14
void ga_grow(garray_T *gap, int n)
Definition: garray.c:77
int ga_itemsize
Definition: garray.h:15
int ga_growsize
Definition: garray.h:16
Definition: garray.h:12
struct growarray garray_T
int ga_len
Definition: garray.h:13
void * ga_data
Definition: garray.h:17