beifg: Factorise code to count interference components.
[libfirm] / ir / adt / array.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Array --- dynamic & flexible arrays.
9  * @author      Markus Armbruster
10  */
11
12 #include "config.h"
13
14 #include <stdlib.h>
15
16 #include "array_t.h"
17 #include "util.h"
18 #include "xmalloc.h"
19
20 /* Undefine the macros to get the functions instead, cf tmalloc.c.  */
21 #undef xmalloc
22 #undef xrealloc
23 #undef xstrdup
24 #undef xfree
25
26 /**
27  * An empty dynamic array descriptor.
28  */
29 ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, 0, { { 0 } } };
30
31 void ir_verify_arr(const void *arr)
32 {
33 #ifndef NDEBUG
34         ir_arr_descr *descr = ARR_DESCR(arr);
35         assert(descr->magic == ARR_D_MAGIC || descr->magic == ARR_A_MAGIC
36                          || descr->magic == ARR_F_MAGIC);
37         assert(descr->magic != ARR_F_MAGIC || descr->allocated >= descr->nelts);
38 #else
39         (void) arr;
40 #endif
41 }
42
43 /**
44  * Creates a dynamic array on a obstack.
45  *
46  * @param obstack    An struct obstack * were the data will be allocated
47  * @param nelts      The number of elements
48  * @param elts_size  The size of the array elements.
49  *
50  * @return A pointer to the dynamic array (can be used as a pointer to the
51  *         first element of this array).
52  *
53  * @remark Helper function, use NEW_ARR_D() instead.
54  */
55 void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size)
56 {
57         ir_arr_descr *dp;
58
59         assert(obstack);
60
61         dp = (ir_arr_descr*)obstack_alloc(obstack, ARR_ELTS_OFFS + elts_size);
62         ARR_SET_DBGINF(dp, ARR_D_MAGIC);
63         dp->allocated = dp->nelts = nelts;
64         return dp->elts;
65 }
66
67 /**
68  * Creates a flexible array.
69  *
70  * @param nelts      The number of elements
71  * @param elts_size  The size of the array elements.
72  *
73  * @return A pointer to the flexible array (can be used as a pointer to the
74  *         first element of this array).
75  *
76  * @remark Helper function, use NEW_ARR_F() instead.
77  */
78 void *ir_new_arr_f(size_t nelts, size_t elts_size)
79 {
80         ir_arr_descr *newa;
81
82         newa = (ir_arr_descr*)xmalloc(ARR_ELTS_OFFS+elts_size);
83         ARR_SET_DBGINF(newa, ARR_F_MAGIC);
84         newa->allocated = newa->nelts = nelts;
85         return newa->elts;
86 }
87
88 /**
89  * Delete a flexible array.
90  *
91  * @param elts    The flexible array (pointer to the first element).
92  *
93  * @remark Helper function, use DEL_ARR_F() instead.
94  */
95 void ir_del_arr_f(void *elts)
96 {
97         ir_arr_descr *dp = ARR_DESCR (elts);
98
99         ARR_VRFY(elts);
100         assert(dp->magic == ARR_F_MAGIC);
101
102 #ifndef NDEBUG
103         dp->magic = 0xdeadbeef;
104 #endif
105         free(dp);
106 }
107
108 /**
109  * Resize a flexible array, always reallocate data.
110  *
111  * @param elts       The flexible array (pointer to the first element).
112  * @param nelts      The new number of elements.
113  * @param elts_size  The size of the array elements.
114  *
115  * @return A resized flexible array, possibly other address than
116  *         elts.
117  *
118  * @remark Helper function, use ARR_SETLEN() instead.
119  */
120 void *ir_arr_setlen (void *elts, size_t nelts, size_t elts_size)
121 {
122         ir_arr_descr *dp = ARR_DESCR (elts);
123
124         assert(dp->magic == ARR_F_MAGIC);
125         ARR_VRFY(elts);
126
127         dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+elts_size);
128         dp->allocated = dp->nelts = nelts;
129
130         return dp->elts;
131 }
132
133 /**
134  * Resize a flexible array, allocate more data if needed but do NOT
135  * reduce.
136  *
137  * @param elts     The flexible array (pointer to the first element).
138  * @param nelts    The new number of elements.
139  * @param eltsize  The size of the array elements.
140  *
141  * @return A resized flexible array, possibly other address than
142  *         elts.
143  *
144  * @remark Helper function, use ARR_RESIZE() instead.
145  */
146 void *ir_arr_resize(void *elts, size_t nelts, size_t eltsize)
147 {
148         ir_arr_descr *dp = ARR_DESCR(elts);
149         size_t n;
150
151         assert(dp->magic == ARR_F_MAGIC);
152         ARR_VRFY(elts);
153
154         /* @@@ lots of resizes for small nelts */
155         n = MAX(1, dp->allocated);
156         while (nelts > n) n <<= 1;
157         while (3*nelts < n) n >>= 1;
158         assert(n >= nelts);
159
160         if (n != dp->allocated) {
161                 dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+eltsize*n);
162                 dp->allocated = n;
163         }
164         dp->nelts = nelts;
165
166         return dp->elts;
167 }
168
169 #ifdef DEBUG_libfirm
170 /* forward declarations to avoid warnings */
171 size_t array_len(const void *arr);
172 ir_arr_descr *array_descr(const void *arr);
173
174 /**
175  * This function returns the length of a flexible array.
176  * Do NOT use is in code, use ARR_LEN() macro!
177  * This function is intended to be called from a debugger.
178  */
179 size_t array_len(const void *arr)
180 {
181         return ARR_LEN(arr);
182 }
183
184 /**
185  * This function returns the array descriptor of a flexible array.
186  * Do NOT use is in code!.
187  * This function is intended to be called from a debugger.
188  */
189 ir_arr_descr *array_descr(const void *arr)
190 {
191         if (! arr)
192                 return NULL;
193         return ARR_DESCR(arr);
194 }
195 #endif /* DEBUG_libfirm */