Mercurial > ~dholland > hg > tradcpp > index.cgi
annotate array.h @ 80:7e64b1dd0a26
improve the nested comment test
author | David A. Holland |
---|---|
date | Mon, 10 Jun 2013 20:19:49 -0400 |
parents | 2e25e55dba6b |
children | 0921c47b4f22 |
rev | line source |
---|---|
1 | 1 /*- |
2 * Copyright (c) 2009 The NetBSD Foundation, Inc. | |
3 * All rights reserved. | |
4 * | |
5 * This code is derived from software contributed to The NetBSD Foundation | |
6 * by David A. Holland. | |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions | |
10 * are met: | |
11 * 1. Redistributions of source code must retain the above copyright | |
12 * notice, this list of conditions and the following disclaimer. | |
13 * 2. Redistributions in binary form must reproduce the above copyright | |
14 * notice, this list of conditions and the following disclaimer in the | |
15 * documentation and/or other materials provided with the distribution. | |
16 * | |
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | |
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | |
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
27 * POSSIBILITY OF SUCH DAMAGE. | |
28 */ | |
29 | |
30 #ifndef ARRAY_H | |
31 #define ARRAY_H | |
32 | |
6 | 33 #include "inlinedefs.h" // XXX |
2 | 34 #include "utils.h" |
35 | |
1 | 36 #define ARRAYS_CHECKED |
37 | |
38 #ifdef ARRAYS_CHECKED | |
39 #include <assert.h> | |
40 #define arrayassert assert | |
41 #else | |
42 #define arrayassert(x) ((void)(x)) | |
43 #endif | |
44 | |
47
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
39
diff
changeset
|
45 #ifndef ARRAYINLINE |
2e25e55dba6b
Fix inline usage as per the version in dholland-make2.
David A. Holland
parents:
39
diff
changeset
|
46 #define ARRAYINLINE __c99inline |
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 | 49 //////////////////////////////////////////////////////////// |
50 // type and base operations | |
51 | |
52 struct array { | |
53 void **v; | |
54 unsigned num, max; | |
55 }; | |
56 | |
57 struct array *array_create(void); | |
58 void array_destroy(struct array *); | |
59 void array_init(struct array *); | |
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 | 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 | 66 void array_insert(struct array *a, unsigned index_); |
1 | 67 void array_remove(struct array *a, unsigned index_); |
68 | |
69 //////////////////////////////////////////////////////////// | |
70 // inlining for base operations | |
71 | |
72 ARRAYINLINE unsigned | |
73 array_num(const struct array *a) | |
74 { | |
75 return a->num; | |
76 } | |
77 | |
78 ARRAYINLINE void * | |
79 array_get(const struct array *a, unsigned index_) | |
80 { | |
81 arrayassert(index_ < a->num); | |
82 return a->v[index_]; | |
83 } | |
84 | |
85 ARRAYINLINE void | |
86 array_set(const struct array *a, unsigned index_, void *val) | |
87 { | |
88 arrayassert(index_ < a->num); | |
89 a->v[index_] = val; | |
90 } | |
91 | |
2 | 92 ARRAYINLINE void |
1 | 93 array_add(struct array *a, void *val, unsigned *index_ret) |
94 { | |
95 unsigned index_ = a->num; | |
2 | 96 array_setsize(a, index_+1); |
1 | 97 a->v[index_] = val; |
98 if (index_ret != NULL) { | |
99 *index_ret = index_; | |
100 } | |
101 } | |
102 | |
103 //////////////////////////////////////////////////////////// | |
104 // bits for declaring and defining typed arrays | |
105 | |
106 /* | |
107 * Usage: | |
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 | 110 * an array of pointers to "bar", plus the operations on it. |
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 | 114 * |
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 | 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 | 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 | 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 | 160 */ |
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 | 178 |
179 | |
180 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \ | |
181 INLINE void \ | |
182 ARRAY##_init(struct ARRAY *a) \ | |
183 { \ | |
184 array_init(&a->arr); \ | |
185 } \ | |
186 \ | |
187 INLINE void \ | |
188 ARRAY##_cleanup(struct ARRAY *a) \ | |
189 { \ | |
190 array_cleanup(&a->arr); \ | |
191 } \ | |
192 \ | |
193 INLINE struct \ | |
194 ARRAY *ARRAY##_create(void) \ | |
195 { \ | |
196 struct ARRAY *a; \ | |
197 \ | |
2 | 198 a = domalloc(sizeof(*a)); \ |
1 | 199 ARRAY##_init(a); \ |
200 return a; \ | |
201 } \ | |
202 \ | |
203 INLINE void \ | |
204 ARRAY##_destroy(struct ARRAY *a) \ | |
205 { \ | |
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 | 208 } \ |
209 \ | |
210 INLINE unsigned \ | |
211 ARRAY##_num(const struct ARRAY *a) \ | |
212 { \ | |
213 return array_num(&a->arr); \ | |
214 } \ | |
215 \ | |
216 INLINE T * \ | |
217 ARRAY##_get(const struct ARRAY *a, unsigned index_) \ | |
218 { \ | |
219 return (T *)array_get(&a->arr, index_); \ | |
220 } \ | |
221 \ | |
222 INLINE void \ | |
223 ARRAY##_set(struct ARRAY *a, unsigned index_, T *val) \ | |
224 { \ | |
225 array_set(&a->arr, index_, (void *)val); \ | |
226 } \ | |
227 \ | |
2 | 228 INLINE void \ |
1 | 229 ARRAY##_setsize(struct ARRAY *a, unsigned num) \ |
230 { \ | |
2 | 231 array_setsize(&a->arr, num); \ |
1 | 232 } \ |
233 \ | |
2 | 234 INLINE void \ |
1 | 235 ARRAY##_add(struct ARRAY *a, T *val, unsigned *ret) \ |
236 { \ | |
2 | 237 array_add(&a->arr, (void *)val, ret); \ |
1 | 238 } \ |
239 \ | |
2 | 240 INLINE void \ |
1 | 241 ARRAY##_insert(struct ARRAY *a, unsigned index_) \ |
242 { \ | |
2 | 243 array_insert(&a->arr, index_); \ |
1 | 244 } \ |
245 \ | |
246 INLINE void \ | |
247 ARRAY##_remove(struct ARRAY *a, unsigned index_) \ | |
248 { \ | |
2 | 249 array_remove(&a->arr, index_); \ |
1 | 250 } |
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 | 253 #define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE) |
254 | |
9 | 255 #define DESTROYALL_ARRAY(T, INLINE) \ |
256 void T##array_destroyall(struct T##array *arr); \ | |
257 \ | |
258 INLINE void \ | |
259 T##array_destroyall(struct T##array *arr) \ | |
260 { \ | |
261 unsigned i, num; \ | |
262 struct T *t; \ | |
263 \ | |
264 num = T##array_num(arr); \ | |
265 for (i=0; i<num; i++) { \ | |
266 t = T##array_get(arr, i); \ | |
267 T##_destroy(t); \ | |
268 } \ | |
269 T##array_setsize(arr, 0); \ | |
270 } | |
271 | |
272 | |
1 | 273 //////////////////////////////////////////////////////////// |
274 // basic array types | |
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 | 277 DEFARRAY_BYTYPE(stringarray, char, ARRAYINLINE); |
278 | |
279 #endif /* ARRAY_H */ |