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