bb5f47c7c282f3776448c0ca63f0a3c931c1a571
[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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifdef HAVE_STDLIB_H
32 # include <stdlib.h>
33 #endif
34
35 #include "array_t.h"
36 #include "xmalloc.h"
37
38 /* Undefine the macros to get the functions instead, cf tmalloc.c.  */
39 #undef xmalloc
40 #undef xrealloc
41 #undef xstrdup
42 #undef xfree
43
44 #ifndef MAX
45 # define MAX(a,b) ((a) > (b) ? (a) : (b))
46 #endif
47 #ifndef MIN
48 # define MIN(a,b) ((a) > (b) ? (b) : (a))
49 #endif
50
51 /**
52  * An empty dynamic array descriptor.
53  */
54 ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, {0}, 0, {{{0}}} };
55
56 void ir_verify_arr(const void *arr)
57 {
58 #ifndef NDEBUG
59         ir_arr_descr *descr = ARR_DESCR(arr);
60         assert(descr->magic == ARR_D_MAGIC || descr->magic == ARR_A_MAGIC
61                          || descr->magic == ARR_F_MAGIC);
62         if (descr->magic == ARR_F_MAGIC) {
63                 assert(descr->u.allocated >= descr->nelts);
64         }
65         assert(descr->nelts >= 0);
66 #else
67         (void) arr;
68 #endif
69 }
70
71 /**
72  * Creates a dynamic array on a obstack.
73  *
74  * @param obstack    An struct obstack * were the data will be allocated
75  * @param nelts      The number of elements
76  * @param elts_size  The size of the array elements.
77  *
78  * @return A pointer to the dynamic array (can be used as a pointer to the
79  *         first element of this array).
80  *
81  * @remark Helper function, use NEW_ARR_D() instead.
82  */
83 void *ir_new_arr_d(struct obstack *obstack, int nelts, size_t elts_size) {
84         ir_arr_descr *dp;
85
86         assert(obstack && (nelts >= 0));
87
88         dp = obstack_alloc(obstack, ARR_ELTS_OFFS + elts_size);
89         ARR_SET_DBGINF(dp, ARR_D_MAGIC, elts_size/nelts);
90         dp->u.obstack = obstack;
91         dp->nelts = nelts;
92         return dp->v.elts;
93 }
94
95 /**
96  * Creates a flexible array.
97  *
98  * @param nelts      The number of elements
99  * @param elts_size  The size of the array elements.
100  *
101  * @return A pointer to the flexible array (can be used as a pointer to the
102  *         first element of this array).
103  *
104  * @remark Helper function, use NEW_ARR_F() instead.
105  */
106 void *ir_new_arr_f(int nelts, size_t elts_size) {
107         ir_arr_descr *new;
108
109         assert (nelts >= 0);
110         new = xmalloc (ARR_ELTS_OFFS+elts_size);
111         ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
112         new->u.allocated = new->nelts = nelts;
113         return new->v.elts;
114 }
115
116 /**
117  * Delete a flexible array.
118  *
119  * @param elts    The flexible array (pointer to the first element).
120  *
121  * @remark Helper function, use DEL_ARR_F() instead.
122  */
123 void ir_del_arr_f(void *elts) {
124         ir_arr_descr *dp = ARR_DESCR (elts);
125
126         ARR_VRFY (elts);
127         assert (dp->magic == ARR_F_MAGIC);
128
129 #ifndef NDEBUG
130         {
131                 ir_arr_descr *wdp = (ir_arr_descr *)dp;
132                 wdp->magic = 0xdeadbeef;
133         }
134 #endif
135         free(dp);
136 }
137
138 /**
139  * Resize a flexible array, always reallocate data.
140  *
141  * @param elts       The flexible array (pointer to the first element).
142  * @param nelts      The new number of elements.
143  * @param elts_size  The size of the array elements.
144  *
145  * @return A resized flexible array, possibly other address than
146  *         elts.
147  *
148  * @remark Helper function, use ARR_SETLEN() instead.
149  */
150 void *ir_arr_setlen (void *elts, int nelts, size_t elts_size) {
151         ir_arr_descr *dp = ARR_DESCR (elts);
152
153         assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
154         ARR_VRFY (elts);
155         assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
156
157         dp = xrealloc (dp, ARR_ELTS_OFFS+elts_size);
158         dp->u.allocated = dp->nelts = nelts;
159
160         return dp->v.elts;
161 }
162
163 /**
164  * Resize a flexible array, allocate more data if needed but do NOT
165  * reduce.
166  *
167  * @param elts     The flexible array (pointer to the first element).
168  * @param nelts    The new number of elements.
169  * @param eltsize  The size of the array elements.
170  *
171  * @return A resized flexible array, possibly other address than
172  *         elts.
173  *
174  * @remark Helper function, use ARR_RESIZE() instead.
175  */
176 void *ir_arr_resize(void *elts, int nelts, size_t eltsize) {
177         ir_arr_descr *dp = ARR_DESCR(elts);
178         int n;
179
180         assert((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
181         ARR_VRFY(elts);
182         assert(dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
183
184         /* @@@ lots of resizes for small nelts */
185         n = MAX(1, dp->u.allocated);
186         while (nelts > n) n <<= 1;
187         while (3*nelts < n) n >>= 1;
188         assert(n >= nelts);
189
190         if (n != dp->u.allocated) {
191                 dp = xrealloc(dp, ARR_ELTS_OFFS+eltsize*n);
192                 dp->u.allocated = n;
193 #if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
194         } else {
195                 tmalloc_tag = NULL;
196 #endif
197         }
198         dp->nelts = nelts;
199
200         return dp->v.elts;
201 }
202
203 #ifdef DEBUG_libfirm
204 /**
205  * This function returns the length of a flexible array.
206  * Do NOT use is in code, use ARR_LEN() macro!
207  * This function is intended to be called from a debugger.
208  */
209 int array_len(const void *arr) {
210         return ARR_LEN(arr);
211 }
212
213 /**
214  * This function returns the array descriptor of a flexible array.
215  * Do NOT use is in code!.
216  * This function is intended to be called from a debugger.
217  */
218 ir_arr_descr *array_descr(const void *arr) {
219         if (! arr)
220                 return NULL;
221         return ARR_DESCR(arr);
222 }
223 #endif /* DEBUG_libfirm */