Blob


1 /* Auto-reallocating array for arbitrary member types. */
2 /*
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #ifdef __linux__
19 /* stupid shims to compile and test on linux */
20 #include <strings.h>
21 static inline void *reallocarray(void *buf, size_t n, size_t member_size)
22 {
23 return realloc(buf, n * member_size);
24 }
25 static inline void *recallocarray(void *buf, size_t oldn, size_t n,
26 size_t member_size)
27 {
28 buf = reallocarray(buf, n, member_size);
29 bzero(((char*)buf) + (oldn * member_size), (n - oldn) * member_size);
30 return buf;
31 }
32 #endif
35 /* Usage:
36 *
37 * ARRAYLIST(any_type_t) list;
38 * // OR
39 * typedef ARRAYLIST(any_type_t) any_type_list_t;
40 * any_type_list_t list;
41 *
42 * // pass the number of (at first unused) members to add on each realloc:
43 * ARRAYLIST_INIT(list, 128);
44 * any_type_t *x;
45 * while (bar) {
46 * // This enlarges the allocated array as needed;
47 * // list.head may change due to realloc:
48 * ARRAYLIST_ADD(x, list);
49 * if (!x)
50 * abort();
51 * *x = random_foo_value;
52 * }
53 * for (i = 0; i < list.len; i++)
54 * printf("%s", foo_to_str(list.head[i]));
55 * ARRAYLIST_FREE(list);
56 */
57 #define ARRAYLIST(MEMBER_TYPE) \
58 struct { \
59 MEMBER_TYPE *head; \
60 unsigned int len; \
61 unsigned int allocated; \
62 unsigned int alloc_blocksize; \
63 }
65 #define ARRAYLIST_INIT(ARRAY_LIST, ALLOC_BLOCKSIZE) do { \
66 (ARRAY_LIST).head = NULL; \
67 (ARRAY_LIST).len = 0; \
68 (ARRAY_LIST).allocated = 0; \
69 (ARRAY_LIST).alloc_blocksize = ALLOC_BLOCKSIZE; \
70 } while(0)
72 #define ARRAYLIST_ADD(NEW_ITEM_P, ARRAY_LIST) do { \
73 if ((ARRAY_LIST).len && !(ARRAY_LIST).allocated) { \
74 NEW_ITEM_P = NULL; \
75 break; \
76 } \
77 if ((ARRAY_LIST).head == NULL \
78 || (ARRAY_LIST).allocated < (ARRAY_LIST).len + 1) { \
79 (ARRAY_LIST).allocated += \
80 (ARRAY_LIST).alloc_blocksize ? : 8; \
81 (ARRAY_LIST).head = recallocarray((ARRAY_LIST).head, \
82 (ARRAY_LIST).len, \
83 (ARRAY_LIST).allocated, \
84 sizeof(*(ARRAY_LIST).head)); \
85 }; \
86 if (!(ARRAY_LIST).head \
87 || (ARRAY_LIST).allocated < (ARRAY_LIST).len + 1) { \
88 NEW_ITEM_P = NULL; \
89 break; \
90 } \
91 (NEW_ITEM_P) = &(ARRAY_LIST).head[(ARRAY_LIST).len]; \
92 (ARRAY_LIST).len++; \
93 } while (0)
95 #define ARRAYLIST_CLEAR(ARRAY_LIST) \
96 (ARRAY_LIST).len = 0
98 #define ARRAYLIST_FREE(ARRAY_LIST) \
99 do { \
100 if ((ARRAY_LIST).head && (ARRAY_LIST).allocated) \
101 free((ARRAY_LIST).head); \
102 ARRAYLIST_INIT(ARRAY_LIST, (ARRAY_LIST).alloc_blocksize); \
103 } while(0)