X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fadt%2Farray.c;h=d84007e4282a979e4bce18b0086fe3d015398043;hb=26b9008643da89a023fd6839ded5b9abf21ef328;hp=bdcd518d832fec6cbc4e49916efb74b1ab2b9591;hpb=57d365a67aaef76497b85e9a18b785c562652b16;p=libfirm diff --git a/ir/adt/array.c b/ir/adt/array.c index bdcd518d8..d84007e42 100644 --- a/ir/adt/array.c +++ b/ir/adt/array.c @@ -1,5 +1,5 @@ /* - * Copyrigth (C) 1995-2007 University of Karlsruhe. All right reserved. + * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved. * * This file is part of libFirm. * @@ -21,18 +21,13 @@ * @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 -#endif +#include -#include "array.h" +#include "array_t.h" #include "xmalloc.h" /* Undefine the macros to get the functions instead, cf tmalloc.c. */ @@ -49,13 +44,21 @@ #endif /** - * An empty dynamic array + * An empty dynamic array descriptor. */ -_arr_descr arr_mt_descr +ir_arr_descr arr_mt_descr = { ARR_D_MAGIC, 0, { 0 }, 0, { { 0 } } }; + +void ir_verify_arr(const void *arr) +{ #ifndef NDEBUG - = { ARR_D_MAGIC } + ir_arr_descr *descr = ARR_DESCR(arr); + assert(descr->magic == ARR_D_MAGIC || descr->magic == ARR_A_MAGIC + || descr->magic == ARR_F_MAGIC); + assert(descr->magic != ARR_F_MAGIC || descr->u.allocated >= descr->nelts); +#else + (void) arr; #endif -; +} /** * Creates a dynamic array on a obstack. @@ -69,18 +72,17 @@ _arr_descr arr_mt_descr * * @remark Helper function, use NEW_ARR_D() instead. */ -void * -_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) { - _arr_descr *new; + ir_arr_descr *dp; - assert (obstack && (nelts >= 0)); + assert(obstack); - new = obstack_alloc (obstack, _ARR_ELTS_OFFS+elts_size); - _ARR_SET_DBGINF (new, ARR_D_MAGIC, elts_size/nelts); - new->u.obstack = obstack; - new->nelts = nelts; - return new->v.elts; + dp = (ir_arr_descr*)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->elts; } /** @@ -94,16 +96,14 @@ _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size) * * @remark Helper function, use NEW_ARR_F() instead. */ -void * -_new_arr_f (int nelts, size_t elts_size) +void *ir_new_arr_f(size_t nelts, size_t elts_size) { - _arr_descr *new; + ir_arr_descr *newa; - 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; + newa = (ir_arr_descr*)xmalloc(ARR_ELTS_OFFS+elts_size); + ARR_SET_DBGINF(newa, ARR_F_MAGIC, nelts ? elts_size/nelts : 0); + newa->u.allocated = newa->nelts = nelts; + return newa->elts; } /** @@ -113,18 +113,17 @@ _new_arr_f (int nelts, size_t elts_size) * * @remark Helper function, use DEL_ARR_F() instead. */ -void -_del_arr_f (void *elts) +void ir_del_arr_f(void *elts) { - _arr_descr *dp = _ARR_DESCR (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 - dp->magic = 0xdeadbeef; + dp->magic = 0xdeadbeef; #endif - free (dp); + free(dp); } /** @@ -139,19 +138,18 @@ _del_arr_f (void *elts) * * @remark Helper function, use ARR_SETLEN() instead. */ -void * -_arr_setlen (void *elts, int nelts, size_t elts_size) +void *ir_arr_setlen (void *elts, size_t nelts, size_t elts_size) { - _arr_descr *dp = _ARR_DESCR (elts); + 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); + assert(!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts)); - 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->u.allocated = dp->nelts = nelts; - return dp->v.elts; + return dp->elts; } /** @@ -167,43 +165,43 @@ _arr_setlen (void *elts, int nelts, size_t elts_size) * * @remark Helper function, use ARR_RESIZE() instead. */ -void * -_arr_resize (void *elts, int nelts, size_t eltsize) +void *ir_arr_resize(void *elts, size_t nelts, size_t eltsize) { - _arr_descr *dp = _ARR_DESCR (elts); - int n; - - assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0)); - 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); - 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 - } - dp->nelts = nelts; - - return dp->v.elts; + ir_arr_descr *dp = ARR_DESCR(elts); + size_t n; + + 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); + while (nelts > n) n <<= 1; + while (3*nelts < n) n >>= 1; + assert(n >= nelts); + + if (n != dp->u.allocated) { + dp = (ir_arr_descr*) xrealloc(dp, ARR_ELTS_OFFS+eltsize*n); + dp->u.allocated = n; + } + dp->nelts = nelts; + + 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(void *arr) { - return ARR_LEN(arr); +size_t array_len(const void *arr) +{ + return ARR_LEN(arr); } /** @@ -211,9 +209,10 @@ int array_len(void *arr) { * Do NOT use is in code!. * This function is intended to be called from a debugger. */ -_arr_descr *array_descr(void *arr) { - if (! arr) - return NULL; - return _ARR_DESCR(arr); +ir_arr_descr *array_descr(const void *arr) +{ + if (! arr) + return NULL; + return ARR_DESCR(arr); } #endif /* DEBUG_libfirm */