X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fadt%2Farray.h;h=76eaf2bc32a2e1f6019c2f9daa9bc67bef8ff022;hb=5a4bec5f11d4d57f749f24a9af178036f7b734b1;hp=11c0be6cf78a63f626087c4b95542c9b95641ef1;hpb=efbeaff549fcc6015da255ed4d453a95937ff0fd;p=libfirm diff --git a/ir/adt/array.h b/ir/adt/array.h index 11c0be6cf..76eaf2bc3 100644 --- a/ir/adt/array.h +++ b/ir/adt/array.h @@ -1,8 +1,18 @@ -/* Declarations for Array. - Copyright (C) 1995, 1996 Markus Armbruster - All rights reserved. */ +/* + * Project: libFIRM + * File name: ir/adt/array.h + * Purpose: Declarations for Array. + * 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. + */ -/* @@@ growing a dynamic on an obstack */ +/** + * @file array.h Dynamic and flexible arrays for C. + */ #ifndef _ARRAY_H #define _ARRAY_H @@ -10,31 +20,129 @@ #include #include #include -#include "cookies.h" -#include "misc.h" +#include "fourcc.h" +#include "align.h" -/* Flexible create / delete */ +#define ARR_D_MAGIC FOURCC('A','R','R','D') +#define ARR_A_MAGIC FOURCC('A','R','R','A') +#define ARR_F_MAGIC FOURCC('A','R','R','F') + +/** + * Creates a flexible array. + * + * @param type The element type of the new array. + * @param nelts a size_t expression evaluating to the number of elements + * + * This macro creates a flexible array of a given type at runtime. + * The size of the array can be changed later. + * + * @return A pointer to the flexible array (can be used as a pointer to the + * first element of this array). + */ #define NEW_ARR_F(type, nelts) \ - (XMALLOC_TRACE (type *)_new_arr_f ((nelts), sizeof(type) * (nelts))) + ((type *)_new_arr_f ((nelts), sizeof(type) * (nelts))) + +/** + * Creates a new flxible array with the same number of elements as a + * given one. + * + * @param type The element type of the new array. + * @param arr An array from which the number of elements will be taken + * + * This macro creates a flexible array of a given type at runtime. + * The size of the array can be changed later. + * + * @return A pointer to the flexible array (can be used as a pointer to the + * first element of this array). + */ #define CLONE_ARR_F(type, arr) \ NEW_ARR_F (type, ARR_LEN ((arr))) + +/** + * Duplicates an array and returns the new flexible one. + * + * @param type The element type of the new array. + * @param arr An array from which the elements will be duplicated + * + * This macro creates a flexible array of a given type at runtime. + * The size of the array can be changed later. + * + * @return A pointer to the flexible array (can be used as a pointer to the + * first element of this array). + */ #define DUP_ARR_F(type, arr) \ memcpy (CLONE_ARR_F (type, (arr)), (arr), sizeof(type) * ARR_LEN((arr))) -#define DEL_ARR_F(arr) (XMALLOC_TRACE _del_arr_f ((arr))) -/* Dynamic create on obstacks */ +/** + * Delete a flexible array. + * + * @param arr The flexible array. + */ +#define DEL_ARR_F(arr) (_del_arr_f ((arr))) + +/** + * Creates a dynamic array on an obstack. + * + * @param type The element type of the new array. + * @param obstack A struct obstack * were the data will be allocated + * @param nelts A size_t expression evaluating to the number of elements + * + * This macro creates a dynamic array of a given type at runtime. + * The size of the array cannot be changed later. + * + * @return A pointer to the dynamic array (can be used as a pointer to the + * first element of this array). + */ #define NEW_ARR_D(type, obstack, nelts) \ ( nelts \ ? (type *)_new_arr_d ((obstack), (nelts), sizeof(type) * (nelts)) \ : (type *)arr_mt_descr.v.elts) + +/** + * Creates a new dynamic array with the same number of elements as a + * given one. + * + * @param type The element type of the new array. + * @param obstack An struct obstack * were the data will be allocated + * @param arr An array from which the number of elements will be taken + * + * This macro creates a dynamic array of a given type at runtime. + * The size of the array cannot be changed later. + * + * @return A pointer to the dynamic array (can be used as a pointer to the + * first element of this array). + */ #define CLONE_ARR_D(type, obstack, arr) \ NEW_ARR_D (type, (obstack), ARR_LEN ((arr))) + +/** + * Duplicates an array and returns the new dynamic one. + * + * @param type The element type of the new array. + * @param obstack An struct obstack * were the data will be allocated + * @param arr An array from which the elements will be duplicated + * + * This macro creates a dynamic array of a given type at runtime. + * The size of the array cannot be changed later. + * + * @return A pointer to the dynamic array (can be used as a pointer to the + * first element of this array). + */ #define DUP_ARR_D(type, obstack, arr) \ memcpy (CLONE_ARR_D (type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))) -/* Automatic create; delete is automatic at return from function */ -/* Quick'n'dirty! */ +/** + * Create an automatic array which will be deleted at return from function. + * Beware, the data will be allocated on the function stack! + * + * @param type The element type of the new array. + * @param var A lvalue of type (type *) which will hold the new array. + * @param n number of elements in this array. + * + * This macro creates a dynamic array on the functions stack of a given type at runtime. + * The size of the array cannot be changed later. + */ #define NEW_ARR_A(type, var, n) \ do { \ int _nelts = (n); \ @@ -43,39 +151,126 @@ _ARR_SET_DBGINF (_ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \ (void)(_ARR_DESCR ((var))->nelts = _nelts); \ } while (0) + +/** + * Creates a new automatic array with the same number of elements as a + * given one. + * + * @param type The element type of the new array. + * @param var A lvalue of type (type *) which will hold the new array. + * @param arr An array from which the elements will be duplicated + * + * This macro creates a dynamic array of a given type at runtime. + * The size of the array cannot be changed later. + * + * @return A pointer to the dynamic array (can be used as a pointer to the + * first element of this array). + */ #define CLONE_ARR_A(type, var, arr) \ NEW_ARR_A (type, (var), ARR_LEN ((arr))) +/** + * Duplicates an array and returns a new automatic one. + * + * @param type The element type of the new array. + * @param var A lvalue of type (type *) which will hold the new array. + * @param arr An array from with the number of elements will be taken + * + * This macro creates a dynamic array of a given type at runtime. + * The size of the array cannot be changed later. + * + * @return A pointer to the dynamic array (can be used as a pointer to the + * first element of this array). + */ #define DUP_ARR_A(type, var, arr) \ do { CLONE_ARR_A(type, (var), (arr)); \ memcpy ((var), (arr), sizeof (type) * ARR_LEN ((arr))); } \ while (0) -/* Declare an initialized array of fixed size */ +/** + * Declare an initialized (zero'ed) array of fixed size. + * This macro should be used at file scope only. + * + * @param type The element type of the new array. + * @param var A lvalue of type (type *) which will hold the new array. + * @param _nelts Number of elements in this new array. + */ #define DECL_ARR_S(type, var, _nelts) \ ARR_STRUCT(type, (_nelts) ? (_nelts) : 1) _##var; \ type *var = (_ARR_SET_DBGINF (&_##var, ARR_A_MAGIC, sizeof (type)), \ _##var.nelts = _nelts, \ _##var.v.elts) -/* Length */ +/** + * Returns the length of an array + * + * @param arr a flexible, dynamic, automatic or static array. + */ #define ARR_LEN(arr) (ARR_VRFY ((arr)), _ARR_DESCR((arr))->nelts) -/* Resize - Applicable to flexibles only, change arr which must be an lvalue. */ -/* resize arr to hold n elts */ +/** + * Resize a flexible array, allocate more data if needed but do NOT + * reduce. + * + * @param type The element type of the array. + * @param arr The array, which must be an lvalue. + * @param n The new size of the array. + * + * @remark This macro may change arr, so update all references! + */ #define ARR_RESIZE(type, arr, n) \ - (XMALLOC_TRACE (arr) = _arr_resize ((arr), (n), sizeof(type))) -/* resize arr to hold exactly n elts */ + ((arr) = _arr_resize ((arr), (n), sizeof(type))) + +/** + * Resize a flexible array, always reallocate data. + * + * @param type The element type of the array. + * @param arr The array, which must be an lvalue. + * @param n The new size of the array. + * + * @remark This macro may change arr, so update all references! + */ #define ARR_SETLEN(type, arr, n) \ - (XMALLOC_TRACE (arr) = _arr_setlen ((arr), (n), sizeof(type) * (n))) -/* resize arr by delta elts */ + ((arr) = _arr_setlen ((arr), (n), sizeof(type) * (n))) + +/** Set a length smaller than the current length of the array. Do not + * resize. len must be <= ARR_LEN(arr). */ +#define ARR_SHRINKLEN(arr,len) \ + (ARR_VRFY ((arr)), assert(_ARR_DESCR((arr))->nelts >= len), \ + _ARR_DESCR((arr))->nelts = len) + +/** + * Resize a flexible array by growing it by delta elements. + * + * @param type The element type of the array. + * @param arr The array, which must be an lvalue. + * @param delta The delta number of elements. + * + * @remark This macro may change arr, so update all references! + */ #define ARR_EXTEND(type, arr, delta) \ ARR_RESIZE (type, (arr), ARR_LEN ((arr)) + (delta)) -/* resize arr to hold n elts only if it is currently shorter */ + +/** + * Resize a flexible array to hold n elements only if it is currently shorter + * than n. + * + * @param type The element type of the array. + * @param arr The array, which must be an lvalue. + * @param n The new size of the array. + * + * @remark This macro may change arr, so update all references! + */ #define ARR_EXTO(type, arr, n) \ ((n) >= ARR_LEN ((arr)) ? ARR_RESIZE (type, (arr), (n)+1) : (arr)) -/* append one elt to arr */ + +/** + * Append one element to a flexible array. + * + * @param type The element type of the array. + * @param arr The array, which must be an lvalue. + * @param elt The new element, must be of type (type). + */ #define ARR_APP1(type, arr, elt) \ (ARR_EXTEND (type, (arr), 1), (arr)[ARR_LEN ((arr))-1] = (elt)) @@ -85,10 +280,10 @@ # define ARR_IDX_VRFY(arr, idx) ((void)0) #else # define ARR_VRFY(arr) \ - assert ( ( (_ARR_DESCR((arr))->cookie == ARR_D_MAGIC) \ - || (_ARR_DESCR((arr))->cookie == ARR_A_MAGIC) \ - || (_ARR_DESCR((arr))->cookie == ARR_F_MAGIC)) \ - && ( (_ARR_DESCR((arr))->cookie != ARR_F_MAGIC) \ + assert ( ( (_ARR_DESCR((arr))->magic == ARR_D_MAGIC) \ + || (_ARR_DESCR((arr))->magic == ARR_A_MAGIC) \ + || (_ARR_DESCR((arr))->magic == ARR_F_MAGIC)) \ + && ( (_ARR_DESCR((arr))->magic != ARR_F_MAGIC) \ || (_ARR_DESCR((arr))->u.allocated >= _ARR_DESCR((arr))->nelts)) \ && (_ARR_DESCR((arr))->nelts >= 0)) # define ARR_IDX_VRFY(arr, idx) \ @@ -96,19 +291,21 @@ #endif -/* Private */ -/* Don't try this at home, kids, we're trained professionals ;-> */ - -// ... or at the IPD, either. +/* Private !!! + Don't try this at home, kids, we're trained professionals ;-> + ... or at the IPD, either. */ #ifdef NDEBUG # define _ARR_DBGINF_DECL -# define _ARR_SET_DBGINF(descr, co, es) ((co), (es)) +# define _ARR_SET_DBGINF(descr, co, es) #else -# define _ARR_DBGINF_DECL int cookie; size_t eltsize; +# define _ARR_DBGINF_DECL int magic; size_t eltsize; # define _ARR_SET_DBGINF(descr, co, es) \ - ((descr)->cookie = (co), (descr)->eltsize = (es)) + ( (descr)->magic = (co), (descr)->eltsize = (es) ) #endif +/** + * Construct an array header. + */ #define ARR_STRUCT(type, _nelts) \ struct { \ _ARR_DBGINF_DECL \ @@ -123,13 +320,16 @@ } v; \ } +/** + * The array descriptor header type. + */ typedef ARR_STRUCT (aligned_type, 1) _arr_descr; extern _arr_descr arr_mt_descr; void *_new_arr_f (int, size_t); void _del_arr_f (void *); -void *_new_arr_d (struct obstack *, int, size_t); +void *_new_arr_d (struct obstack *obstack, int nelts, size_t elts_size); void *_arr_resize (void *, int, size_t); void *_arr_setlen (void *, int, size_t);