/*
- * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
- *
* This file is part of libFirm.
- *
- * This file may be distributed and/or modified under the terms of the
- * GNU General Public License version 2 as published by the Free Software
- * Foundation and appearing in the file LICENSE.GPL included in the
- * packaging of this file.
- *
- * Licensees holding valid libFirm Professional Edition licenses may use
- * this file in accordance with the libFirm Commercial License.
- * Agreement provided with the Software.
- *
- * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
- * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE.
+ * Copyright (C) 2012 University of Karlsruhe.
*/
/**
* @file
* @brief Array --- dynamic & flexible arrays.
* @author Markus Armbruster
- * @version $Id$
*/
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
-#ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
+#include <stdlib.h>
#include "array_t.h"
+#include "util.h"
#include "xmalloc.h"
/* Undefine the macros to get the functions instead, cf tmalloc.c. */
#undef xstrdup
#undef xfree
-#ifndef MAX
-# define MAX(a,b) ((a) > (b) ? (a) : (b))
-#endif
-#ifndef MIN
-# define MIN(a,b) ((a) > (b) ? (b) : (a))
-#endif
-
/**
* An empty dynamic array descriptor.
*/
-ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, {0}, 0, {{{0}}} };
+ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, 0, { { 0 } } };
void ir_verify_arr(const void *arr)
{
ir_arr_descr *descr = ARR_DESCR(arr);
assert(descr->magic == ARR_D_MAGIC || descr->magic == ARR_A_MAGIC
|| descr->magic == ARR_F_MAGIC);
- if (descr->magic == ARR_F_MAGIC) {
- assert(descr->u.allocated >= descr->nelts);
- }
- assert(descr->nelts >= 0);
+ assert(descr->magic != ARR_F_MAGIC || descr->allocated >= descr->nelts);
#else
(void) arr;
#endif
*
* @remark Helper function, use NEW_ARR_D() instead.
*/
-void *ir_new_arr_d(struct obstack *obstack, int nelts, size_t elts_size) {
+void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size)
+{
ir_arr_descr *dp;
- assert(obstack && (nelts >= 0));
+ assert(obstack);
- dp = obstack_alloc(obstack, ARR_ELTS_OFFS + elts_size);
- ARR_SET_DBGINF(dp, ARR_D_MAGIC, elts_size/nelts);
- dp->u.obstack = obstack;
- dp->nelts = nelts;
- return dp->v.elts;
+ dp = (ir_arr_descr*)obstack_alloc(obstack, ARR_ELTS_OFFS + elts_size);
+ ARR_SET_DBGINF(dp, ARR_D_MAGIC);
+ dp->allocated = dp->nelts = nelts;
+ return dp->elts;
}
/**
*
* @remark Helper function, use NEW_ARR_F() instead.
*/
-void *ir_new_arr_f(int nelts, size_t elts_size) {
- ir_arr_descr *new;
-
- assert (nelts >= 0);
- new = xmalloc (ARR_ELTS_OFFS+elts_size);
- ARR_SET_DBGINF (new, ARR_F_MAGIC, nelts ? elts_size/nelts : 0);
- new->u.allocated = new->nelts = nelts;
- return new->v.elts;
+void *ir_new_arr_f(size_t nelts, size_t elts_size)
+{
+ ir_arr_descr *newa;
+
+ newa = (ir_arr_descr*)xmalloc(ARR_ELTS_OFFS+elts_size);
+ ARR_SET_DBGINF(newa, ARR_F_MAGIC);
+ newa->allocated = newa->nelts = nelts;
+ return newa->elts;
}
/**
*
* @remark Helper function, use DEL_ARR_F() instead.
*/
-void ir_del_arr_f(void *elts) {
+void ir_del_arr_f(void *elts)
+{
ir_arr_descr *dp = ARR_DESCR (elts);
- ARR_VRFY (elts);
- assert (dp->magic == ARR_F_MAGIC);
+ ARR_VRFY(elts);
+ assert(dp->magic == ARR_F_MAGIC);
#ifndef NDEBUG
- {
- ir_arr_descr *wdp = (ir_arr_descr *)dp;
- wdp->magic = 0xdeadbeef;
- }
+ dp->magic = 0xdeadbeef;
#endif
free(dp);
}
*
* @remark Helper function, use ARR_SETLEN() instead.
*/
-void *ir_arr_setlen (void *elts, int nelts, size_t elts_size) {
+void *ir_arr_setlen (void *elts, size_t nelts, size_t elts_size)
+{
ir_arr_descr *dp = ARR_DESCR (elts);
- assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
- ARR_VRFY (elts);
- assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts));
+ assert(dp->magic == ARR_F_MAGIC);
+ ARR_VRFY(elts);
- dp = xrealloc (dp, ARR_ELTS_OFFS+elts_size);
- dp->u.allocated = dp->nelts = nelts;
+ dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+elts_size);
+ dp->allocated = dp->nelts = nelts;
- return dp->v.elts;
+ return dp->elts;
}
/**
*
* @remark Helper function, use ARR_RESIZE() instead.
*/
-void *ir_arr_resize(void *elts, int nelts, size_t eltsize) {
+void *ir_arr_resize(void *elts, size_t nelts, size_t eltsize)
+{
ir_arr_descr *dp = ARR_DESCR(elts);
- int n;
+ size_t n;
- assert((dp->magic == ARR_F_MAGIC) && (nelts >= 0));
+ assert(dp->magic == ARR_F_MAGIC);
ARR_VRFY(elts);
- assert(dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1));
/* @@@ lots of resizes for small nelts */
- n = MAX(1, dp->u.allocated);
+ n = MAX(1, dp->allocated);
while (nelts > n) n <<= 1;
while (3*nelts < n) n >>= 1;
assert(n >= nelts);
- if (n != dp->u.allocated) {
- dp = xrealloc(dp, ARR_ELTS_OFFS+eltsize*n);
- dp->u.allocated = n;
-#if defined(DEBUG) && defined(HAVE_GNU_MALLOC)
- } else {
- tmalloc_tag = NULL;
-#endif
+ if (n != dp->allocated) {
+ dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+eltsize*n);
+ dp->allocated = n;
}
dp->nelts = nelts;
- return dp->v.elts;
+ return dp->elts;
}
#ifdef DEBUG_libfirm
+/* forward declarations to avoid warnings */
+size_t array_len(const void *arr);
+ir_arr_descr *array_descr(const void *arr);
+
/**
* This function returns the length of a flexible array.
* Do NOT use is in code, use ARR_LEN() macro!
* This function is intended to be called from a debugger.
*/
-int array_len(const void *arr) {
+size_t array_len(const void *arr)
+{
return ARR_LEN(arr);
}
* Do NOT use is in code!.
* This function is intended to be called from a debugger.
*/
-ir_arr_descr *array_descr(const void *arr) {
+ir_arr_descr *array_descr(const void *arr)
+{
if (! arr)
return NULL;
return ARR_DESCR(arr);