comparison array.h @ 2:9c1cecba517c

Make arrays crash on malloc failure.
author David A. Holland
date Sun, 19 Dec 2010 16:51:21 -0500
parents 411b28d78483
children 0601b6e8e53d
comparison
equal deleted inserted replaced
1:411b28d78483 2:9c1cecba517c
28 */ 28 */
29 29
30 #ifndef ARRAY_H 30 #ifndef ARRAY_H
31 #define ARRAY_H 31 #define ARRAY_H
32 32
33 #include "utils.h"
34
33 #define ARRAYS_CHECKED 35 #define ARRAYS_CHECKED
34 36
35 #ifdef ARRAYS_CHECKED 37 #ifdef ARRAYS_CHECKED
36 #include <assert.h> 38 #include <assert.h>
37 #define arrayassert assert 39 #define arrayassert assert
52 void array_init(struct array *); 54 void array_init(struct array *);
53 void array_cleanup(struct array *); 55 void array_cleanup(struct array *);
54 unsigned array_num(const struct array *); 56 unsigned array_num(const struct array *);
55 void *array_get(const struct array *, unsigned index_); 57 void *array_get(const struct array *, unsigned index_);
56 void array_set(const struct array *, unsigned index_, void *val); 58 void array_set(const struct array *, unsigned index_, void *val);
57 int array_setsize(struct array *, unsigned num); 59 void array_setsize(struct array *, unsigned num);
58 int array_add(struct array *, void *val, unsigned *index_ret); 60 void array_add(struct array *, void *val, unsigned *index_ret);
59 int array_insert(struct array *a, unsigned index_); 61 void array_insert(struct array *a, unsigned index_);
60 void array_remove(struct array *a, unsigned index_); 62 void array_remove(struct array *a, unsigned index_);
61 63
62 //////////////////////////////////////////////////////////// 64 ////////////////////////////////////////////////////////////
63 // inlining for base operations 65 // inlining for base operations
64 66
84 { 86 {
85 arrayassert(index_ < a->num); 87 arrayassert(index_ < a->num);
86 a->v[index_] = val; 88 a->v[index_] = val;
87 } 89 }
88 90
89 ARRAYINLINE int 91 ARRAYINLINE void
90 array_add(struct array *a, void *val, unsigned *index_ret) 92 array_add(struct array *a, void *val, unsigned *index_ret)
91 { 93 {
92 unsigned index_ = a->num; 94 unsigned index_ = a->num;
93 if (array_setsize(a, index_+1)) { 95 array_setsize(a, index_+1);
94 return -1;
95 }
96 a->v[index_] = val; 96 a->v[index_] = val;
97 if (index_ret != NULL) { 97 if (index_ret != NULL) {
98 *index_ret = index_; 98 *index_ret = index_;
99 } 99 }
100 return 0;
101 } 100 }
102 101
103 //////////////////////////////////////////////////////////// 102 ////////////////////////////////////////////////////////////
104 // bits for declaring and defining typed arrays 103 // bits for declaring and defining typed arrays
105 104
144 void ARRAY##_init(struct ARRAY *a); \ 143 void ARRAY##_init(struct ARRAY *a); \
145 void ARRAY##_cleanup(struct ARRAY *a); \ 144 void ARRAY##_cleanup(struct ARRAY *a); \
146 unsigned ARRAY##_num(const struct ARRAY *a); \ 145 unsigned ARRAY##_num(const struct ARRAY *a); \
147 T *ARRAY##_get(const struct ARRAY *a, unsigned index_); \ 146 T *ARRAY##_get(const struct ARRAY *a, unsigned index_); \
148 void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \ 147 void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \
149 int ARRAY##_setsize(struct ARRAY *a, unsigned num); \ 148 void ARRAY##_setsize(struct ARRAY *a, unsigned num); \
150 int ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \ 149 void ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \
151 int ARRAY##_insert(struct ARRAY *a, unsigned index_); \ 150 void ARRAY##_insert(struct ARRAY *a, unsigned index_); \
152 void ARRAY##_remove(struct ARRAY *a, unsigned index_) 151 void ARRAY##_remove(struct ARRAY *a, unsigned index_)
153 152
154 153
155 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \ 154 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \
156 INLINE void \ 155 INLINE void \
168 INLINE struct \ 167 INLINE struct \
169 ARRAY *ARRAY##_create(void) \ 168 ARRAY *ARRAY##_create(void) \
170 { \ 169 { \
171 struct ARRAY *a; \ 170 struct ARRAY *a; \
172 \ 171 \
173 a = malloc(sizeof(*a)); \ 172 a = domalloc(sizeof(*a)); \
174 if (a == NULL) { \
175 return NULL; \
176 } \
177 ARRAY##_init(a); \ 173 ARRAY##_init(a); \
178 return a; \ 174 return a; \
179 } \ 175 } \
180 \ 176 \
181 INLINE void \ 177 INLINE void \
201 ARRAY##_set(struct ARRAY *a, unsigned index_, T *val) \ 197 ARRAY##_set(struct ARRAY *a, unsigned index_, T *val) \
202 { \ 198 { \
203 array_set(&a->arr, index_, (void *)val); \ 199 array_set(&a->arr, index_, (void *)val); \
204 } \ 200 } \
205 \ 201 \
206 INLINE int \ 202 INLINE void \
207 ARRAY##_setsize(struct ARRAY *a, unsigned num) \ 203 ARRAY##_setsize(struct ARRAY *a, unsigned num) \
208 { \ 204 { \
209 return array_setsize(&a->arr, num); \ 205 array_setsize(&a->arr, num); \
210 } \ 206 } \
211 \ 207 \
212 INLINE int \ 208 INLINE void \
213 ARRAY##_add(struct ARRAY *a, T *val, unsigned *ret) \ 209 ARRAY##_add(struct ARRAY *a, T *val, unsigned *ret) \
214 { \ 210 { \
215 return array_add(&a->arr, (void *)val, ret); \ 211 array_add(&a->arr, (void *)val, ret); \
216 } \ 212 } \
217 \ 213 \
218 INLINE int \ 214 INLINE void \
219 ARRAY##_insert(struct ARRAY *a, unsigned index_) \ 215 ARRAY##_insert(struct ARRAY *a, unsigned index_) \
220 { \ 216 { \
221 return array_insert(&a->arr, index_); \ 217 array_insert(&a->arr, index_); \
222 } \ 218 } \
223 \ 219 \
224 INLINE void \ 220 INLINE void \
225 ARRAY##_remove(struct ARRAY *a, unsigned index_) \ 221 ARRAY##_remove(struct ARRAY *a, unsigned index_) \
226 { \ 222 { \
227 return array_remove(&a->arr, index_); \ 223 array_remove(&a->arr, index_); \
228 } 224 }
229 225
230 #define DECLARRAY(T) DECLARRAY_BYTYPE(T##array, struct T) 226 #define DECLARRAY(T) DECLARRAY_BYTYPE(T##array, struct T)
231 #define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE) 227 #define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE)
232 228