2 * UCW Library -- A simple growing array of an arbitrary type
4 * (c) 2010--2012 Martin Mares <mj@ucw.cz>
17 #define GARY_HDR_SIZE ALIGN_TO(sizeof(struct gary_hdr), CPU_STRUCT_ALIGN)
18 #define GARY_HDR(ptr) ((struct gary_hdr *)((byte*)(ptr) - GARY_HDR_SIZE))
19 #define GARY_BODY(ptr) ((byte *)(ptr) + GARY_HDR_SIZE)
21 #define GARY_INIT(ptr, n) (ptr) = gary_init(sizeof(*(ptr)), (n), 0)
22 #define GARY_INIT_ZERO(ptr, n) (ptr) = gary_init(sizeof(*(ptr)), (n), 1)
23 #define GARY_FREE(ptr) do { if (ptr) xfree(GARY_HDR(ptr)); } while (0)
24 #define GARY_SIZE(ptr) (GARY_HDR(ptr)->num_elts)
25 #define GARY_RESIZE(ptr, n) (ptr) = gary_set_size((ptr), (n))
26 #define GARY_INIT_OR_RESIZE(ptr, n) (ptr) = (ptr) ? gary_set_size((ptr), (n)) : gary_init(sizeof(*(ptr)), (n), 0)
28 #define GARY_PUSH(ptr, n) ({ \
29 struct gary_hdr *_h = GARY_HDR(ptr); \
30 typeof(*(ptr)) *_c = &(ptr)[_h->num_elts]; \
33 if (_h->num_elts > _h->have_space) \
34 (ptr) = gary_push_helper((ptr), _n, (byte **) &_c); \
37 #define GARY_POP(ptr, n) GARY_HDR(ptr)->num_elts -= (n)
38 #define GARY_FIX(ptr) (ptr) = gary_fix((ptr))
40 /* Internal functions */
41 void *gary_init(size_t elt_size, size_t num_elts, int zeroed);
42 void gary_free(void *array);
43 void *gary_set_size(void *array, size_t n);
44 void *gary_push_helper(void *array, size_t n, byte **cptr);
45 void *gary_fix(void *array);