4 * Copyright (c) 2006 Nicholas Marriott <nicm@users.sourceforge.net>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #define ARRAY_INITIALIZER { NULL, 0, 0 }
24 #define ARRAY_DECL(n, c) \
31 #define ARRAY_ITEM(a, i) ((a)->list[i])
32 #define ARRAY_ITEMSIZE(a) (sizeof *(a)->list)
33 #define ARRAY_INITIALSPACE(a) (10 * ARRAY_ITEMSIZE(a))
35 #define ARRAY_ENSURE(a, n) do { \
36 if (UINT_MAX - (n) < (a)->num) \
37 fatalx("number too big"); \
38 if (SIZE_MAX / ((a)->num + (n)) < ARRAY_ITEMSIZE(a)) \
39 fatalx("size too big"); \
40 if ((a)->space == 0) { \
41 (a)->space = ARRAY_INITIALSPACE(a); \
42 (a)->list = xrealloc((a)->list, 1, (a)->space); \
44 while ((a)->space <= ((a)->num + (n)) * ARRAY_ITEMSIZE(a)) { \
45 (a)->list = xrealloc((a)->list, 2, (a)->space); \
50 #define ARRAY_EMPTY(a) (((void *) (a)) == NULL || (a)->num == 0)
51 #define ARRAY_LENGTH(a) ((a)->num)
52 #define ARRAY_DATA(a) ((a)->list)
54 #define ARRAY_FIRST(a) ARRAY_ITEM(a, 0)
55 #define ARRAY_LAST(a) ARRAY_ITEM(a, (a)->num - 1)
57 #define ARRAY_INIT(a) do { \
62 #define ARRAY_CLEAR(a) do { \
66 #define ARRAY_SET(a, i, s) do { \
70 #define ARRAY_ADD(a, s) do { \
72 (a)->list[(a)->num] = s; \
75 #define ARRAY_INSERT(a, i, s) do { \
77 if ((i) < (a)->num) { \
78 memmove((a)->list + (i) + 1, (a)->list + (i), \
79 ARRAY_ITEMSIZE(a) * ((a)->num - (i))); \
84 #define ARRAY_REMOVE(a, i) do { \
85 if ((i) < (a)->num - 1) { \
86 memmove((a)->list + (i), (a)->list + (i) + 1, \
87 ARRAY_ITEMSIZE(a) * ((a)->num - (i) - 1)); \
94 #define ARRAY_EXPAND(a, n) do { \
98 #define ARRAY_TRUNC(a, n) do { \
105 #define ARRAY_CONCAT(a, b) do { \
106 ARRAY_ENSURE(a, (b)->num); \
107 memcpy((a)->list + (a)->num, (b)->list, (b)->num * ARRAY_ITEMSIZE(a)); \
108 (a)->num += (b)->num; \
111 #define ARRAY_FREE(a) do { \
115 #define ARRAY_FREEALL(a) do { \