2 * UCW Library -- A simple growing array of an arbitrary type
4 * (c) 2010--2014 Martin Mares <mj@ucw.cz>
10 #include <ucw/alloc.h>
12 #ifdef CONFIG_UCW_CLEAN_ABI
13 #define gary_fix ucw_gary_fix
14 #define gary_init ucw_gary_init
15 #define gary_push_helper ucw_gary_push_helper
16 #define gary_set_size ucw_gary_set_size
23 struct ucw_allocator *allocator;
26 #define GARY_HDR_SIZE ALIGN_TO(sizeof(struct gary_hdr), CPU_STRUCT_ALIGN)
27 #define GARY_HDR(ptr) ((struct gary_hdr *)((byte*)(ptr) - GARY_HDR_SIZE))
28 #define GARY_BODY(ptr) ((byte *)(ptr) + GARY_HDR_SIZE)
31 * Create a new growing array, initially containing @n elements,
32 * and let @ptr point to its first element. The memory used by the
33 * array is allocated by xmalloc().
35 #define GARY_INIT(ptr, n) (ptr) = gary_init(sizeof(*(ptr)), (n), &ucw_allocator_std)
38 * Create a growing array like GARY_INIT() does, but all newly
39 * allocated elements will be automatically zeroed.
41 #define GARY_INIT_ZERO(ptr, n) (ptr) = gary_init(sizeof(*(ptr)), (n), &ucw_allocator_zeroed)
44 * Create a growing array like GARY_INIT() does, but based upon the given
45 * <<alloc:,generic allocator>>.
47 #define GARY_INIT_ALLOC(ptr, n, a) (ptr) = gary_init(sizeof(*(ptr)), (n), (a))
50 * Create a growing array, initially containing 0 elements, but with enough
51 * space to keep @n of them without needing reallocation. The @ptr variable
52 * will point to the first element of the array.
54 #define GARY_INIT_SPACE(ptr, n) do { GARY_INIT(ptr, n); (GARY_HDR(ptr))->num_elts = 0; } while (0)
56 /** A combination of GARY_INIT_ZERO() and GARY_INIT_SPACE(). **/
57 #define GARY_INIT_SPACE_ZERO(ptr, n) do { GARY_INIT_ZERO(ptr, n); (GARY_HDR(ptr))->num_elts = 0; } while (0)
59 /** A combination of GARY_INIT_ALLOC() and GARY_INIT_SPACE(). **/
60 #define GARY_INIT_SPACE_ALLOC(ptr, n, a) do { GARY_INIT_ALLOC(ptr, n, a); (GARY_HDR(ptr))->num_elts = 0; } while (0)
62 /** Destroy a growing array and free memory used by it. If @ptr is NULL, nothing happens. **/
63 #define GARY_FREE(ptr) gary_free(ptr)
65 /** Return the current number elements of the given growing array. **/
66 #define GARY_SIZE(ptr) (GARY_HDR(ptr)->num_elts)
69 * Resize the given growing array to @n elements.
70 * The @ptr can change, if the array has to be re-allocated.
72 #define GARY_RESIZE(ptr, n) ((ptr) = gary_set_size((ptr), (n)))
74 /** Create a new growing array, or resize it if it already exists. **/
75 #define GARY_INIT_OR_RESIZE(ptr, n) (ptr) = (ptr) ? gary_set_size((ptr), (n)) : gary_init(sizeof(*(ptr)), (n), &ucw_allocator_std)
78 * Push @n elements to a growing array. That is, make space for @n more elements
79 * at the end of the array and return a pointer to the first of these elements.
80 * The @ptr can change, if the array has to be re-allocated.
82 #define GARY_PUSH_MULTI(ptr, n) ({ \
83 struct gary_hdr *_h = GARY_HDR(ptr); \
84 typeof(*(ptr)) *_c = &(ptr)[_h->num_elts]; \
87 if (_h->num_elts > _h->have_space) \
88 (ptr) = gary_push_helper((ptr), _n, (byte **) &_c); \
92 * Push a single element at the end of a growing array and return a pointer to it.
93 * The @ptr can change, if the array has to be re-allocated.
95 #define GARY_PUSH(ptr) GARY_PUSH_MULTI(ptr, 1)
98 * Pop @n elements from the end of a growing array.
99 * The @ptr can change, if the array has to be re-allocated.
101 #define GARY_POP_MULTI(ptr, n) GARY_HDR(ptr)->num_elts -= (n)
104 * Pop a single element from the end of a growing array.
105 * The @ptr can change, if the array has to be re-allocated.
107 #define GARY_POP(ptr) GARY_POP_MULTI(ptr, 1)
110 * Fix size of a growing array, returning all unused memory to the
111 * system (or more precisely, to the underlying allocator).
112 * The @ptr can change.
114 #define GARY_FIX(ptr) (ptr) = gary_fix((ptr))
116 /* Internal functions */
117 void *gary_init(size_t elt_size, size_t num_elts, struct ucw_allocator *allocator);
118 void *gary_set_size(void *array, size_t n);
119 void *gary_push_helper(void *array, size_t n, byte **cptr);
120 void *gary_fix(void *array);
122 static inline void gary_free(void *ptr)
126 struct gary_hdr *h = GARY_HDR(ptr);
127 h->allocator->free(h->allocator, h);
131 /* A forever empty gary. Used internally. */
133 extern struct gary_hdr gary_empty_hdr;
134 #define GARY_FOREVER_EMPTY GARY_BODY(&gary_empty_hdr)
136 /* Type-agnostic interface. Currently it's recommended for internal use only. */
138 #define GARY_PUSH_GENERIC(ptr) ({ \
139 struct gary_hdr *_h = GARY_HDR(ptr); \
140 void *_c = (byte *)(ptr) + _h->num_elts++ * _h->elt_size; \
141 if (_h->num_elts > _h->have_space) \
142 (ptr) = gary_push_helper((ptr), 1, (byte **) &_c); \