X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fadt%2Farray.c;h=6f9618482bee0a1960ac0e16b622862570b2e634;hb=d0d85962ef52c14950db90e5981a7bea36023ab3;hp=53984b96e1af25bbc2683d0d30c2ef9858e2aa1f;hpb=e3e22fa6f927847099c0bff62457003aa81f2518;p=libfirm diff --git a/ir/adt/array.c b/ir/adt/array.c index 53984b96e..6f9618482 100644 --- a/ir/adt/array.c +++ b/ir/adt/array.c @@ -1,15 +1,25 @@ -/* Array --- dynamic & flexible arrays. - Copyright (C) 1995, 1996 Markus Armbruster - All rights reserved. */ - -/* $Id$ */ +/* + * 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. + */ #ifdef HAVE_CONFIG_H -# include +# include "config.h" +#endif + +#ifdef HAVE_STDLIB_H +# include #endif -#include #include "array.h" +#include "xmalloc.h" /* Undefine the macros to get the functions instead, cf tmalloc.c. */ #undef xmalloc @@ -17,13 +27,34 @@ #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 + */ _arr_descr arr_mt_descr #ifndef NDEBUG = { ARR_D_MAGIC } #endif ; +/** + * Creates a dynamic array on a obstack. + * + * @param obstack An struct obstack * were the data will be allocated + * @param nelts The number of elements + * @param elts_size The size of the array elements. + * + * @return A pointer to the dynamic array (can be used as a pointer to the + * first element of this array). + * + * @remark Helper function, use NEW_ARR_D() instead. + */ void * _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size) { @@ -38,7 +69,17 @@ _new_arr_d (struct obstack *obstack, int nelts, size_t elts_size) return new->v.elts; } - +/** + * Creates a flexible array. + * + * @param nelts The number of elements + * @param elts_size The size of the array elements. + * + * @return A pointer to the flexible array (can be used as a pointer to the + * first element of this array). + * + * @remark Helper function, use NEW_ARR_F() instead. + */ void * _new_arr_f (int nelts, size_t elts_size) { @@ -51,28 +92,45 @@ _new_arr_f (int nelts, size_t elts_size) return new->v.elts; } - +/** + * Delete a flexible array. + * + * @param elts The flexible array (pointer to the first element). + * + * @remark Helper function, use DEL_ARR_F() instead. + */ void _del_arr_f (void *elts) { _arr_descr *dp = _ARR_DESCR (elts); ARR_VRFY (elts); - assert (dp->cookie == ARR_F_MAGIC); + assert (dp->magic == ARR_F_MAGIC); #ifndef NDEBUG - dp->cookie = 0xdeadbeef; + dp->magic = 0xdeadbeef; #endif free (dp); } - +/** + * Resize a flexible array, always reallocate data. + * + * @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. + * + * @return A resized flexible array, possibly other address than + * 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); - assert ((dp->cookie == ARR_F_MAGIC) && (nelts >= 0)); + assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0)); ARR_VRFY (elts); assert (!dp->eltsize || !nelts || (dp->eltsize == elts_size/nelts)); @@ -82,14 +140,26 @@ _arr_setlen (void *elts, int nelts, size_t elts_size) 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 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)); + assert ((dp->magic == ARR_F_MAGIC) && (nelts >= 0)); ARR_VRFY (elts); assert (dp->eltsize ? dp->eltsize == eltsize : (dp->eltsize = eltsize, 1)); @@ -111,3 +181,25 @@ _arr_resize (void *elts, int nelts, size_t eltsize) 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(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. + */ +_arr_descr *array_descr(void *arr) { + if (! arr) + return NULL; + return _ARR_DESCR(arr); +} +#endif /* DEBUG_libfirm */