darray: cleanup
[platform/upstream/libxkbcommon.git] / src / darray.h
1 /*
2  * Copyright (C) 2011 Joseph Adams <joeyadams3.14159@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22
23 #ifndef CCAN_DARRAY_H
24 #define CCAN_DARRAY_H
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #define darray(type) struct { type *item; size_t size; size_t alloc; }
30
31 #define darray_new() { 0, 0, 0 }
32
33 #define darray_init(arr) do { \
34     (arr).item = 0; (arr).size = 0; (arr).alloc = 0; \
35 } while (0)
36
37 #define darray_free(arr) do { \
38     free((arr).item); \
39     darray_init(arr); \
40 } while (0)
41
42 /*
43  * Typedefs for darrays of common types.  These are useful
44  * when you want to pass a pointer to an darray(T) around.
45  *
46  * The following will produce an incompatible pointer warning:
47  *
48  *     void foo(darray(int) *arr);
49  *     darray(int) arr = darray_new();
50  *     foo(&arr);
51  *
52  * The workaround:
53  *
54  *     void foo(darray_int *arr);
55  *     darray_int arr = darray_new();
56  *     foo(&arr);
57  */
58
59 typedef darray (char)           darray_char;
60 typedef darray (signed char)    darray_schar;
61 typedef darray (unsigned char)  darray_uchar;
62
63 typedef darray (short)          darray_short;
64 typedef darray (int)            darray_int;
65 typedef darray (long)           darray_long;
66
67 typedef darray (unsigned short) darray_ushort;
68 typedef darray (unsigned int)   darray_uint;
69 typedef darray (unsigned long)  darray_ulong;
70
71 /*** Access ***/
72
73 #define darray_item(arr, i)     ((arr).item[i])
74 #define darray_size(arr)        ((arr).size)
75 #define darray_empty(arr)       ((arr).size == 0)
76 #define darray_mem(arr, offset) ((arr).item + (offset))
77
78 /*** Insertion (single item) ***/
79
80 #define darray_append(arr, ...)  do { \
81     darray_resize(arr, (arr).size + 1); \
82     (arr).item[(arr).size - 1] = (__VA_ARGS__); \
83 } while (0)
84
85 /*** Insertion (multiple items) ***/
86
87 #define darray_append_items(arr, items, count) do { \
88     size_t __count = (count), __oldSize = (arr).size; \
89     darray_resize(arr, __oldSize + __count); \
90     memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
91 } while (0)
92
93 #define darray_from_items(arr, items, count) do { \
94     size_t __count = (count); \
95     darray_resize(arr, __count); \
96     memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
97 } while (0)
98
99 #define darray_copy(arr_to, arr_from) \
100     darray_from_items((arr_to), (arr_from).item, (arr_from).size)
101
102 /*** String buffer ***/
103
104 #define darray_append_string(arr, str) do { \
105     const char *__str = (str); \
106     darray_append_items(arr, __str, strlen(__str) + 1); \
107     (arr).size--; \
108 } while (0)
109
110 #define darray_append_lit(arr, stringLiteral) do { \
111     darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); \
112     (arr).size--; \
113 } while (0)
114
115 #define darray_appends_nullterminate(arr, items, count) do { \
116     size_t __count = (count), __oldSize = (arr).size; \
117     darray_resize(arr, __oldSize + __count + 1); \
118     memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
119     (arr).item[--(arr).size] = 0; \
120 } while (0)
121
122 #define darray_prepends_nullterminate(arr, items, count) do { \
123     size_t __count = (count), __oldSize = (arr).size; \
124     darray_resize(arr, __count + __oldSize + 1); \
125     memmove((arr).item + __count, (arr).item, \
126             __oldSize * sizeof(*(arr).item)); \
127     memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
128     (arr).item[--(arr).size] = 0; \
129 } while (0)
130
131 /*** Size management ***/
132
133 #define darray_resize(arr, newSize) \
134     darray_growalloc(arr, (arr).size = (newSize))
135
136 #define darray_resize0(arr, newSize) do { \
137     size_t __oldSize = (arr).size, __newSize = (newSize); \
138     (arr).size = __newSize; \
139     if (__newSize > __oldSize) { \
140         darray_growalloc(arr, __newSize); \
141         memset(&(arr).item[__oldSize], 0, \
142                (__newSize - __oldSize) * sizeof(*(arr).item)); \
143     } \
144 } while (0)
145
146 #define darray_realloc(arr, newAlloc) do { \
147     (arr).item = realloc((arr).item, \
148                          ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \
149 } while (0)
150
151 #define darray_growalloc(arr, need)   do { \
152     size_t __need = (need); \
153     if (__need > (arr).alloc) \
154     darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \
155 } while (0)
156
157 static inline size_t
158 darray_next_alloc(size_t alloc, size_t need)
159 {
160     if (alloc == 0)
161         alloc = 4;
162     while (alloc < need)
163         alloc *= 2;
164     return alloc;
165 }
166
167 /*** Traversal ***/
168
169 #define darray_foreach(i, arr) \
170     for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
171
172 #define darray_foreach_from(i, arr, from) \
173     for ((i) = &(arr).item[from]; (i) < &(arr).item[(arr).size]; (i)++)
174
175 /* Iterate on index and value at the same time, like Python's enumerate. */
176 #define darray_enumerate(idx, val, arr) \
177     for ((idx) = 0, (val) = &(arr).item[0]; \
178          (idx) < (arr).size; \
179          (idx)++, (val)++)
180
181 #define darray_enumerate_from(idx, val, arr, from) \
182     for ((idx) = (from), (val) = &(arr).item[0]; \
183          (idx) < (arr).size; \
184          (idx)++, (val)++)
185
186 #define darray_foreach_reverse(i, arr) \
187     for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; )
188
189 #endif /* CCAN_DARRAY_H */