annotate array.h @ 136:59680a727e9d

Improve previous. Just in case we ever crash and reach cleanup() while processing an -include foo option, take the array entry for it out of the array to make sure it doesn't get freed twice. This case shouldn't be reachable, but it's better to be safe.
author David A. Holland
date Tue, 09 Jul 2013 13:38:43 -0400
parents 0921c47b4f22
children c440247cbd69
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
1 /*-
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
2 * Copyright (c) 2009 The NetBSD Foundation, Inc.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
3 * All rights reserved.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
4 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
5 * This code is derived from software contributed to The NetBSD Foundation
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
6 * by David A. Holland.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
7 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
9 * modification, are permitted provided that the following conditions
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
10 * are met:
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
15 * documentation and/or other materials provided with the distribution.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
16 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
27 * POSSIBILITY OF SUCH DAMAGE.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
28 */
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
29
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
30 #ifndef ARRAY_H
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
31 #define ARRAY_H
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
32
6
0601b6e8e53d checkpoint - can find files
David A. Holland
parents: 2
diff changeset
33 #include "inlinedefs.h" // XXX
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
34 #include "utils.h"
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
35
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
36 #define ARRAYS_CHECKED
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
37
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
38 #ifdef ARRAYS_CHECKED
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
39 #include <assert.h>
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
40 #define arrayassert assert
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
41 #else
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
42 #define arrayassert(x) ((void)(x))
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
43 #endif
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
44
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
45 #ifndef ARRAYINLINE
108
0921c47b4f22 C99INLINE, not __c99inline
David A. Holland
parents: 47
diff changeset
46 #define ARRAYINLINE C99INLINE
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
47 #endif
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
48
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
49 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
50 // type and base operations
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
51
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
52 struct array {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
53 void **v;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
54 unsigned num, max;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
55 };
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
56
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
57 struct array *array_create(void);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
58 void array_destroy(struct array *);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
59 void array_init(struct array *);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
60 void array_cleanup(struct array *);
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
61 ARRAYINLINE unsigned array_num(const struct array *);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
62 ARRAYINLINE void *array_get(const struct array *, unsigned index_);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
63 ARRAYINLINE void array_set(const struct array *, unsigned index_, void *val);
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
64 void array_setsize(struct array *, unsigned num);
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
65 ARRAYINLINE void array_add(struct array *, void *val, unsigned *index_ret);
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
66 void array_insert(struct array *a, unsigned index_);
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
67 void array_remove(struct array *a, unsigned index_);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
68
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
69 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
70 // inlining for base operations
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
71
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
72 ARRAYINLINE unsigned
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
73 array_num(const struct array *a)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
74 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
75 return a->num;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
76 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
77
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
78 ARRAYINLINE void *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
79 array_get(const struct array *a, unsigned index_)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
80 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
81 arrayassert(index_ < a->num);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
82 return a->v[index_];
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
83 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
84
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
85 ARRAYINLINE void
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
86 array_set(const struct array *a, unsigned index_, void *val)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
87 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
88 arrayassert(index_ < a->num);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
89 a->v[index_] = val;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
90 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
91
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
92 ARRAYINLINE void
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
93 array_add(struct array *a, void *val, unsigned *index_ret)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
94 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
95 unsigned index_ = a->num;
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
96 array_setsize(a, index_+1);
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
97 a->v[index_] = val;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
98 if (index_ret != NULL) {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
99 *index_ret = index_;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
100 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
101 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
102
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
103 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
104 // bits for declaring and defining typed arrays
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
105
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
106 /*
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
107 * Usage:
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
108 *
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
109 * DECLARRAY_BYTYPE(foo, bar, INLINE) declares "struct foo", which is
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
110 * an array of pointers to "bar", plus the operations on it.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
111 *
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
112 * DECLARRAY(foo, INLINE) is equivalent to
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
113 * DECLARRAY_BYTYPE(fooarray, struct foo, INLINE).
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
114 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
115 * DEFARRAY_BYTYPE and DEFARRAY are the same as DECLARRAY except that
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
116 * they define the operations.
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
117 *
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
118 * The argument INLINE can be used as follows:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
119 *
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
120 * 1. For no inlining:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
121 * In foo.h:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
122 * DECLARRAY(foo, );
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
123 * In foo.c:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
124 * DEFARRAY(foo, );
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
125 *
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
126 * 2. To be file-static:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
127 * In foo.c:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
128 * DECLARRAY(foo, static);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
129 * DEFARRAY(foo, static);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
130 *
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
131 * 3. To inline using C99:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
132 * In foo.h:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
133 * DECLARRAY(foo, inline);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
134 * DEFARRAY(foo, inline);
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
135 *
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
136 * 4. To inline with old gcc:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
137 * In foo.h:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
138 * #ifndef FOO_INLINE
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
139 * #define FOO_INLINE extern inline
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
140 * #endif
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
141 * DECLARRAY(foo, );
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
142 * DEFARRAY(foo, FOO_INLINE);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
143 * In foo.c:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
144 * #define FOO_INLINE
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
145 * #include "foo.h"
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
146 *
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
147 * 5. To inline such that it works both with old gcc and C99:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
148 * In foo.h:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
149 * #ifndef FOO_INLINE
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
150 * #define FOO_INLINE extern inline
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
151 * #endif
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
152 * DECLARRAY(foo, FOO_INLINE);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
153 * DEFARRAY(foo, FOO_INLINE);
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
154 * In foo.c:
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
155 * #define FOO_INLINE
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
156 * #include "foo.h"
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
157 *
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
158 * The mechanism in case (4) ensures that an externally linkable
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
159 * definition exists.
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
160 */
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
161
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
162 #define DECLARRAY_BYTYPE(ARRAY, T, INLINE) \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
163 struct ARRAY { \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
164 struct array arr; \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
165 }; \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
166 \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
167 INLINE struct ARRAY *ARRAY##_create(void); \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
168 INLINE void ARRAY##_destroy(struct ARRAY *a); \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
169 INLINE void ARRAY##_init(struct ARRAY *a); \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
170 INLINE void ARRAY##_cleanup(struct ARRAY *a); \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
171 INLINE unsigned ARRAY##_num(const struct ARRAY *a); \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
172 INLINE T *ARRAY##_get(const struct ARRAY *a, unsigned index_); \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
173 INLINE void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
174 INLINE void ARRAY##_setsize(struct ARRAY *a, unsigned num); \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
175 INLINE void ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret);\
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
176 INLINE void ARRAY##_insert(struct ARRAY *a, unsigned index_); \
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
177 INLINE void ARRAY##_remove(struct ARRAY *a, unsigned index_)
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
178
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
179
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
180 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
181 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
182 ARRAY##_init(struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
183 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
184 array_init(&a->arr); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
185 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
186 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
187 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
188 ARRAY##_cleanup(struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
189 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
190 array_cleanup(&a->arr); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
191 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
192 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
193 INLINE struct \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
194 ARRAY *ARRAY##_create(void) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
195 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
196 struct ARRAY *a; \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
197 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
198 a = domalloc(sizeof(*a)); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
199 ARRAY##_init(a); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
200 return a; \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
201 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
202 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
203 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
204 ARRAY##_destroy(struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
205 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
206 ARRAY##_cleanup(a); \
39
337110e7240a Pass the size to free; it makes debug checking easier.
David A. Holland
parents: 38
diff changeset
207 dofree(a, sizeof(*a)); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
208 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
209 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
210 INLINE unsigned \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
211 ARRAY##_num(const struct ARRAY *a) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
212 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
213 return array_num(&a->arr); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
214 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
215 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
216 INLINE T * \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
217 ARRAY##_get(const struct ARRAY *a, unsigned index_) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
218 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
219 return (T *)array_get(&a->arr, index_); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
220 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
221 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
222 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
223 ARRAY##_set(struct ARRAY *a, unsigned index_, T *val) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
224 { \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
225 array_set(&a->arr, index_, (void *)val); \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
226 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
227 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
228 INLINE void \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
229 ARRAY##_setsize(struct ARRAY *a, unsigned num) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
230 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
231 array_setsize(&a->arr, num); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
232 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
233 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
234 INLINE void \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
235 ARRAY##_add(struct ARRAY *a, T *val, unsigned *ret) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
236 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
237 array_add(&a->arr, (void *)val, ret); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
238 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
239 \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
240 INLINE void \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
241 ARRAY##_insert(struct ARRAY *a, unsigned index_) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
242 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
243 array_insert(&a->arr, index_); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
244 } \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
245 \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
246 INLINE void \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
247 ARRAY##_remove(struct ARRAY *a, unsigned index_) \
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
248 { \
2
9c1cecba517c Make arrays crash on malloc failure.
David A. Holland
parents: 1
diff changeset
249 array_remove(&a->arr, index_); \
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
250 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
251
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
252 #define DECLARRAY(T, INLINE) DECLARRAY_BYTYPE(T##array, struct T, INLINE)
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
253 #define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
254
9
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
255 #define DESTROYALL_ARRAY(T, INLINE) \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
256 void T##array_destroyall(struct T##array *arr); \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
257 \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
258 INLINE void \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
259 T##array_destroyall(struct T##array *arr) \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
260 { \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
261 unsigned i, num; \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
262 struct T *t; \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
263 \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
264 num = T##array_num(arr); \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
265 for (i=0; i<num; i++) { \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
266 t = T##array_get(arr, i); \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
267 T##_destroy(t); \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
268 } \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
269 T##array_setsize(arr, 0); \
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
270 }
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
271
1fbcbd58742e move destroyall to array.h
David A. Holland
parents: 6
diff changeset
272
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
273 ////////////////////////////////////////////////////////////
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
274 // basic array types
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
275
47
2e25e55dba6b Fix inline usage as per the version in dholland-make2.
David A. Holland
parents: 39
diff changeset
276 DECLARRAY_BYTYPE(stringarray, char, ARRAYINLINE);
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
277 DEFARRAY_BYTYPE(stringarray, char, ARRAYINLINE);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
278
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
279 #endif /* ARRAY_H */