2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Array --- dynamic & flexible arrays.
9 * @author Markus Armbruster
20 /* Undefine the macros to get the functions instead, cf tmalloc.c. */
27 * An empty dynamic array descriptor.
29 ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, 0, { { 0 } } };
31 void ir_verify_arr(const void *arr)
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);
44 * Creates a dynamic array on a obstack.
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.
50 * @return A pointer to the dynamic array (can be used as a pointer to the
51 * first element of this array).
53 * @remark Helper function, use NEW_ARR_D() instead.
55 void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size)
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;
68 * Creates a flexible array.
70 * @param nelts The number of elements
71 * @param elts_size The size of the array elements.
73 * @return A pointer to the flexible array (can be used as a pointer to the
74 * first element of this array).
76 * @remark Helper function, use NEW_ARR_F() instead.
78 void *ir_new_arr_f(size_t nelts, size_t elts_size)
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;
89 * Delete a flexible array.
91 * @param elts The flexible array (pointer to the first element).
93 * @remark Helper function, use DEL_ARR_F() instead.
95 void ir_del_arr_f(void *elts)
97 ir_arr_descr *dp = ARR_DESCR (elts);
100 assert(dp->magic == ARR_F_MAGIC);
103 dp->magic = 0xdeadbeef;
109 * Resize a flexible array, always reallocate data.
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.
115 * @return A resized flexible array, possibly other address than
118 * @remark Helper function, use ARR_SETLEN() instead.
120 void *ir_arr_setlen (void *elts, size_t nelts, size_t elts_size)
122 ir_arr_descr *dp = ARR_DESCR (elts);
124 assert(dp->magic == ARR_F_MAGIC);
127 dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+elts_size);
128 dp->allocated = dp->nelts = nelts;
134 * Resize a flexible array, allocate more data if needed but do NOT
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.
141 * @return A resized flexible array, possibly other address than
144 * @remark Helper function, use ARR_RESIZE() instead.
146 void *ir_arr_resize(void *elts, size_t nelts, size_t eltsize)
148 ir_arr_descr *dp = ARR_DESCR(elts);
151 assert(dp->magic == ARR_F_MAGIC);
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;
160 if (n != dp->allocated) {
161 dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+eltsize*n);
170 /* forward declarations to avoid warnings */
171 size_t array_len(const void *arr);
172 ir_arr_descr *array_descr(const void *arr);
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.
179 size_t array_len(const void *arr)
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.
189 ir_arr_descr *array_descr(const void *arr)
193 return ARR_DESCR(arr);
195 #endif /* DEBUG_libfirm */