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 /* Usage:
19 3b0f3d61 2020-01-22 neels *
20 3b0f3d61 2020-01-22 neels * ARRAYLIST(any_type_t) list;
21 3b0f3d61 2020-01-22 neels * // OR
22 3b0f3d61 2020-01-22 neels * typedef ARRAYLIST(any_type_t) any_type_list_t;
23 3b0f3d61 2020-01-22 neels * any_type_list_t list;
24 3b0f3d61 2020-01-22 neels *
25 0d27172a 2020-05-06 neels * // pass the number of (at first unused) members to add on each realloc:
26 0d27172a 2020-05-06 neels * ARRAYLIST_INIT(list, 128);
27 3b0f3d61 2020-01-22 neels * any_type_t *x;
28 3b0f3d61 2020-01-22 neels * while (bar) {
29 0d27172a 2020-05-06 neels * // This enlarges the allocated array as needed;
30 0d27172a 2020-05-06 neels * // list.head may change due to realloc:
31 0d27172a 2020-05-06 neels * ARRAYLIST_ADD(x, list);
32 3b0f3d61 2020-01-22 neels * if (!x)
33 945524ed 2020-09-20 stsp * return ENOMEM;
34 3b0f3d61 2020-01-22 neels * *x = random_foo_value;
35 3b0f3d61 2020-01-22 neels * }
36 3b0f3d61 2020-01-22 neels * for (i = 0; i < list.len; i++)
37 3b0f3d61 2020-01-22 neels * printf("%s", foo_to_str(list.head[i]));
38 3b0f3d61 2020-01-22 neels * ARRAYLIST_FREE(list);
39 3b0f3d61 2020-01-22 neels */
40 3b0f3d61 2020-01-22 neels #define ARRAYLIST(MEMBER_TYPE) \
41 3b0f3d61 2020-01-22 neels struct { \
42 3b0f3d61 2020-01-22 neels MEMBER_TYPE *head; \
43 32be3022 2020-09-22 stsp MEMBER_TYPE *p; \
44 3b0f3d61 2020-01-22 neels unsigned int len; \
45 3b0f3d61 2020-01-22 neels unsigned int allocated; \
46 3b0f3d61 2020-01-22 neels unsigned int alloc_blocksize; \
47 3b0f3d61 2020-01-22 neels }
48 3b0f3d61 2020-01-22 neels
49 3b0f3d61 2020-01-22 neels #define ARRAYLIST_INIT(ARRAY_LIST, ALLOC_BLOCKSIZE) do { \
50 3b0f3d61 2020-01-22 neels (ARRAY_LIST).head = NULL; \
51 3b0f3d61 2020-01-22 neels (ARRAY_LIST).len = 0; \
52 3b0f3d61 2020-01-22 neels (ARRAY_LIST).allocated = 0; \
53 3b0f3d61 2020-01-22 neels (ARRAY_LIST).alloc_blocksize = ALLOC_BLOCKSIZE; \
54 3b0f3d61 2020-01-22 neels } while(0)
55 3b0f3d61 2020-01-22 neels
56 3b0f3d61 2020-01-22 neels #define ARRAYLIST_ADD(NEW_ITEM_P, ARRAY_LIST) do { \
57 3b0f3d61 2020-01-22 neels if ((ARRAY_LIST).len && !(ARRAY_LIST).allocated) { \
58 3b0f3d61 2020-01-22 neels NEW_ITEM_P = NULL; \
59 3b0f3d61 2020-01-22 neels break; \
60 3b0f3d61 2020-01-22 neels } \
61 0d27172a 2020-05-06 neels if ((ARRAY_LIST).head == NULL \
62 0d27172a 2020-05-06 neels || (ARRAY_LIST).allocated < (ARRAY_LIST).len + 1) { \
63 32be3022 2020-09-22 stsp (ARRAY_LIST).p = recallocarray((ARRAY_LIST).head, \
64 0d27172a 2020-05-06 neels (ARRAY_LIST).len, \
65 bf11ce0e 2020-09-22 stsp (ARRAY_LIST).allocated + \
66 9ecc16c7 2020-09-22 stsp ((ARRAY_LIST).alloc_blocksize ? : 8), \
67 0d27172a 2020-05-06 neels sizeof(*(ARRAY_LIST).head)); \
68 32be3022 2020-09-22 stsp if ((ARRAY_LIST).p == NULL) { \
69 32be3022 2020-09-22 stsp NEW_ITEM_P = NULL; \
70 32be3022 2020-09-22 stsp break; \
71 32be3022 2020-09-22 stsp } \
72 bf11ce0e 2020-09-22 stsp (ARRAY_LIST).allocated += \
73 bf11ce0e 2020-09-22 stsp (ARRAY_LIST).alloc_blocksize ? : 8; \
74 32be3022 2020-09-22 stsp (ARRAY_LIST).head = (ARRAY_LIST).p; \
75 32be3022 2020-09-22 stsp (ARRAY_LIST).p = NULL; \
76 3b0f3d61 2020-01-22 neels }; \
77 32be3022 2020-09-22 stsp if ((ARRAY_LIST).head == NULL \
78 0d27172a 2020-05-06 neels || (ARRAY_LIST).allocated < (ARRAY_LIST).len + 1) { \
79 3b0f3d61 2020-01-22 neels NEW_ITEM_P = NULL; \
80 3b0f3d61 2020-01-22 neels break; \
81 3b0f3d61 2020-01-22 neels } \
82 3b0f3d61 2020-01-22 neels (NEW_ITEM_P) = &(ARRAY_LIST).head[(ARRAY_LIST).len]; \
83 3b0f3d61 2020-01-22 neels (ARRAY_LIST).len++; \
84 3b0f3d61 2020-01-22 neels } while (0)
85 3b0f3d61 2020-01-22 neels
86 2c8a57df 2020-09-20 neels #define ARRAYLIST_INSERT(NEW_ITEM_P, ARRAY_LIST, AT_IDX) do { \
87 2c8a57df 2020-09-20 neels ARRAYLIST_ADD(NEW_ITEM_P, ARRAY_LIST); \
88 dabc1008 2020-09-22 stsp if ((NEW_ITEM_P) && (AT_IDX) < (ARRAY_LIST).len) \
89 2c8a57df 2020-09-20 neels memmove(&(ARRAY_LIST).head[(AT_IDX) + 1], \
90 2c8a57df 2020-09-20 neels &(ARRAY_LIST).head[AT_IDX], \
91 2c8a57df 2020-09-20 neels ((ARRAY_LIST).len - (AT_IDX)) \
92 2c8a57df 2020-09-20 neels * sizeof(*(ARRAY_LIST).head)); \
93 2c8a57df 2020-09-20 neels } while (0)
94 2c8a57df 2020-09-20 neels
95 3b0f3d61 2020-01-22 neels #define ARRAYLIST_CLEAR(ARRAY_LIST) \
96 3b0f3d61 2020-01-22 neels (ARRAY_LIST).len = 0
97 3b0f3d61 2020-01-22 neels
98 3b0f3d61 2020-01-22 neels #define ARRAYLIST_FREE(ARRAY_LIST) \
99 3b0f3d61 2020-01-22 neels do { \
100 3b0f3d61 2020-01-22 neels if ((ARRAY_LIST).head && (ARRAY_LIST).allocated) \
101 3b0f3d61 2020-01-22 neels free((ARRAY_LIST).head); \
102 3b0f3d61 2020-01-22 neels ARRAYLIST_INIT(ARRAY_LIST, (ARRAY_LIST).alloc_blocksize); \
103 3b0f3d61 2020-01-22 neels } while(0)