Fixed expansion again (hopefully last time :-)
[libfirm] / ir / adt / array.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/adt/array.h
4  * Purpose:     Declarations for Array.
5  * Author:      Markus Armbruster
6  * Modified by:
7  * Created:     1999 by getting from fiasco
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1995, 1996 Markus Armbruster
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 /* @@@ growing a dynamic on an obstack */
14
15 #ifndef _ARRAY_H
16 #define _ARRAY_H
17
18 #include <assert.h>
19 #include <stddef.h>
20 #include <obstack.h>
21
22 #include "cookies.h"
23 #include "xmalloc.h"
24
25
26 /* Flexible create / delete */
27 #define NEW_ARR_F(type, nelts)                                          \
28   (XMALLOC_TRACE (type *)_new_arr_f ((nelts), sizeof(type) * (nelts)))
29 #define CLONE_ARR_F(type, arr)                  \
30   NEW_ARR_F (type, ARR_LEN ((arr)))
31 #define DUP_ARR_F(type, arr)                                                    \
32   memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
33 #define DEL_ARR_F(arr) (XMALLOC_TRACE _del_arr_f ((arr)))
34
35 /* Dynamic create on obstacks */
36 #define NEW_ARR_D(type, obstack, nelts)                                 \
37   (  nelts                                                              \
38    ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts))    \
39    : (type *)arr_mt_descr.v.elts)
40 #define CLONE_ARR_D(type, obstack, arr)         \
41   NEW_ARR_D (type, (obstack), ARR_LEN ((arr)))
42 #define DUP_ARR_D(type, obstack, arr)                                                   \
43   memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr)))
44
45 /* Automatic create; delete is automatic at return from function */
46 /* Quick'n'dirty! */
47 #define NEW_ARR_A(type, var, n)                                                                 \
48   do {                                                                                          \
49     int _nelts = (n);                                                                           \
50     assert (_nelts >= 0);                                                                       \
51     (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts;    \
52     _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type));                           \
53     (void)(_ARR_DESCR ((var))->nelts = _nelts);                                                 \
54   } while (0)
55 #define CLONE_ARR_A(type, var, arr)             \
56   NEW_ARR_A (type, (var), ARR_LEN ((arr)))
57
58 #define DUP_ARR_A(type, var, arr)                                       \
59   do { CLONE_ARR_A(type, (var), (arr));                                 \
60        memcpy ((var), (arr), sizeof (type) * ARR_LEN ((arr))); }        \
61   while (0)
62
63 /* Declare an initialized array of fixed size */
64 #define DECL_ARR_S(type, var, _nelts)                                   \
65   ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var;                     \
66   type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)),   \
67                _##var.nelts = _nelts,                                   \
68                _##var.v.elts)
69
70 /* Length */
71 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
72
73 /* Resize
74    Applicable to flexibles only, change arr which must be an lvalue.  */
75 /* resize arr to hold n elts */
76 #define ARR_RESIZE(type, arr, n)                                        \
77   (XMALLOC_TRACE (arr) = _arr_resize ((arr), (n), sizeof(type)))
78 /* resize arr to hold exactly n elts */
79 #define ARR_SETLEN(type, arr, n)                                        \
80   (XMALLOC_TRACE (arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
81 /* resize arr by delta elts */
82 #define ARR_EXTEND(type, arr, delta)                    \
83   ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta))
84 /* resize arr to hold n elts only if it is currently shorter */
85 #define ARR_EXTO(type, arr, n)                                          \
86   ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr))
87 /* append one elt to arr */
88 #define ARR_APP1(type, arr, elt)                                        \
89   (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
90
91
92 #ifdef NDEBUG
93 # define ARR_VRFY(arr) ((void)0)
94 # define ARR_IDX_VRFY(arr, idx) ((void)0)
95 #else
96 # define ARR_VRFY(arr)                                                                  \
97     assert (   (   (_ARR_DESCR((arr))->cookie == ARR_D_MAGIC)                           \
98                 || (_ARR_DESCR((arr))->cookie == ARR_A_MAGIC)                           \
99                 || (_ARR_DESCR((arr))->cookie == ARR_F_MAGIC))                          \
100             && (   (_ARR_DESCR((arr))->cookie != ARR_F_MAGIC)                           \
101                 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts))        \
102             && (_ARR_DESCR((arr))->nelts >= 0))
103 # define ARR_IDX_VRFY(arr, idx)                         \
104     assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
105 #endif
106
107
108 /* Private !!!
109    Don't try this at home, kids, we're trained professionals ;->
110    ... or at the IPD, either. */
111 #ifdef NDEBUG
112 # define _ARR_DBGINF_DECL
113 # define _ARR_SET_DBGINF(descr, co, es) ((co), (es))
114 #else
115 # define _ARR_DBGINF_DECL int cookie; size_t eltsize;
116 # define _ARR_SET_DBGINF(descr, co, es)                                 \
117     ((descr)->cookie = (co), (descr)->eltsize = (es))
118 #endif
119
120 #define ARR_STRUCT(type, _nelts)                                                \
121   struct {                                                                      \
122     _ARR_DBGINF_DECL                                                            \
123     union {                                                                     \
124       struct obstack *obstack;  /* dynamic: allocated on this obstack */        \
125       int allocated;                    /* flexible: #slots allocated */        \
126     } u;                                                                        \
127     int nelts;                                                                  \
128     union {                                                                     \
129       type elts[(_nelts)];                                                      \
130       aligned_type align[1];                                                    \
131     } v;                                                                        \
132   }
133
134 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
135
136 extern _arr_descr arr_mt_descr;
137
138 void *_new_arr_f (int, size_t);
139 void _del_arr_f (void *);
140 void *_new_arr_d (struct obstack *, int, size_t);
141 void *_arr_resize (void *, int, size_t);
142 void *_arr_setlen (void *, int, size_t);
143
144 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
145 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))
146
147 #endif