Home | History | Annotate | Line # | Download | only in sail
array.h revision 1.1.2.2
      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 #include "inlinedefs.h"
     34 
     35 #define ARRAYS_CHECKED
     36 
     37 #ifdef ARRAYS_CHECKED
     38 #include <assert.h>
     39 #define arrayassert assert
     40 #else
     41 #define arrayassert(x) ((void)(x))
     42 #endif
     43 
     44 ////////////////////////////////////////////////////////////
     45 // type and base operations
     46 
     47 struct array {
     48 	void **v;
     49 	unsigned num, max;
     50 };
     51 
     52 struct array *array_create(void);
     53 void array_destroy(struct array *);
     54 void array_init(struct array *);
     55 void array_cleanup(struct array *);
     56 unsigned array_num(const struct array *);
     57 void *array_get(const struct array *, unsigned index_);
     58 void array_set(const struct array *, unsigned index_, void *val);
     59 int array_setsize(struct array *, unsigned num);
     60 int array_add(struct array *, void *val, unsigned *index_ret);
     61 int array_insert(struct array *a, unsigned index_);
     62 void array_remove(struct array *a, unsigned index_);
     63 
     64 ////////////////////////////////////////////////////////////
     65 // inlining for base operations
     66 
     67 #ifndef ARRAYINLINE
     68 #define ARRAYINLINE INLINE
     69 #endif
     70 
     71 ARRAYINLINE unsigned
     72 array_num(const struct array *a)
     73 {
     74 	return a->num;
     75 }
     76 
     77 ARRAYINLINE void *
     78 array_get(const struct array *a, unsigned index_)
     79 {
     80 	arrayassert(index_ < a->num);
     81 	return a->v[index_];
     82 }
     83 
     84 ARRAYINLINE void
     85 array_set(const struct array *a, unsigned index_, void *val)
     86 {
     87 	arrayassert(index_ < a->num);
     88 	a->v[index_] = val;
     89 }
     90 
     91 ARRAYINLINE int
     92 array_add(struct array *a, void *val, unsigned *index_ret)
     93 {
     94 	unsigned index_ = a->num;
     95 	if (array_setsize(a, index_+1)) {
     96 		return -1;
     97 	}
     98 	a->v[index_] = val;
     99 	if (index_ret != NULL) {
    100 		*index_ret = index_;
    101 	}
    102 	return 0;
    103 }
    104 
    105 ////////////////////////////////////////////////////////////
    106 // bits for declaring and defining typed arrays
    107 
    108 /*
    109  * Usage:
    110  *
    111  * DECLARRAY_BYTYPE(foo, bar) declares "struct foo", which is
    112  * an array of pointers to "bar", plus the operations on it.
    113  *
    114  * DECLARRAY(foo) is equivalent to DECLARRAY_BYTYPE(fooarray, struct foo).
    115  *
    116  * DEFARRAY_BYTYPE and DEFARRAY are the same as DECLARRAY except that
    117  * they define the operations, and both take an extra argument INLINE.
    118  * For C99 this should be INLINE in header files and empty in the
    119  * master source file, the same as the usage of ARRAYINLINE above and
    120  * in array.c.
    121  *
    122  * Example usage in e.g. item.h of some game:
    123  *
    124  * DECLARRAY_BYTYPE(stringarray, char);
    125  * DECLARRAY(potion);
    126  * DECLARRAY(sword);
    127  *
    128  * #ifndef ITEMINLINE
    129  * #define ITEMINLINE INLINE
    130  * #endif
    131  *
    132  * DEFARRAY_BYTYPE(stringarray, char, ITEMINLINE);
    133  * DEFARRAY(potion, ITEMINLINE);
    134  * DEFARRAY(sword, ITEMINLINE);
    135  *
    136  * Then item.c would do "#define ITEMINLINE" before including item.h.
    137  */
    138 
    139 #define DECLARRAY_BYTYPE(ARRAY, T) \
    140 	struct ARRAY {						\
    141 		struct array arr;				\
    142 	};							\
    143 								\
    144 	struct ARRAY *ARRAY##_create(void);			\
    145 	void ARRAY##_destroy(struct ARRAY *a);			\
    146 	void ARRAY##_init(struct ARRAY *a);			\
    147 	void ARRAY##_cleanup(struct ARRAY *a);			\
    148 	unsigned ARRAY##_num(const struct ARRAY *a);		\
    149 	T *ARRAY##_get(const struct ARRAY *a, unsigned index_);	\
    150 	void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \
    151 	int ARRAY##_setsize(struct ARRAY *a, unsigned num);	\
    152 	int ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \
    153 	int ARRAY##_insert(struct ARRAY *a, unsigned index_);	\
    154 	void ARRAY##_remove(struct ARRAY *a, unsigned index_)
    155 
    156 
    157 #define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \
    158 	INLINE void						\
    159 	ARRAY##_init(struct ARRAY *a)				\
    160 	{							\
    161 		array_init(&a->arr);				\
    162 	}							\
    163 								\
    164 	INLINE void						\
    165 	ARRAY##_cleanup(struct ARRAY *a)			\
    166 	{							\
    167 		array_cleanup(&a->arr);				\
    168 	}							\
    169 								\
    170 	INLINE struct						\
    171 	ARRAY *ARRAY##_create(void)				\
    172 	{							\
    173 		struct ARRAY *a;				\
    174 								\
    175 		a  = malloc(sizeof(*a));			\
    176 		if (a == NULL) {				\
    177 			return NULL;				\
    178 		}						\
    179 		ARRAY##_init(a);				\
    180 		return a;					\
    181 	}							\
    182 								\
    183 	INLINE void						\
    184 	ARRAY##_destroy(struct ARRAY *a)			\
    185 	{							\
    186 		ARRAY##_cleanup(a);				\
    187 		free(a);					\
    188 	}							\
    189 								\
    190 	INLINE unsigned						\
    191 	ARRAY##_num(const struct ARRAY *a)			\
    192 	{							\
    193 		return array_num(&a->arr);			\
    194 	}							\
    195 								\
    196 	INLINE T *						\
    197 	ARRAY##_get(const struct ARRAY *a, unsigned index_)	\
    198 	{				 			\
    199 		return (T *)array_get(&a->arr, index_);		\
    200 	}							\
    201 								\
    202 	INLINE void						\
    203 	ARRAY##_set(struct ARRAY *a, unsigned index_, T *val)	\
    204 	{				 			\
    205 		array_set(&a->arr, index_, (void *)val);	\
    206 	}							\
    207 								\
    208 	INLINE int						\
    209 	ARRAY##_setsize(struct ARRAY *a, unsigned num)		\
    210 	{				 			\
    211 		return array_setsize(&a->arr, num);		\
    212 	}							\
    213 								\
    214 	INLINE int						\
    215 	ARRAY##_add(struct ARRAY *a, T *val, unsigned *ret)	\
    216 	{				 			\
    217 		return array_add(&a->arr, (void *)val, ret);	\
    218 	}							\
    219 								\
    220 	INLINE int						\
    221 	ARRAY##_insert(struct ARRAY *a, unsigned index_)	\
    222 	{				 			\
    223 		return array_insert(&a->arr, index_);		\
    224 	}							\
    225 								\
    226 	INLINE void						\
    227 	ARRAY##_remove(struct ARRAY *a, unsigned index_)	\
    228 	{				 			\
    229 		return array_remove(&a->arr, index_);		\
    230 	}
    231 
    232 #define DECLARRAY(T) DECLARRAY_BYTYPE(T##array, struct T)
    233 #define DEFARRAY(T, INLINE) DEFARRAY_BYTYPE(T##array, struct T, INLINE)
    234 
    235 ////////////////////////////////////////////////////////////
    236 // basic array types
    237 
    238 DECLARRAY_BYTYPE(stringarray, char);
    239 DEFARRAY_BYTYPE(stringarray, char, ARRAYINLINE);
    240 
    241 #endif /* ARRAY_H */
    242