X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fadt%2Farray.c;h=84674e3c0012df3e009b5a8a7ee049628b91b6e1;hb=48b0fa8564962b17e136a0f925eff458ca16abef;hp=c082aba7d8702f341eb585ddc4c8c96637563996;hpb=ab2620a4aef90ff7cf9419abf0a0612339864579;p=libfirm diff --git a/ir/adt/array.c b/ir/adt/array.c index c082aba7d..84674e3c0 100644 --- a/ir/adt/array.c +++ b/ir/adt/array.c @@ -1,22 +1,34 @@ /* - * Project: libFIRM - * File name: ir/adt/array.c - * Purpose: Array --- dynamic & flexible arrays. - * Author: Markus Armbruster - * Modified by: - * Created: 1999 by getting from fiasco - * CVS-ID: $Id$ - * Copyright: (c) 1995, 1996 Markus Armbruster - * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. + * 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. */ -#ifdef HAVE_CONFIG_H -# include -#endif +/** + * @file + * @brief Array --- dynamic & flexible arrays. + * @author Markus Armbruster + * @version $Id$ + */ + +#include "config.h" #include -#include "array.h" +#include "array_t.h" #include "xmalloc.h" /* Undefine the macros to get the functions instead, cf tmalloc.c. */ @@ -33,13 +45,24 @@ #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); + if (descr->magic == ARR_F_MAGIC) { + assert(descr->u.allocated >= descr->nelts); + } + assert(descr->nelts >= 0); +#else + (void) arr; #endif -; +} /** * Creates a dynamic array on a obstack. @@ -53,18 +76,16 @@ _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) -{ - _arr_descr *new; +void *ir_new_arr_d(struct obstack *obstack, int nelts, size_t elts_size) { + ir_arr_descr *dp; - assert (obstack && (nelts >= 0)); + assert(obstack && (nelts >= 0)); - 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 = 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; } /** @@ -78,16 +99,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) -{ - _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(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; } /** @@ -97,18 +116,19 @@ _new_arr_f (int nelts, size_t elts_size) * * @remark Helper function, use DEL_ARR_F() instead. */ -void -_del_arr_f (void *elts) -{ - _arr_descr *dp = _ARR_DESCR (elts); +void ir_del_arr_f(void *elts) { + ir_arr_descr *dp = ARR_DESCR (elts); - ARR_VRFY (elts); - assert (dp->cookie == ARR_F_MAGIC); + ARR_VRFY (elts); + assert (dp->magic == ARR_F_MAGIC); #ifndef NDEBUG - dp->cookie = 0xdeadbeef; + { + ir_arr_descr *wdp = (ir_arr_descr *)dp; + wdp->magic = 0xdeadbeef; + } #endif - free (dp); + free(dp); } /** @@ -123,59 +143,77 @@ _del_arr_f (void *elts) * * @remark Helper function, use ARR_SETLEN() instead. */ -void * -_arr_setlen (void *elts, int nelts, size_t elts_size) -{ - _arr_descr *dp = _ARR_DESCR (elts); +void *ir_arr_setlen (void *elts, int nelts, size_t elts_size) { + ir_arr_descr *dp = ARR_DESCR (elts); - assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0)); - ARR_VRFY (elts); - assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts)); + assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0)); + 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 = xrealloc (dp, ARR_ELTS_OFFS+elts_size); + dp->u.allocated = dp->nelts = nelts; - return dp->v.elts; + return dp->v.elts; } /** * Resize a flexible array, allocate more data if needed but do NOT * reduce. * - * @param elts The flexible array (pointer to the first element). - * @param nelts The new number of elements. - * @param elts_size The size of the array elements. + * @param elts The flexible array (pointer to the first element). + * @param nelts The new number of elements. + * @param eltsize The size of the array elements. * * @return A resized flexible array, possibly other address than * elts. * * @remark Helper function, use ARR_RESIZE() instead. */ -void * -_arr_resize (void *elts, int nelts, size_t eltsize) -{ - _arr_descr *dp = _ARR_DESCR (elts); - int n; - - assert ((dp->cookie == 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; +void *ir_arr_resize(void *elts, int nelts, size_t eltsize) { + ir_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; + } else { + tmalloc_tag = NULL; #endif - } - dp->nelts = nelts; + } + dp->nelts = nelts; + + return dp->v.elts; +} - return dp->v.elts; +#ifdef DEBUG_libfirm +/** + * 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) { + return ARR_LEN(arr); +} + +/** + * This function returns the array descriptor of a flexible array. + * Do NOT use is in code!. + * This function is intended to be called from a debugger. + */ +ir_arr_descr *array_descr(const void *arr) { + if (! arr) + return NULL; + return ARR_DESCR(arr); } +#endif /* DEBUG_libfirm */