1 /* Declarations for Array.
2 Copyright (C) 1995, 1996 Markus Armbruster
3 All rights reserved. */
7 /* @@@ growing a dynamic on an obstack */
19 /* Flexible create / delete */
20 #define NEW_ARR_F(type, nelts) \
21 (XMALLOC_TRACE (type *)_new_arr_f ((nelts), sizeof(type) * (nelts)))
22 #define CLONE_ARR_F(type, arr) \
23 NEW_ARR_F (type, ARR_LEN ((arr)))
24 #define DUP_ARR_F(type, arr) \
25 memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr)))
26 #define DEL_ARR_F(arr) (XMALLOC_TRACE _del_arr_f ((arr)))
28 /* Dynamic create on obstacks */
29 #define NEW_ARR_D(type, obstack, nelts) \
31 ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts)) \
32 : (type *)arr_mt_descr.v.elts)
33 #define CLONE_ARR_D(type, obstack, arr) \
34 NEW_ARR_D (type, (obstack), ARR_LEN ((arr)))
35 #define DUP_ARR_D(type, obstack, arr) \
36 memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr)))
38 /* Automatic create; delete is automatic at return from function */
40 #define NEW_ARR_A(type, var, n) \
43 assert (_nelts >= 0); \
44 (var) = (void *)((_arr_descr *)alloca (_ARR_ELTS_OFFS + sizeof(type) * _nelts))->v.elts; \
45 _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \
46 (void)(_ARR_DESCR ((var))->nelts = _nelts); \
48 #define CLONE_ARR_A(type, var, arr) \
49 NEW_ARR_A (type, (var), ARR_LEN ((arr)))
51 #define DUP_ARR_A(type, var, arr) \
52 do { CLONE_ARR_A(type, (var), (arr)); \
53 memcpy ((var), (arr), sizeof (type) * ARR_LEN ((arr))); } \
56 /* Declare an initialized array of fixed size */
57 #define DECL_ARR_S(type, var, _nelts) \
58 ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var; \
59 type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)), \
60 _##var.nelts = _nelts, \
64 #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts)
67 Applicable to flexibles only, change arr which must be an lvalue. */
68 /* resize arr to hold n elts */
69 #define ARR_RESIZE(type, arr, n) \
70 (XMALLOC_TRACE (arr) = _arr_resize ((arr), (n), sizeof(type)))
71 /* resize arr to hold exactly n elts */
72 #define ARR_SETLEN(type, arr, n) \
73 (XMALLOC_TRACE (arr) = _arr_setlen ((arr), (n), sizeof(type) * (n)))
74 /* resize arr by delta elts */
75 #define ARR_EXTEND(type, arr, delta) \
76 ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta))
77 /* resize arr to hold n elts only if it is currently shorter */
78 #define ARR_EXTO(type, arr, n) \
79 ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr))
80 /* append one elt to arr */
81 #define ARR_APP1(type, arr, elt) \
82 (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt))
86 # define ARR_VRFY(arr) ((void)0)
87 # define ARR_IDX_VRFY(arr, idx) ((void)0)
89 # define ARR_VRFY(arr) \
90 assert ( ( (_ARR_DESCR((arr))->cookie == ARR_D_MAGIC) \
91 || (_ARR_DESCR((arr))->cookie == ARR_A_MAGIC) \
92 || (_ARR_DESCR((arr))->cookie == ARR_F_MAGIC)) \
93 && ( (_ARR_DESCR((arr))->cookie != ARR_F_MAGIC) \
94 || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts)) \
95 && (_ARR_DESCR((arr))->nelts >= 0))
96 # define ARR_IDX_VRFY(arr, idx) \
97 assert ((0 <= (idx)) && ((idx) < ARR_LEN ((arr))))
102 Don't try this at home, kids, we're trained professionals ;->
103 ... or at the IPD, either. */
105 # define _ARR_DBGINF_DECL
106 # define _ARR_SET_DBGINF(descr, co, es) ((co), (es))
108 # define _ARR_DBGINF_DECL int cookie; size_t eltsize;
109 # define _ARR_SET_DBGINF(descr, co, es) \
110 ((descr)->cookie = (co), (descr)->eltsize = (es))
113 #define ARR_STRUCT(type, _nelts) \
117 struct obstack *obstack; /* dynamic: allocated on this obstack */ \
118 int allocated; /* flexible: #slots allocated */ \
122 type elts[(_nelts)]; \
123 aligned_type align[1]; \
127 typedef ARR_STRUCT (aligned_type, 1) _arr_descr;
129 extern _arr_descr arr_mt_descr;
131 void *_new_arr_f (int, size_t);
132 void _del_arr_f (void *);
133 void *_new_arr_d (struct obstack *, int, size_t);
134 void *_arr_resize (void *, int, size_t);
135 void *_arr_setlen (void *, int, size_t);
137 #define _ARR_ELTS_OFFS offsetof (_arr_descr, v.elts)
138 #define _ARR_DESCR(elts) ((_arr_descr *)(void *)((char *)(elts) - _ARR_ELTS_OFFS))