Blame


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