2 * UCW Library -- A simple growing array of an arbitrary type
4 * (c) 2010--2013 Martin Mares <mj@ucw.cz>
10 #ifdef CONFIG_UCW_CLEAN_ABI
11 #define gary_fix ucw_gary_fix
12 #define gary_free ucw_gary_free
13 #define gary_init ucw_gary_init
14 #define gary_push_helper ucw_gary_push_helper
15 #define gary_set_size ucw_gary_set_size
25 #define GARY_HDR_SIZE ALIGN_TO(sizeof(struct gary_hdr), CPU_STRUCT_ALIGN)
26 #define GARY_HDR(ptr) ((struct gary_hdr *)((byte*)(ptr) - GARY_HDR_SIZE))
27 #define GARY_BODY(ptr) ((byte *)(ptr) + GARY_HDR_SIZE)
29 #define GARY_INIT(ptr, n) (ptr) = gary_init(sizeof(*(ptr)), (n), 0)
30 #define GARY_INIT_ZERO(ptr, n) (ptr) = gary_init(sizeof(*(ptr)), (n), 1)
31 #define GARY_INIT_SPACE(ptr, n) do { GARY_INIT(ptr, n); (GARY_HDR(ptr))->num_elts = 0; } while (0)
32 #define GARY_INIT_SPACE_ZERO(ptr, n) do { GARY_INIT_ZERO(ptr, n); (GARY_HDR(ptr))->num_elts = 0; } while (0)
33 #define GARY_FREE(ptr) do { if (ptr) xfree(GARY_HDR(ptr)); } while (0)
34 #define GARY_SIZE(ptr) (GARY_HDR(ptr)->num_elts)
35 #define GARY_RESIZE(ptr, n) ((ptr) = gary_set_size((ptr), (n)))
36 #define GARY_INIT_OR_RESIZE(ptr, n) (ptr) = (ptr) ? gary_set_size((ptr), (n)) : gary_init(sizeof(*(ptr)), (n), 0)
38 #define GARY_PUSH(ptr, n) ({ \
39 struct gary_hdr *_h = GARY_HDR(ptr); \
40 typeof(*(ptr)) *_c = &(ptr)[_h->num_elts]; \
43 if (_h->num_elts > _h->have_space) \
44 (ptr) = gary_push_helper((ptr), _n, (byte **) &_c); \
47 #define GARY_POP(ptr, n) GARY_HDR(ptr)->num_elts -= (n)
48 #define GARY_FIX(ptr) (ptr) = gary_fix((ptr))
50 /* Internal functions */
51 void *gary_init(size_t elt_size, size_t num_elts, int zeroed);
52 void gary_free(void *array);
53 void *gary_set_size(void *array, size_t n);
54 void *gary_push_helper(void *array, size_t n, byte **cptr);
55 void *gary_fix(void *array);