1 /* Auto-reallocating array for arbitrary member types. */
3 * Copyright (c) 2020 Neels Hofmeyr <neels@hofmeyr.de>
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.
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.
20 * ARRAYLIST(any_type_t) list;
22 * typedef ARRAYLIST(any_type_t) any_type_list_t;
23 * any_type_list_t list;
25 * // pass the number of (at first unused) members to add on each realloc:
26 * ARRAYLIST_INIT(list, 128);
29 * // This enlarges the allocated array as needed;
30 * // list.head may change due to realloc:
31 * ARRAYLIST_ADD(x, list);
34 * *x = random_foo_value;
36 * for (i = 0; i < list.len; i++)
37 * printf("%s", foo_to_str(list.head[i]));
38 * ARRAYLIST_FREE(list);
41 #include "got_compat.h"
43 #define ARRAYLIST(MEMBER_TYPE) \
48 unsigned int allocated; \
49 unsigned int alloc_blocksize; \
52 #define ARRAYLIST_INIT(ARRAY_LIST, ALLOC_BLOCKSIZE) do { \
53 (ARRAY_LIST).head = NULL; \
54 (ARRAY_LIST).len = 0; \
55 (ARRAY_LIST).allocated = 0; \
56 (ARRAY_LIST).alloc_blocksize = ALLOC_BLOCKSIZE; \
59 #define ARRAYLIST_ADD(NEW_ITEM_P, ARRAY_LIST) do { \
60 if ((ARRAY_LIST).len && !(ARRAY_LIST).allocated) { \
64 if ((ARRAY_LIST).head == NULL \
65 || (ARRAY_LIST).allocated < (ARRAY_LIST).len + 1) { \
66 (ARRAY_LIST).p = recallocarray((ARRAY_LIST).head, \
68 (ARRAY_LIST).allocated + \
69 ((ARRAY_LIST).allocated ? \
70 (ARRAY_LIST).allocated / 2 : \
71 (ARRAY_LIST).alloc_blocksize ? \
72 (ARRAY_LIST).alloc_blocksize : 8), \
73 sizeof(*(ARRAY_LIST).head)); \
74 if ((ARRAY_LIST).p == NULL) { \
78 (ARRAY_LIST).allocated += \
79 (ARRAY_LIST).allocated ? \
80 (ARRAY_LIST).allocated / 2 : \
81 (ARRAY_LIST).alloc_blocksize ? \
82 (ARRAY_LIST).alloc_blocksize : 8, \
83 (ARRAY_LIST).head = (ARRAY_LIST).p; \
84 (ARRAY_LIST).p = NULL; \
86 if ((ARRAY_LIST).head == NULL \
87 || (ARRAY_LIST).allocated < (ARRAY_LIST).len + 1) { \
91 (NEW_ITEM_P) = &(ARRAY_LIST).head[(ARRAY_LIST).len]; \
95 #define ARRAYLIST_INSERT(NEW_ITEM_P, ARRAY_LIST, AT_IDX) do { \
96 int _at_idx = (AT_IDX); \
97 ARRAYLIST_ADD(NEW_ITEM_P, ARRAY_LIST); \
100 && _at_idx < (ARRAY_LIST).len) { \
101 memmove(&(ARRAY_LIST).head[_at_idx + 1], \
102 &(ARRAY_LIST).head[_at_idx], \
103 ((ARRAY_LIST).len - 1 - _at_idx) \
104 * sizeof(*(ARRAY_LIST).head)); \
105 (NEW_ITEM_P) = &(ARRAY_LIST).head[_at_idx]; \
109 #define ARRAYLIST_CLEAR(ARRAY_LIST) \
112 #define ARRAYLIST_FREE(ARRAY_LIST) \
114 if ((ARRAY_LIST).head && (ARRAY_LIST).allocated) \
115 free((ARRAY_LIST).head); \
116 ARRAYLIST_INIT(ARRAY_LIST, (ARRAY_LIST).alloc_blocksize); \
119 #define ARRAYLIST_FOREACH(ITEM_P, ARRAY_LIST) \
120 for ((ITEM_P) = (ARRAY_LIST).head; \
121 (ITEM_P) - (ARRAY_LIST).head < (ARRAY_LIST).len; \
124 #define ARRAYLIST_IDX(ITEM_P, ARRAY_LIST) ((ITEM_P) - (ARRAY_LIST).head)