Debian Codegraph @

lib/util/list.h

raw
1 /// @file
2 /// @brief type-generic dynamically expanding list
3 /// @ingroup cgraph_utils
4 ///
5 /// The code in this header is structured as a public API made up of macros that
6 /// do as little as possible before handing off to internal functions.
7 ///
8 /// If you are familiar with the concept of a dynamically expanding array like
9 /// C++’s `std::vector`, the only things that will likely throw you off are:
10 /// 1. The genericity of `LIST` is implemented through a `union` that overlaps
11 /// the `base` member of a `list_t_` (a generic list core) with a typed
12 /// pointer. This design involves the public macros dealing in the
13 /// `LIST(foo)` type while the private functions deal in `list_t_`s.
14 /// 2. The start of the list is not always index 0, in order to more
15 /// efficiently implement a queue. See the diagram and discussion in
16 /// `try_reserve` to better understand this.
17 ///
18 /// Some general terminology you may see in function/macro names:
19 /// base – the start of the underlying heap allocation backing a list
20 /// dtor – destructor
21 /// head – slot index of the start of a list
22 /// item – a list element
23 /// slot – an item-sized space in the list, offset recorded from base
24 ///
25 /// Some unorthodox idioms you may see used in this file:
26 /// • `(void)(foo == bar)` as a way to force the compiler to type-check that
27 /// `foo` and `bar` have compatible types. This is the best we can do for
28 /// pointer compatibility checks without `typeof`.
29 /// • `(void)(sizeof(foo) == sizeof(bar) ? (void)0 : (void)(…,abort())` as an
30 /// even weaker version of the above, for when we need to delay a check to
31 /// runtime instead of compile-time. This is a very unreliable check for
32 /// `foo` and `bar` being the same type, so should be avoided wherever
33 /// possible.
34
35 #pragma once
36
37 #include <assert.h>
38 #include <stdint.h>
39 #include <string.h>
40 #include <util/list-private.h>
41
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45
46 static_assert(
47 offsetof(list_t_, base) == 0,
48 "LIST(<type>).base and LIST(<type>).impl.base will not alias each other");
49
50 /// list data structure
51 ///
52 /// Typical usage:
53 ///
54 /// LIST(int) my_int_list = {0};
55 #define LIST(type) \
56 struct { \
57 union { \
58 type *base; \
59 list_t_ impl; \
60 }; /**< backing storage */ \
61 void (*dtor)(type); /**< optional destructor */ \
62 type scratch; /**< temporary space for storing off-list items */ \
63 }
64
65 /// sentinel value to indicate you want `free` to be used as a list destructor
66 ///
67 /// Sample usage:
68 ///
69 /// LIST(char *) my_strings = {.dtor = LIST_DTOR_FREE};
70 #define LIST_DTOR_FREE ((void *)1)
71
72 /// get the number of elements in a list
73 ///
74 /// You can think of this macro as having the C type:
75 ///
76 /// size_t LIST_SIZE(const LIST(<type>) *list);
77 ///
78 /// @param list List to inspect
79 /// @return Size of the list
80 #define LIST_SIZE(list) gv_list_size_((list)->impl)
81
82 /// does this list contain no elements?
83 ///
84 /// You can think of this macro as having the C type:
85 ///
86 /// bool LIST_IS_EMPTY(const LIST(<type>) *list);
87 ///
88 /// @param list List to inspect
89 /// @return True if the list is empty
90 #define LIST_IS_EMPTY(list) (LIST_SIZE(list) == 0)
91
92 /// try to append a new item to a list
93 ///
94 /// You can think of this macro as having the C type:
95 ///
96 /// bool LIST_TRY_APPEND(LIST(<type>) *list, <type> item);
97 ///
98 /// @param list List to operate on
99 /// @param item Item to append
100 /// @return True if the append succeeded
101 #define LIST_TRY_APPEND(list, item) \
102 gv_list_try_append_(&(list)->impl, \
103 ((list)->scratch = (item), &(list)->scratch), \
104 sizeof((list)->base[0]))
105
106 /// add an item to the end of a list
107 ///
108 /// You can think of this macro as having the C type:
109 ///
110 /// void LIST_APPEND(LIST(<type>) *list, <type> item);
111 ///
112 /// This macro succeeds or exits on out-of-memory; it never return failure.
113 ///
114 /// Note, in contrast to `LIST_TRY_APPEND`, `gv_list_append_slot_` and the write
115 /// to `(list)->base[…]` are in separate statements because the
116 /// `gv_list_append_slot_` call here _can_ alter `(list)->base`.
117 ///
118 /// @param list List to operate on
119 /// @param item Element to append
120 #define LIST_APPEND(list, item) \
121 do { \
122 (list)->scratch = (item); \
123 const size_t slot_ = \
124 gv_list_append_slot_(&(list)->impl, sizeof((list)->base[0])); \
125 (list)->base[slot_] = (list)->scratch; \
126 } while (0)
127
128 /// add an item to the beginning of a list
129 ///
130 /// You can think of this macro as having the C type:
131 ///
132 /// void LIST_PREPEND(LIST(<type>) *list, <type> item);
133 ///
134 /// This macro succeeds or exits on out-of-memory; it never return failure.
135 ///
136 /// @param list List to operate on
137 /// @param item Element to prepend
138 #define LIST_PREPEND(list, item) \
139 do { \
140 (list)->scratch = (item); \
141 const size_t slot_ = \
142 gv_list_prepend_slot_(&(list)->impl, sizeof((list)->base[0])); \
143 (list)->base[slot_] = (list)->scratch; \
144 } while (0)
145
146 /// retrieve an item from a list
147 ///
148 /// You can think of this macro as having the C type:
149 ///
150 /// <type> LIST_GET(const LIST(<type>) *list, size_t index);
151 ///
152 /// @param list List to operate on
153 /// @param index Item index to get
154 /// @return Item at the given index
155 #define LIST_GET(list, index) \
156 ((list)->base[gv_list_get_((list)->impl, (index))])
157
158 /// retrieve a pointer to an item from a list
159 ///
160 /// You can think of this macro as having one of the C types:
161 ///
162 /// <type> *LIST_AT(LIST(<type>) *list, size_t index);
163 /// const <type> *LIST_AT(const LIST(<type>) *list, size_t index);
164 ///
165 /// @param list List to operate on
166 /// @param index Item index to get
167 /// @return Pointer to item at the given index
168 #define LIST_AT(list, index) \
169 (&(list)->base[gv_list_get_((list)->impl, (index))])
170
171 /// retrieve a pointer to the first item in a list
172 ///
173 /// You can think of this macro as having one of the C types:
174 ///
175 /// <type> *LIST_FRONT(LIST(<type>) *list);
176 /// const <type> *LIST_FRONT(const LIST(<type>) *list);
177 ///
178 /// @param list List to operate on
179 /// @return Pointer to the first item in the list
180 #define LIST_FRONT(list) LIST_AT((list), 0)
181
182 /// retrieve a pointer to the last item in a list
183 ///
184 /// You can think of this macro as having one of the C types:
185 ///
186 /// <type> *LIST_BACK(LIST(<type>) *list);
187 /// const <type> *LIST_BACK(const LIST(<type>) *list);
188 ///
189 /// @param list List to operate on
190 /// @return Pointer to the last item in the list
191 #define LIST_BACK(list) LIST_AT((list), LIST_SIZE(list) - 1)
192
193 /// update the value of an item in a list
194 ///
195 /// You can think of this macro as having the C type:
196 ///
197 /// void LIST_SET(LIST(<type>) *list, size_t index, <type> item);
198 ///
199 /// @param list List to operate on
200 /// @param index Index of item to update
201 /// @param item New value to set
202 #define LIST_SET(list, index, item) \
203 do { \
204 (list)->scratch = (item); \
205 const size_t slot_ = gv_list_get_((list)->impl, (index)); \
206 LIST_DTOR_((list), slot_); \
207 (list)->base[slot_] = (list)->scratch; \
208 } while (0)
209
210 /// remove an item from a list
211 ///
212 /// You can think of this macro as having the C type:
213 ///
214 /// void LIST_REMOVE(LIST(<type>) *list, <type> item);
215 ///
216 /// @param list List to operate on
217 /// @param item Item to remove
218 #define LIST_REMOVE(list, item) \
219 do { \
220 /* get something we can take the address of */ \
221 (list)->scratch = (item); \
222 \
223 const size_t found_ = gv_list_find_((list)->impl, &(list)->scratch, \
224 sizeof((list)->base[0])); \
225 if (found_ == SIZE_MAX) { /* not found */ \
226 break; \
227 } \
228 \
229 LIST_DTOR_((list), found_); \
230 gv_list_remove_(&(list)->impl, found_, sizeof((list)->base[0])); \
231 } while (0)
232
233 /// remove all items from a list
234 ///
235 /// You can think of this macro as having the C type:
236 ///
237 /// void LIST_CLEAR(LIST(<type>) *list);
238 ///
239 /// @param list List to clear
240 #define LIST_CLEAR(list) \
241 do { \
242 for (size_t i_ = 0; i_ < LIST_SIZE(list); ++i_) { \
243 const size_t slot_ = gv_list_get_((list)->impl, i_); \
244 LIST_DTOR_((list), slot_); \
245 } \
246 gv_list_clear_(&(list)->impl, sizeof((list)->base[0])); \
247 } while (0)
248
249 /// reserve space for new items in a list
250 ///
251 /// You can think of this macro as having the C type:
252 ///
253 /// void LIST_RESERVE(LIST(<type>) *list, size_t capacity);
254 ///
255 /// @param list List to operate on
256 /// @param capacity Total number of item slots to make available
257 #define LIST_RESERVE(list, capacity) \
258 gv_list_reserve_(&(list)->impl, capacity, sizeof((list)->base[0]))
259
260 /// does a list contain a given item?
261 ///
262 /// You can think of this macro as having the C type:
263 ///
264 /// bool LIST_CONTAINS(const LIST(<type>) *list, <type> needle);
265 ///
266 /// The `needle` parameter must be an expression that can have its address
267 /// taken. E.g. `LIST_CONTAINS(my_ints, 2)` is not valid. This can be worked
268 /// around with C99 compound literals, `LIST_CONTAINS(my_ints, (int){2})`.
269 ///
270 /// @param list List to search
271 /// @param needle Item to search for
272 /// @return True if the item was found
273 #define LIST_CONTAINS(list, needle) \
274 gv_list_contains_((list)->impl, \
275 ((void)((list)->base == &(needle)), &(needle)), \
276 sizeof((list)->base[0]))
277
278 /// copy a list
279 ///
280 /// You can think of this macro as having the C type:
281 ///
282 /// void LIST_COPY(LIST(<type>) *dst, const LIST(<type>) *src);
283 ///
284 /// @param [out] dst Copy of the source list on completion
285 /// @param src List to copy
286 #define LIST_COPY(dst, src) \
287 do { \
288 memset((dst), 0, sizeof(*(dst))); \
289 (void)((dst)->base == (src)->base); \
290 (dst)->impl = gv_list_copy_((src)->impl, sizeof((src)->base[0])); \
291 (dst)->dtor = (src)->dtor; \
292 } while (0)
293
294 /// does the list not wrap past its end?
295 ///
296 /// This checks whether the list is discontiguous in how its elements
297 /// appear in memory:
298 ///
299 /// ┌───┬───┬───┬───┬───┬───┬───┬───┐
300 /// a contiguous list: │ │ │ w │ x │ y │ z │ │ │
301 /// └───┴───┴───┴───┴───┴───┴───┴───┘
302 /// 0 1 2 3
303 ///
304 /// ┌───┬───┬───┬───┬───┬───┬───┬───┐
305 /// a discontiguous list: │ y │ z │ │ │ │ │ w │ x │
306 /// └───┴───┴───┴───┴───┴───┴───┴───┘
307 /// 2 3 0 1
308 ///
309 /// You can think of this macro as having the C type:
310 ///
311 /// bool LIST_IS_CONTIGUOUS(const LIST(<type>>) *list);
312 ///
313 /// @param list List to inspect
314 /// @return True if the list is contiguous
315 #define LIST_IS_CONTIGUOUS(list) gv_list_is_contiguous_((list)->impl);
316
317 /// shuffle the populated contents to reset `head` to 0
318 ///
319 /// You can think of this macro as having the C type:
320 ///
321 /// void LIST_SYNC(LIST(<type>) *list);
322 ///
323 /// See the `LIST_IS_CONTIGUOUS` leading comment for a better understanding of
324 /// what it means for `head` to be non-zero.
325 ///
326 /// @param list List to operate on
327 #define LIST_SYNC(list) gv_list_sync_(&(list)->impl, sizeof((list)->base[0]))
328
329 /// sort a list
330 ///
331 /// You can think of this macro as having the C type:
332 ///
333 /// void LIST_SORT(LIST(<type>) *list,
334 /// int (*comparator)(const void *a, const void *b));
335 ///
336 /// @param list List to operate on
337 /// @param comparator How to compare two list items
338 #define LIST_SORT(list, cmp) \
339 gv_list_sort_(&(list)->impl, (cmp), sizeof((list)->base[0]))
340
341 /// reverse the item order of a list
342 ///
343 /// You can think of this macro as having the C type:
344 ///
345 /// void LIST_REVERSE(LIST(<type>) *list);
346 ///
347 /// @param list List to operate on
348 #define LIST_REVERSE(list) \
349 gv_list_reverse_(&(list)->impl, sizeof((list)->base[0]))
350
351 /// decrease the allocated capacity of a list to minimum
352 ///
353 /// You can think of this macro as having the C type:
354 ///
355 /// void LIST_SHRINK_TO_FIT(LIST(<type>) *list);
356 ///
357 /// @param list List to operate on
358 #define LIST_SHRINK_TO_FIT(list) \
359 gv_list_shrink_to_fit_(&(list)->impl, sizeof((list)->base[0]))
360
361 /// free resources associated with a list
362 ///
363 /// You can think of this macro as having the C type:
364 ///
365 /// void LIST_FREE(LIST(<type>) *list);
366 ///
367 /// After a call to this function, the list is empty and may be reused.
368 ///
369 /// @param list List to free
370 #define LIST_FREE(list) \
371 do { \
372 LIST_CLEAR(list); \
373 gv_list_free_(&(list)->impl); \
374 } while (0)
375
376 /// alias for append
377 ///
378 /// You can think of this macro as having the C type:
379 ///
380 /// void LIST_PUSH_BACK(LIST(<type>) *list, <type> item);
381 ///
382 /// @param list List to operate on
383 /// @param item Item to append
384 #define LIST_PUSH_BACK(list, item) LIST_APPEND((list), (item))
385
386 /// remove and return the first item of a list
387 ///
388 /// You can think of this macro as having the C type:
389 ///
390 /// <type> LIST_POP_FRONT(LIST(<type>) *list);
391 ///
392 /// @param list List to operate on
393 /// @return Popped item
394 #define LIST_POP_FRONT(list) \
395 (gv_list_pop_front_(&(list)->impl, &(list)->scratch, \
396 sizeof((list)->base[0])), \
397 (list)->scratch)
398
399 /// remove and return the last item of a list
400 ///
401 /// You can think of this macro as having the C type:
402 ///
403 /// <type> LIST_POP_BACK(LIST(<type>) *list);
404 ///
405 /// @param list List to operate on
406 /// @return Popped item
407 #define LIST_POP_BACK(list) \
408 (gv_list_pop_back_(&(list)->impl, &(list)->scratch, \
409 sizeof((list)->base[0])), \
410 (list)->scratch)
411
412 /// remove the last item of a list
413 ///
414 /// You can think of this macro as having the C type:
415 ///
416 /// void LIST_DROP_BACK(LIST(<type>) *list);
417 ///
418 /// This can be used to pop the last element when the caller does not need the
419 /// popped item.
420 ///
421 /// @param list List to operate on
422 #define LIST_DROP_BACK(list) \
423 do { \
424 const size_t slot_ = gv_list_get_((list)->impl, LIST_SIZE(list) - 1); \
425 LIST_DTOR_((list), slot_); \
426 gv_list_pop_back_(&(list)->impl, &(list)->scratch, \
427 sizeof((list)->base[0])); \
428 } while (0)
429
430 /// transform a managed list into a bare array
431 ///
432 /// You can think of this macro as having the C type:
433 ///
434 /// void LIST_DETACH(LIST(<type>) *list, <type> **data, size_t *size);
435 ///
436 /// This can be useful when needing to pass data to a callee who does not
437 /// use this API. The managed list is emptied and left in a state where it
438 /// can be reused for other purposes.
439 ///
440 /// @param list List to operate on
441 /// @param [out] datap The list data on completion
442 /// @param [out] sizep The list size on completion
443 #define LIST_DETACH(list, datap, sizep) \
444 gv_list_detach_(&(list)->impl, ((void)(&(list)->base == (datap)), (datap)), \
445 (sizep), sizeof((list)->base[0]))
446
447 #ifdef __cplusplus
448 }
449 #endif