]> mj.ucw.cz Git - libucw.git/blob - ucw/gary.h
Doc: Updated the list of contributors
[libucw.git] / ucw / gary.h
1 /*
2  *      UCW Library -- A simple growing array of an arbitrary type
3  *
4  *      (c) 2010--2014 Martin Mares <mj@ucw.cz>
5  */
6
7 #ifndef _UCW_GARY_H
8 #define _UCW_GARY_H
9
10 #include <ucw/alloc.h>
11
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
17 #endif
18
19 struct gary_hdr {
20   size_t num_elts;
21   size_t have_space;
22   size_t elt_size;
23   struct ucw_allocator *allocator;
24 };
25
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)
29
30 /**
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().
34  **/
35 #define GARY_INIT(ptr, n) (ptr) = gary_init(sizeof(*(ptr)), (n), &ucw_allocator_std)
36
37 /**
38  * Create a growing array like GARY_INIT() does, but all newly
39  * allocated elements will be automatically zeroed.
40  **/
41 #define GARY_INIT_ZERO(ptr, n) (ptr) = gary_init(sizeof(*(ptr)), (n), &ucw_allocator_zeroed)
42
43 /**
44  * Create a growing array like GARY_INIT() does, but based upon the given
45  * <<alloc:,generic allocator>>.
46  **/
47 #define GARY_INIT_ALLOC(ptr, n, a) (ptr) = gary_init(sizeof(*(ptr)), (n), (a))
48
49 /**
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.
53  **/
54 #define GARY_INIT_SPACE(ptr, n) do { GARY_INIT(ptr, n); (GARY_HDR(ptr))->num_elts = 0; } while (0)
55
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)
58
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)
61
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)
64
65 /** Return the current number elements of the given growing array. **/
66 #define GARY_SIZE(ptr) (GARY_HDR(ptr)->num_elts)
67
68 /**
69  * Resize the given growing array to @n elements.
70  * The @ptr can change, if the array has to be re-allocated.
71  **/
72 #define GARY_RESIZE(ptr, n) ((ptr) = gary_set_size((ptr), (n)))
73
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)
76
77 /**
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.
81  **/
82 #define GARY_PUSH_MULTI(ptr, n) ({                                      \
83   struct gary_hdr *_h = GARY_HDR(ptr);                                  \
84   typeof(*(ptr)) *_c = &(ptr)[_h->num_elts];                            \
85   size_t _n = n;                                                        \
86   _h->num_elts += _n;                                                   \
87   if (_h->num_elts > _h->have_space)                                    \
88     (ptr) = gary_push_helper((ptr), _n, (byte **) &_c);                 \
89   _c; })
90
91 /**
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.
94  **/
95 #define GARY_PUSH(ptr) GARY_PUSH_MULTI(ptr, 1)
96
97 /**
98  * Pop @n elements from the end of a growing array.
99  * The @ptr can change, if the array has to be re-allocated.
100  **/
101 #define GARY_POP_MULTI(ptr, n) GARY_HDR(ptr)->num_elts -= (n)
102
103 /**
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.
106  **/
107 #define GARY_POP(ptr) GARY_POP_MULTI(ptr, 1)
108
109 /**
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.
113  **/
114 #define GARY_FIX(ptr) (ptr) = gary_fix((ptr))
115
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);
121
122 static inline void gary_free(void *ptr)
123 {
124   if (ptr)
125     {
126       struct gary_hdr *h = GARY_HDR(ptr);
127       h->allocator->free(h->allocator, h);
128     }
129 }
130
131 /* A forever empty gary. Used internally. */
132
133 extern struct gary_hdr gary_empty_hdr;
134 #define GARY_FOREVER_EMPTY GARY_BODY(&gary_empty_hdr)
135
136 /* Type-agnostic interface. Currently it's recommended for internal use only. */
137
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);                  \
143   _c; })
144
145 #endif