Module Name: othersrc
Committed By: dholland
Date: Mon Mar 4 06:45:23 UTC 2013
Modified Files:
othersrc/usr.bin/dholland-make2: array.h
Log Message:
Improve inline handling. This will probably need more improving as I think
it needs to support a no inlining at all mode for the tools build.
To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 othersrc/usr.bin/dholland-make2/array.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: othersrc/usr.bin/dholland-make2/array.h
diff -u othersrc/usr.bin/dholland-make2/array.h:1.1 othersrc/usr.bin/dholland-make2/array.h:1.2
--- othersrc/usr.bin/dholland-make2/array.h:1.1 Mon Mar 4 05:47:15 2013
+++ othersrc/usr.bin/dholland-make2/array.h Mon Mar 4 06:45:23 2013
@@ -39,6 +39,10 @@
#define arrayassert(x) ((void)(x))
#endif
+#ifndef ARRAYINLINE
+#define ARRAYINLINE MAKE_INLINE
+#endif
+
////////////////////////////////////////////////////////////
// type and base operations
@@ -51,21 +55,17 @@ struct array *array_create(void);
void array_destroy(struct array *);
void array_init(struct array *);
void array_cleanup(struct array *);
-unsigned array_num(const struct array *);
-void *array_get(const struct array *, unsigned index_);
-void array_set(const struct array *, unsigned index_, void *val);
+ARRAYINLINE unsigned array_num(const struct array *);
+ARRAYINLINE void *array_get(const struct array *, unsigned index_);
+ARRAYINLINE void array_set(const struct array *, unsigned index_, void *val);
int array_setsize(struct array *, unsigned num);
-int array_add(struct array *, void *val, unsigned *index_ret);
+ARRAYINLINE int array_add(struct array *, void *val, unsigned *index_ret);
int array_insert(struct array *a, unsigned index_);
void array_remove(struct array *a, unsigned index_);
////////////////////////////////////////////////////////////
// inlining for base operations
-#ifndef ARRAYINLINE
-#define ARRAYINLINE MAKE_INLINE
-#endif
-
ARRAYINLINE unsigned
array_num(const struct array *a)
{
@@ -106,50 +106,77 @@ array_add(struct array *a, void *val, un
/*
* Usage:
*
- * DECLARRAY_BYTYPE(foo, bar) declares "struct foo", which is
+ * DECLARRAY_BYTYPE(foo, bar, INLINE) declares "struct foo", which is
* an array of pointers to "bar", plus the operations on it.
*
- * DECLARRAY(foo) is equivalent to DECLARRAY_BYTYPE(fooarray, struct foo).
+ * DECLARRAY(foo, INLINE) is equivalent to
+ * DECLARRAY_BYTYPE(fooarray, struct foo, INLINE).
*
* DEFARRAY_BYTYPE and DEFARRAY are the same as DECLARRAY except that
- * they define the operations, and both take an extra argument INLINE.
- * For C99 this should be INLINE in header files and empty in the
- * master source file, the same as the usage of ARRAYINLINE above and
- * in array.c.
- *
- * Example usage in e.g. item.h of some game:
- *
- * DECLARRAY_BYTYPE(stringarray, char);
- * DECLARRAY(potion);
- * DECLARRAY(sword);
- *
- * #ifndef ITEMINLINE
- * #define ITEMINLINE INLINE
- * #endif
- *
- * DEFARRAY_BYTYPE(stringarray, char, ITEMINLINE);
- * DEFARRAY(potion, ITEMINLINE);
- * DEFARRAY(sword, ITEMINLINE);
+ * they define the operations.
+ *
+ * The argument INLINE can be used as follows:
*
- * Then item.c would do "#define ITEMINLINE" before including item.h.
+ * 1. For no inlining:
+ * In foo.h:
+ * DECLARRAY(foo, );
+ * In foo.c:
+ * DEFARRAY(foo, );
+ *
+ * 2. To be file-static:
+ * In foo.c:
+ * DECLARRAY(foo, static);
+ * DEFARRAY(foo, static);
+ *
+ * 3. To inline using C99:
+ * In foo.h:
+ * DECLARRAY(foo, inline);
+ * DEFARRAY(foo, inline);
+ *
+ * 4. To inline with old gcc:
+ * In foo.h:
+ * #ifndef FOO_INLINE
+ * #define FOO_INLINE extern inline
+ * #endif
+ * DECLARRAY(foo, );
+ * DEFARRAY(foo, FOO_INLINE);
+ * In foo.c:
+ * #define FOO_INLINE
+ * #include "foo.h"
+ *
+ * 5. To inline such that it works both with old gcc and C99:
+ * In foo.h:
+ * #ifndef FOO_INLINE
+ * #define FOO_INLINE extern inline
+ * #endif
+ * DECLARRAY(foo, FOO_INLINE);
+ * DEFARRAY(foo, FOO_INLINE);
+ * In foo.c:
+ * #define FOO_INLINE
+ * #include "foo.h"
+ *
+ * The mechanism in case (4) ensures that an externally linkable
+ * definition exists. There appears to be no need for this in C99;
+ * however, providing inline definitions does not work unless the
+ * declaration also contains inline.
*/
-#define DECLARRAY_BYTYPE(ARRAY, T) \
- struct ARRAY { \
- struct array arr; \
- }; \
- \
- struct ARRAY *ARRAY##_create(void); \
- void ARRAY##_destroy(struct ARRAY *a); \
- void ARRAY##_init(struct ARRAY *a); \
- void ARRAY##_cleanup(struct ARRAY *a); \
- unsigned ARRAY##_num(const struct ARRAY *a); \
- T *ARRAY##_get(const struct ARRAY *a, unsigned index_); \
- void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \
- int ARRAY##_setsize(struct ARRAY *a, unsigned num); \
- int ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \
- int ARRAY##_insert(struct ARRAY *a, unsigned index_); \
- void ARRAY##_remove(struct ARRAY *a, unsigned index_)
+#define DECLARRAY_BYTYPE(ARRAY, T, INLINE) \
+ struct ARRAY { \
+ struct array arr; \
+ }; \
+ \
+ INLINE struct ARRAY *ARRAY##_create(void); \
+ INLINE void ARRAY##_destroy(struct ARRAY *a); \
+ INLINE void ARRAY##_init(struct ARRAY *a); \
+ INLINE void ARRAY##_cleanup(struct ARRAY *a); \
+ INLINE unsigned ARRAY##_num(const struct ARRAY *a); \
+ INLINE T *ARRAY##_get(const struct ARRAY *a, unsigned index_); \
+ INLINE void ARRAY##_set(struct ARRAY *a, unsigned index_, T *val); \
+ INLINE int ARRAY##_setsize(struct ARRAY *a, unsigned num); \
+ INLINE int ARRAY##_add(struct ARRAY *a, T *val, unsigned *index_ret); \
+ INLINE int ARRAY##_insert(struct ARRAY *a, unsigned index_); \
+ INLINE void ARRAY##_remove(struct ARRAY *a, unsigned index_)
#define DEFARRAY_BYTYPE(ARRAY, T, INLINE) \
@@ -233,7 +260,7 @@ array_add(struct array *a, void *val, un
////////////////////////////////////////////////////////////
// basic array types
-DECLARRAY_BYTYPE(stringarray, char);
+DECLARRAY_BYTYPE(stringarray, char, ARRAYINLINE);
DEFARRAY_BYTYPE(stringarray, char, ARRAYINLINE);
#endif /* ARRAY_H */