Blame


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