2 * Copyright (C) 2011 Joseph Adams <joeyadams3.14159@gmail.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
26 /* Originally taken from: http://ccodearchive.net/info/darray.html
27 * But modified for libxkbcommon. */
34 #define darray(type) struct { type *item; unsigned size; unsigned alloc; }
36 #define darray_new() { 0, 0, 0 }
38 #define darray_init(arr) do { \
39 (arr).item = 0; (arr).size = 0; (arr).alloc = 0; \
42 #define darray_free(arr) do { \
48 * Typedefs for darrays of common types. These are useful
49 * when you want to pass a pointer to an darray(T) around.
51 * The following will produce an incompatible pointer warning:
53 * void foo(darray(int) *arr);
54 * darray(int) arr = darray_new();
59 * void foo(darray_int *arr);
60 * darray_int arr = darray_new();
64 typedef darray (char) darray_char;
65 typedef darray (signed char) darray_schar;
66 typedef darray (unsigned char) darray_uchar;
68 typedef darray (short) darray_short;
69 typedef darray (int) darray_int;
70 typedef darray (long) darray_long;
72 typedef darray (unsigned short) darray_ushort;
73 typedef darray (unsigned int) darray_uint;
74 typedef darray (unsigned long) darray_ulong;
78 #define darray_item(arr, i) ((arr).item[i])
79 #define darray_size(arr) ((arr).size)
80 #define darray_empty(arr) ((arr).size == 0)
81 #define darray_mem(arr, offset) ((arr).item + (offset))
83 /*** Insertion (single item) ***/
85 #define darray_append(arr, ...) do { \
86 darray_resize(arr, (arr).size + 1); \
87 (arr).item[(arr).size - 1] = (__VA_ARGS__); \
90 /*** Insertion (multiple items) ***/
92 #define darray_append_items(arr, items, count) do { \
93 unsigned __count = (count), __oldSize = (arr).size; \
94 darray_resize(arr, __oldSize + __count); \
95 memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
98 #define darray_from_items(arr, items, count) do { \
99 unsigned __count = (count); \
100 darray_resize(arr, __count); \
101 memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
104 #define darray_copy(arr_to, arr_from) \
105 darray_from_items((arr_to), (arr_from).item, (arr_from).size)
107 /*** String buffer ***/
109 #define darray_append_string(arr, str) do { \
110 const char *__str = (str); \
111 darray_append_items(arr, __str, strlen(__str) + 1); \
115 #define darray_append_lit(arr, stringLiteral) do { \
116 darray_append_items(arr, stringLiteral, sizeof(stringLiteral)); \
120 #define darray_appends_nullterminate(arr, items, count) do { \
121 unsigned __count = (count), __oldSize = (arr).size; \
122 darray_resize(arr, __oldSize + __count + 1); \
123 memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
124 (arr).item[--(arr).size] = 0; \
127 #define darray_prepends_nullterminate(arr, items, count) do { \
128 unsigned __count = (count), __oldSize = (arr).size; \
129 darray_resize(arr, __count + __oldSize + 1); \
130 memmove((arr).item + __count, (arr).item, \
131 __oldSize * sizeof(*(arr).item)); \
132 memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
133 (arr).item[--(arr).size] = 0; \
136 /*** Size management ***/
138 #define darray_resize(arr, newSize) \
139 darray_growalloc(arr, (arr).size = (newSize))
141 #define darray_resize0(arr, newSize) do { \
142 unsigned __oldSize = (arr).size, __newSize = (newSize); \
143 (arr).size = __newSize; \
144 if (__newSize > __oldSize) { \
145 darray_growalloc(arr, __newSize); \
146 memset(&(arr).item[__oldSize], 0, \
147 (__newSize - __oldSize) * sizeof(*(arr).item)); \
151 #define darray_realloc(arr, newAlloc) do { \
152 (arr).item = realloc((arr).item, \
153 ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \
156 #define darray_growalloc(arr, need) do { \
157 unsigned __need = (need); \
158 if (__need > (arr).alloc) \
159 darray_realloc(arr, darray_next_alloc((arr).alloc, __need, \
160 sizeof(*(arr).item))); \
163 static inline unsigned
164 darray_next_alloc(unsigned alloc, unsigned need, unsigned itemSize)
166 assert(need < UINT_MAX / itemSize / 2); /* Overflow. */
176 #define darray_foreach(i, arr) \
177 for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
179 #define darray_foreach_from(i, arr, from) \
180 for ((i) = &(arr).item[from]; (i) < &(arr).item[(arr).size]; (i)++)
182 /* Iterate on index and value at the same time, like Python's enumerate. */
183 #define darray_enumerate(idx, val, arr) \
184 for ((idx) = 0, (val) = &(arr).item[0]; \
185 (idx) < (arr).size; \
188 #define darray_enumerate_from(idx, val, arr, from) \
189 for ((idx) = (from), (val) = &(arr).item[0]; \
190 (idx) < (arr).size; \
193 #endif /* CCAN_DARRAY_H */