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 1dfba055 2020-10-07 stsp ((ARRAY_LIST).alloc_blocksize ? : 8), \
67 1dfba055 2020-10-07 stsp sizeof(*(ARRAY_LIST).head)); \
68 1dfba055 2020-10-07 stsp if ((ARRAY_LIST).p == NULL) { \
69 1dfba055 2020-10-07 stsp NEW_ITEM_P = NULL; \
70 1dfba055 2020-10-07 stsp break; \
71 1dfba055 2020-10-07 stsp } \
72 1dfba055 2020-10-07 stsp (ARRAY_LIST).allocated += \
73 1dfba055 2020-10-07 stsp (ARRAY_LIST).alloc_blocksize ? : 8; \
74 1dfba055 2020-10-07 stsp (ARRAY_LIST).head = (ARRAY_LIST).p; \
75 1dfba055 2020-10-07 stsp (ARRAY_LIST).p = NULL; \
76 1dfba055 2020-10-07 stsp }; \
77 1dfba055 2020-10-07 stsp if ((ARRAY_LIST).head == NULL \
78 1dfba055 2020-10-07 stsp || (ARRAY_LIST).allocated < (ARRAY_LIST).len + 1) { \
79 1dfba055 2020-10-07 stsp NEW_ITEM_P = NULL; \
80 1dfba055 2020-10-07 stsp break; \
81 1dfba055 2020-10-07 stsp } \
82 1dfba055 2020-10-07 stsp (NEW_ITEM_P) = &(ARRAY_LIST).head[(ARRAY_LIST).len]; \
83 1dfba055 2020-10-07 stsp (ARRAY_LIST).len++; \
84 1dfba055 2020-10-07 stsp } while (0)
85 1dfba055 2020-10-07 stsp
86 1dfba055 2020-10-07 stsp #define ARRAYLIST_INSERT(NEW_ITEM_P, ARRAY_LIST, AT_IDX) do { \
87 1dfba055 2020-10-07 stsp ARRAYLIST_ADD(NEW_ITEM_P, ARRAY_LIST); \
88 1dfba055 2020-10-07 stsp if ((NEW_ITEM_P) && (AT_IDX) < (ARRAY_LIST).len) \
89 1dfba055 2020-10-07 stsp memmove(&(ARRAY_LIST).head[(AT_IDX) + 1], \
90 1dfba055 2020-10-07 stsp &(ARRAY_LIST).head[AT_IDX], \
91 1dfba055 2020-10-07 stsp ((ARRAY_LIST).len - (AT_IDX)) \
92 1dfba055 2020-10-07 stsp * sizeof(*(ARRAY_LIST).head)); \
93 1dfba055 2020-10-07 stsp } while (0)
94 1dfba055 2020-10-07 stsp
95 1dfba055 2020-10-07 stsp #define ARRAYLIST_CLEAR(ARRAY_LIST) \
96 1dfba055 2020-10-07 stsp (ARRAY_LIST).len = 0
97 1dfba055 2020-10-07 stsp
98 1dfba055 2020-10-07 stsp #define ARRAYLIST_FREE(ARRAY_LIST) \
99 1dfba055 2020-10-07 stsp do { \
100 1dfba055 2020-10-07 stsp if ((ARRAY_LIST).head && (ARRAY_LIST).allocated) \
101 1dfba055 2020-10-07 stsp free((ARRAY_LIST).head); \
102 1dfba055 2020-10-07 stsp ARRAYLIST_INIT(ARRAY_LIST, (ARRAY_LIST).alloc_blocksize); \
103 1dfba055 2020-10-07 stsp } while(0)