]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/darray.h
Update bcachefs sources to 0e705f5944 fixup! bcachefs: Refactor bch2_btree_node_mem_a...
[bcachefs-tools-debian] / libbcachefs / 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 #include "config.h"
29
30 /*
31  * SYNOPSIS
32  *
33  * Life cycle of a darray (dynamically-allocated array):
34  *
35  *     darray(int) a = darray_new();
36  *     darray_free(a);
37  *
38  *     struct {darray(int) a;} foo;
39  *     darray_init(foo.a);
40  *     darray_free(foo.a);
41  *
42  * Typedefs for darrays of common types:
43  *
44  *     darray_char, darray_schar, darray_uchar
45  *     darray_short, darray_int, darray_long
46  *     darray_ushort, darray_uint, darray_ulong
47  *
48  * Access:
49  *
50  *     T      darray_item(darray(T) arr, size_t index);
51  *     size_t darray_size(darray(T) arr);
52  *     size_t darray_alloc(darray(T) arr);
53  *     bool   darray_empty(darray(T) arr);
54  *
55  * Insertion (single item):
56  *
57  *     void   darray_append(darray(T) arr, T item);
58  *     void   darray_prepend(darray(T) arr, T item);
59  *     void   darray_push(darray(T) arr, T item); // same as darray_append
60  *
61  * Insertion (multiple items):
62  *
63  *     void   darray_append_items(darray(T) arr, T *items, size_t count);
64  *     void   darray_prepend_items(darray(T) arr, T *items, size_t count);
65  *
66  *     void   darray_appends(darray(T) arr, [T item, [...]]);
67  *     void   darray_prepends(darray(T) arr, [T item, [...]]);
68  *
69  *     // Same functionality as above, but does not require typeof.
70  *     void   darray_appends_t(darray(T) arr, #T, [T item, [...]]);
71  *     void   darray_prepends_t(darray(T) arr, #T, [T item, [...]]);
72  *
73  * Removal:
74  *
75  *     T      darray_pop(darray(T) arr | darray_size(arr) != 0);
76  *     T*     darray_pop_check(darray(T*) arr);
77  *     void   darray_remove(darray(T) arr, size_t index);
78  *
79  * Replacement:
80  *
81  *     void   darray_from_items(darray(T) arr, T *items, size_t count);
82  *     void   darray_from_c(darray(T) arr, T c_array[N]);
83  *
84  * String buffer:
85  *
86  *     void   darray_append_string(darray(char) arr, const char *str);
87  *     void   darray_append_lit(darray(char) arr, char stringLiteral[N+1]);
88  *
89  *     void   darray_prepend_string(darray(char) arr, const char *str);
90  *     void   darray_prepend_lit(darray(char) arr, char stringLiteral[N+1]);
91  *
92  *     void   darray_from_string(darray(T) arr, const char *str);
93  *     void   darray_from_lit(darray(char) arr, char stringLiteral[N+1]);
94  *
95  * Size management:
96  *
97  *     void   darray_resize(darray(T) arr, size_t newSize);
98  *     void   darray_resize0(darray(T) arr, size_t newSize);
99  *
100  *     void   darray_realloc(darray(T) arr, size_t newAlloc);
101  *     void   darray_growalloc(darray(T) arr, size_t newAlloc);
102  *
103  *     void   darray_make_room(darray(T) arr, size_t room);
104  *
105  * Traversal:
106  *
107  *     darray_foreach(T *&i, darray(T) arr) {...}
108  *     darray_foreach_reverse(T *&i, darray(T) arr) {...}
109  *
110  * Except for darray_foreach, darray_foreach_reverse, and darray_remove,
111  * all macros evaluate their non-darray arguments only once.
112  */
113
114 /*** Life cycle ***/
115
116 #define darray(type) struct {type *item; size_t size; size_t alloc;}
117
118 #define darray_new() {0,0,0}
119 #define darray_init(arr) do {(arr).item=0; (arr).size=0; (arr).alloc=0;} while(0)
120 #define darray_free(arr) do {kfree((arr).item);} while(0)
121
122
123
124 /*** Access ***/
125
126 #define darray_item(arr, i) ((arr).item[i])
127 #define darray_size(arr)    ((arr).size)
128 #define darray_alloc(arr)   ((arr).alloc)
129 #define darray_empty(arr)   ((arr).size == 0)
130
131
132 /*** Insertion (single item) ***/
133
134 #define darray_append(arr, ...) do { \
135                 darray_resize(arr, (arr).size+1); \
136                 (arr).item[(arr).size-1] = (__VA_ARGS__); \
137         } while(0)
138 #define darray_prepend(arr, ...) do { \
139                 darray_resize(arr, (arr).size+1); \
140                 memmove((arr).item+1, (arr).item, ((arr).size-1)*sizeof(*(arr).item)); \
141                 (arr).item[0] = (__VA_ARGS__); \
142         } while(0)
143 #define darray_push(arr, ...) darray_append(arr, __VA_ARGS__)
144
145
146 /*** Insertion (multiple items) ***/
147
148 #define darray_append_items(arr, items, count) do { \
149                 size_t __count = (count), __oldSize = (arr).size; \
150                 darray_resize(arr, __oldSize + __count); \
151                 memcpy((arr).item + __oldSize, items, __count * sizeof(*(arr).item)); \
152         } while(0)
153
154 #define darray_prepend_items(arr, items, count) do { \
155                 size_t __count = (count), __oldSize = (arr).size; \
156                 darray_resize(arr, __count + __oldSize); \
157                 memmove((arr).item + __count, (arr).item, __oldSize * sizeof(*(arr).item)); \
158                 memcpy((arr).item, items, __count * sizeof(*(arr).item)); \
159         } while(0)
160
161 #if HAVE_TYPEOF
162 #define darray_appends(arr, ...) darray_appends_t(arr, typeof((*(arr).item)), __VA_ARGS__)
163 #define darray_prepends(arr, ...) darray_prepends_t(arr, typeof((*(arr).item)), __VA_ARGS__)
164 #endif
165
166 #define darray_appends_t(arr, type, ...) do { \
167                 type __src[] = {__VA_ARGS__}; \
168                 darray_append_items(arr, __src, sizeof(__src)/sizeof(*__src)); \
169         } while(0)
170 #define darray_prepends_t(arr, type, ...) do { \
171                 type __src[] = {__VA_ARGS__}; \
172                 darray_prepend_items(arr, __src, sizeof(__src)/sizeof(*__src)); \
173         } while(0)
174
175
176 /*** Removal ***/
177
178 /* Warning: Do not call darray_pop on an empty darray. */
179 #define darray_pop(arr) ((arr).item[--(arr).size])
180 #define darray_pop_check(arr) ((arr).size ? darray_pop(arr) : NULL)
181 /* Warning, slow: Requires copying all elements after removed item. */
182 #define darray_remove(arr, index) do { \
183         if (index < arr.size-1)    \
184                 memmove(&(arr).item[index], &(arr).item[index+1], ((arr).size-1-i)*sizeof(*(arr).item)); \
185         (arr).size--;  \
186         } while(0)
187
188
189 /*** Replacement ***/
190
191 #define darray_from_items(arr, items, count) do {size_t __count = (count); darray_resize(arr, __count); memcpy((arr).item, items, __count*sizeof(*(arr).item));} while(0)
192 #define darray_from_c(arr, c_array) darray_from_items(arr, c_array, sizeof(c_array)/sizeof(*(c_array)))
193
194
195 /*** Size management ***/
196
197 #define darray_resize(arr, newSize) darray_growalloc(arr, (arr).size = (newSize))
198 #define darray_resize0(arr, newSize) do { \
199                 size_t __oldSize = (arr).size, __newSize = (newSize); \
200                 (arr).size = __newSize; \
201                 if (__newSize > __oldSize) { \
202                         darray_growalloc(arr, __newSize); \
203                         memset(&(arr).item[__oldSize], 0, (__newSize - __oldSize) * sizeof(*(arr).item)); \
204                 } \
205         } while(0)
206
207 #define darray_realloc(arr, newAlloc) do { \
208                 (arr).item = realloc((arr).item, ((arr).alloc = (newAlloc)) * sizeof(*(arr).item)); \
209         } while(0)
210 #define darray_growalloc(arr, need) do { \
211                 size_t __need = (need); \
212                 if (__need > (arr).alloc) \
213                         darray_realloc(arr, darray_next_alloc((arr).alloc, __need)); \
214         } while(0)
215
216 #if HAVE_STATEMENT_EXPR==1
217 #define darray_make_room(arr, room) ({size_t newAlloc = (arr).size+(room); if ((arr).alloc<newAlloc) darray_realloc(arr, newAlloc); (arr).item+(arr).size; })
218 #endif
219
220 static inline size_t darray_next_alloc(size_t alloc, size_t need)
221 {
222         return roundup_pow_of_two(alloc + need);
223 }
224
225
226 /*** Traversal ***/
227
228 /*
229  * darray_foreach(T *&i, darray(T) arr) {...}
230  *
231  * Traverse a darray.  `i` must be declared in advance as a pointer to an item.
232  */
233 #define darray_foreach(i, arr) \
234         for ((i) = &(arr).item[0]; (i) < &(arr).item[(arr).size]; (i)++)
235
236 /*
237  * darray_foreach_reverse(T *&i, darray(T) arr) {...}
238  *
239  * Like darray_foreach, but traverse in reverse order.
240  */
241 #define darray_foreach_reverse(i, arr) \
242         for ((i) = &(arr).item[(arr).size]; (i)-- > &(arr).item[0]; )
243
244
245 #endif /* CCAN_DARRAY_H */
246
247 /*
248
249 darray_growalloc(arr, newAlloc) sees if the darray can currently hold newAlloc items;
250         if not, it increases the alloc to satisfy this requirement, allocating slack
251         space to avoid having to reallocate for every size increment.
252
253 darray_from_string(arr, str) copies a string to an darray_char.
254
255 darray_push(arr, item) pushes an item to the end of the darray.
256 darray_pop(arr) pops it back out.  Be sure there is at least one item in the darray before calling.
257 darray_pop_check(arr) does the same as darray_pop, but returns NULL if there are no more items left in the darray.
258
259 darray_make_room(arr, room) ensures there's 'room' elements of space after the end of the darray, and it returns a pointer to this space.
260 Currently requires HAVE_STATEMENT_EXPR, but I plan to remove this dependency by creating an inline function.
261
262 The following require HAVE_TYPEOF==1 :
263
264 darray_appends(arr, item0, item1...) appends a collection of comma-delimited items to the darray.
265 darray_prepends(arr, item0, item1...) prepends a collection of comma-delimited items to the darray.\
266
267
268 Examples:
269
270         darray(int)  arr;
271         int        *i;
272         
273         darray_appends(arr, 0,1,2,3,4);
274         darray_appends(arr, -5,-4,-3,-2,-1);
275         darray_foreach(i, arr)
276                 printf("%d ", *i);
277         printf("\n");
278         
279         darray_free(arr);
280         
281
282         typedef struct {int n,d;} Fraction;
283         darray(Fraction) fractions;
284         Fraction        *i;
285         
286         darray_appends(fractions, {3,4}, {3,5}, {2,1});
287         darray_foreach(i, fractions)
288                 printf("%d/%d\n", i->n, i->d);
289         
290         darray_free(fractions);
291 */