remove #ifdef HAVE_CONFIG_Hs
[libfirm] / ir / adt / array.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Array --- dynamic & flexible arrays.
23  * @author      Markus Armbruster
24  * @version     $Id$
25  */
26
27 #include "config.h"
28
29 #ifdef HAVE_STDLIB_H
30 # include <stdlib.h>
31 #endif
32
33 #include "array_t.h"
34 #include "xmalloc.h"
35
36 /* Undefine the macros to get the functions instead, cf tmalloc.c.  */
37 #undef xmalloc
38 #undef xrealloc
39 #undef xstrdup
40 #undef xfree
41
42 #ifndef MAX
43 # define MAX(a,b) ((a) > (b) ? (a) : (b))
44 #endif
45 #ifndef MIN
46 # define MIN(a,b) ((a) > (b) ? (b) : (a))
47 #endif
48
49 /**
50  * An empty dynamic array descriptor.
51  */
52 ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, {0}, 0, {{{0}}} };
53
54 void ir_verify_arr(const void *arr)
55 {
56 #ifndef NDEBUG
57         ir_arr_descr *descr = ARR_DESCR(arr);
58         assert(descr->magic == ARR_D_MAGIC || descr->magic == ARR_A_MAGIC
59                          || descr->magic == ARR_F_MAGIC);
60         if (descr->magic == ARR_F_MAGIC) {
61                 assert(descr->u.allocated >= descr->nelts);
62         }
63         assert(descr->nelts >= 0);
64 #else
65         (void) arr;
66 #endif
67 }
68
69 /**
70  * Creates a dynamic array on a obstack.
71  *
72  * @param obstack    An struct obstack * were the data will be allocated
73  * @param nelts      The number of elements
74  * @param elts_size  The size of the array elements.
75  *
76  * @return A pointer to the dynamic array (can be used as a pointer to the
77  *         first element of this array).
78  *
79  * @remark Helper function, use NEW_ARR_D() instead.
80  */
81 void *ir_new_arr_d(struct obstack *obstack, int nelts, size_t elts_size) {
82         ir_arr_descr *dp;
83
84         assert(obstack && (nelts >= 0));
85
86         dp = obstack_alloc(obstack, ARR_ELTS_OFFS + elts_size);
87         ARR_SET_DBGINF(dp, ARR_D_MAGIC, elts_size/nelts);
88         dp->u.obstack = obstack;
89         dp->nelts = nelts;
90         return dp->v.elts;
91 }
92
93 /**
94  * Creates a flexible array.
95  *
96  * @param nelts      The number of elements
97  * @param elts_size  The size of the array elements.
98  *
99  * @return A pointer to the flexible array (can be used as a pointer to the
100  *         first element of this array).
101  *
102  * @remark Helper function, use NEW_ARR_F() instead.
103  */
104 void *ir_new_arr_f(int nelts, size_t elts_size) {
105         ir_arr_descr *new;
106
107         assert (nelts >= 0);
108         new = xmalloc (ARR_ELTS_OFFS+elts_size);
109         ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
110         new->u.allocated = new->nelts = nelts;
111         return new->v.elts;
112 }
113
114 /**
115  * Delete a flexible array.
116  *
117  * @param elts    The flexible array (pointer to the first element).
118  *
119  * @remark Helper function, use DEL_ARR_F() instead.
120  */
121 void ir_del_arr_f(void *elts) {
122         ir_arr_descr *dp = ARR_DESCR (elts);
123
124         ARR_VRFY (elts);
125         assert (dp->magic == ARR_F_MAGIC);
126
127 #ifndef NDEBUG
128         {
129                 ir_arr_descr *wdp = (ir_arr_descr *)dp;
130                 wdp->magic = 0xdeadbeef;
131         }
132 #endif
133         free(dp);
134 }
135
136 /**
137  * Resize a flexible array, always reallocate data.
138  *
139  * @param elts       The flexible array (pointer to the first element).
140  * @param nelts      The new number of elements.
141  * @param elts_size  The size of the array elements.
142  *
143  * @return A resized flexible array, possibly other address than
144  *         elts.
145  *
146  * @remark Helper function, use ARR_SETLEN() instead.
147  */
148 void *ir_arr_setlen (void *elts, int nelts, size_t elts_size) {
149         ir_arr_descr *dp = ARR_DESCR (elts);
150
151         assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
152         ARR_VRFY (elts);
153         assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
154
155         dp = xrealloc (dp, ARR_ELTS_OFFS+elts_size);
156         dp->u.allocated = dp->nelts = nelts;
157
158         return dp->v.elts;
159 }
160
161 /**
162  * Resize a flexible array, allocate more data if needed but do NOT
163  * reduce.
164  *
165  * @param elts     The flexible array (pointer to the first element).
166  * @param nelts    The new number of elements.
167  * @param eltsize  The size of the array elements.
168  *
169  * @return A resized flexible array, possibly other address than
170  *         elts.
171  *
172  * @remark Helper function, use ARR_RESIZE() instead.
173  */
174 void *ir_arr_resize(void *elts, int nelts, size_t eltsize) {
175         ir_arr_descr *dp = ARR_DESCR(elts);
176         int n;
177
178         assert((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
179         ARR_VRFY(elts);
180         assert(dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
181
182         /* @@@ lots of resizes for small nelts */
183         n = MAX(1, dp->u.allocated);
184         while (nelts > n) n <<= 1;
185         while (3*nelts < n) n >>= 1;
186         assert(n >= nelts);
187
188         if (n != dp->u.allocated) {
189                 dp = xrealloc(dp, ARR_ELTS_OFFS+eltsize*n);
190                 dp->u.allocated = n;
191 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
192         } else {
193                 tmalloc_tag = NULL;
194 #endif
195         }
196         dp->nelts = nelts;
197
198         return dp->v.elts;
199 }
200
201 #ifdef DEBUG_libfirm
202 /**
203  * This function returns the length of a flexible array.
204  * Do NOT use is in code, use ARR_LEN() macro!
205  * This function is intended to be called from a debugger.
206  */
207 int array_len(const void *arr) {
208         return ARR_LEN(arr);
209 }
210
211 /**
212  * This function returns the array descriptor of a flexible array.
213  * Do NOT use is in code!.
214  * This function is intended to be called from a debugger.
215  */
216 ir_arr_descr *array_descr(const void *arr) {
217         if (! arr)
218                 return NULL;
219         return ARR_DESCR(arr);
220 }
221 #endif /* DEBUG_libfirm */