Blame


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