Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/kern/include/array.h
2093 views
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
33
#define ARRAYS_CHECKED
34
35
#ifdef ARRAYS_CHECKED
36
#define ARRAYASSERT KASSERT
37
#else
38
#define ARRAYASSERT(x) ((void)(x))
39
#endif
40
41
/*
42
* Base array type (resizeable array of void pointers) and operations.
43
*
44
* create - allocate an array.
45
* destroy - destroy an allocated array.
46
* init - initialize an array in space externally allocated.
47
* cleanup - clean up an array in space exteranlly allocated.
48
* num - return number of elements in array.
49
* get - return element no. INDEX.
50
* set - set element no. INDEX to VAL.
51
* setsize - change size to NUM elements; may fail and return error.
52
* add - append VAL to end of array; return its index in INDEX_RET if
53
* INDEX_RET isn't null; may fail and return error.
54
* remove - excise entry INDEX and slide following entries down to
55
* close the resulting gap.
56
*
57
* Note that expanding an array with setsize doesn't initialize the new
58
* elements. (Usually the caller is about to store into them anyway.)
59
*/
60
61
struct array {
62
void **v;
63
unsigned num, max;
64
};
65
66
struct array *array_create(void);
67
void array_destroy(struct array *);
68
void array_init(struct array *);
69
void array_cleanup(struct array *);
70
unsigned array_num(const struct array *);
71
void *array_get(const struct array *, unsigned index);
72
void array_set(const struct array *, unsigned index, void *val);
73
int array_setsize(struct array *, unsigned num);
74
int array_add(struct array *, void *val, unsigned *index_ret);
75
void array_remove(struct array *, unsigned index);
76
77
/*
78
* Inlining for base operations
79
*/
80
81
#ifndef ARRAYINLINE
82
#define ARRAYINLINE INLINE
83
#endif
84
85
ARRAYINLINE unsigned
86
array_num(const struct array *a)
87
{
88
return a->num;
89
}
90
91
ARRAYINLINE void *
92
array_get(const struct array *a, unsigned index)
93
{
94
ARRAYASSERT(index < a->num);
95
return a->v[index];
96
}
97
98
ARRAYINLINE void
99
array_set(const struct array *a, unsigned index, void *val)
100
{
101
ARRAYASSERT(index < a->num);
102
a->v[index] = val;
103
}
104
105
ARRAYINLINE int
106
array_add(struct array *a, void *val, unsigned *index_ret)
107
{
108
unsigned index;
109
int ret;
110
111
index = a->num;
112
ret = array_setsize(a, index+1);
113
if (ret) {
114
return ret;
115
}
116
a->v[index] = val;
117
if (index_ret != NULL) {
118
*index_ret = index;
119
}
120
return 0;
121
}
122
123
/*
124
* Bits for declaring and defining typed arrays.
125
*
126
* Usage:
127
*
128
* DECLARRAY_BYTYPE(foo, bar) declares "struct foo", which is
129
* an array of pointers to "bar", plus the operations on it.
130
*
131
* DECLARRAY(foo) is equivalent to DECLARRAY_BYTYPE(fooarray, struct foo).
132
*
133
* DEFARRAY_BYTYPE and DEFARRAY are the same as DECLARRAY except that
134
* they define the operations, and both take an extra argument INLINE.
135
* For C99 this should be INLINE in header files and empty in the
136
* master source file, the same as the usage of ARRAYINLINE above and
137
* in array.c.
138
*
139
* Example usage in e.g. item.h of some game:
140
*
141
* DECLARRAY_BYTYPE(stringarray, char);
142
* DECLARRAY(potion);
143
* DECLARRAY(sword);
144
*
145
* #ifndef ITEMINLINE
146
* #define ITEMINLINE INLINE
147
* #endif
148
*
149
* DEFARRAY_BYTYPE(stringarray, char, ITEMINLINE);
150
* DEFARRAY(potion, ITEMINLINE);
151
* DEFARRAY(sword, ITEMINLINE);
152
*
153
* Then item.c would do "#define ITEMINLINE" before including item.h.
154
*
155
* This creates types "struct stringarray", "struct potionarray",
156
* and "struct swordarray", with operations such as "swordarray_num".
157
*
158
* The operations on typed arrays are the same as the operations on
159
* the base array, except typed.
160
*/
161
162
#define DECLARRAY_BYTYPE(ARRAY, T) \
163
struct ARRAY { \
164
struct array arr; \
165
}; \
166
\
167
struct ARRAY *ARRAY##_create(void); \
168
void ARRAY##_destroy(struct ARRAY *a); \
169
void ARRAY##_init(struct ARRAY *a); \
170
void ARRAY##_cleanup(struct ARRAY *a); \
171
unsigned ARRAY##_num(const struct ARRAY *a); \
172
T *ARRAY##_get(const struct ARRAY *a, unsigned index); \
173
void ARRAY##_set(struct ARRAY *a, unsigned index, T *val); \
174
int ARRAY##_setsize(struct ARRAY *a, unsigned num); \
175
int ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \
176
void ARRAY##_remove(struct ARRAY *a, unsigned index)
177
178
#define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \
179
INLINE struct ARRAY * \
180
ARRAY##_create(void) \
181
{ \
182
struct ARRAY *a = kmalloc(sizeof(*a)); \
183
if (a == NULL) { \
184
return NULL; \
185
} \
186
array_init(&a->arr); \
187
return a; \
188
} \
189
\
190
INLINE void \
191
ARRAY##_destroy(struct ARRAY *a) \
192
{ \
193
array_cleanup(&a->arr); \
194
kfree(a); \
195
} \
196
\
197
INLINE void \
198
ARRAY##_init(struct ARRAY *a) \
199
{ \
200
array_init(&a->arr); \
201
} \
202
\
203
INLINE void \
204
ARRAY##_cleanup(struct ARRAY *a) \
205
{ \
206
array_cleanup(&a->arr); \
207
} \
208
\
209
INLINE unsigned \
210
ARRAY##_num(const struct ARRAY *a) \
211
{ \
212
return array_num(&a->arr); \
213
} \
214
\
215
INLINE T * \
216
ARRAY##_get(const struct ARRAY *a, unsigned index) \
217
{ \
218
return (T *)array_get(&a->arr, index); \
219
} \
220
\
221
INLINE void \
222
ARRAY##_set(struct ARRAY *a, unsigned index, T *val) \
223
{ \
224
array_set(&a->arr, index, (void *)val); \
225
} \
226
\
227
INLINE int \
228
ARRAY##_setsize(struct ARRAY *a, unsigned num) \
229
{ \
230
return array_setsize(&a->arr, num); \
231
} \
232
\
233
INLINE int \
234
ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret) \
235
{ \
236
return array_add(&a->arr, (void *)val, index_ret); \
237
} \
238
\
239
INLINE void \
240
ARRAY##_remove(struct ARRAY *a, unsigned index) \
241
{ \
242
return array_remove(&a->arr, index); \
243
}
244
245
#define DECLARRAY(T) DECLARRAY_BYTYPE(T##array, struct T)
246
#define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE)
247
248
/*
249
* This is how you declare an array of strings; it works out as
250
* an array of pointers to char.
251
*/
252
DECLARRAY_BYTYPE(stringarray, char);
253
DEFARRAY_BYTYPE(stringarray, char, ARRAYINLINE);
254
255
256
#endif /* ARRAY_H */
257
258