libFirm 1.20
libfirm/adt/array.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
00003  *
00004  * This file is part of libFirm.
00005  *
00006  * This file may be distributed and/or modified under the terms of the
00007  * GNU General Public License version 2 as published by the Free Software
00008  * Foundation and appearing in the file LICENSE.GPL included in the
00009  * packaging of this file.
00010  *
00011  * Licensees holding valid libFirm Professional Edition licenses may use
00012  * this file in accordance with the libFirm Commercial License.
00013  * Agreement provided with the Software.
00014  *
00015  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00017  * PURPOSE.
00018  */
00019 
00025 #ifndef FIRM_ADT_ARRAY_H
00026 #define FIRM_ADT_ARRAY_H
00027 
00028 #include <assert.h>
00029 #include <stddef.h>
00030 
00031 #include "obst.h"
00032 
00033 #include "../begin.h"
00034 
00053 #define NEW_ARR_F(type, nelts) \
00054   ((type *)ir_new_arr_f((nelts), sizeof(type) * (nelts)))
00055 
00069 #define CLONE_ARR_F(type, arr) \
00070   NEW_ARR_F(type, ARR_LEN((arr)))
00071 
00084 #define DUP_ARR_F(type, arr) \
00085   ((type*) memcpy(CLONE_ARR_F(type, (arr)), (arr), sizeof(type) * ARR_LEN((arr))))
00086 
00092 #define DEL_ARR_F(arr) (ir_del_arr_f((void *)(arr)))
00093 
00107 #define NEW_ARR_D(type, obstack, nelts)                                 \
00108   (  nelts                                                              \
00109    ? (type *)ir_new_arr_d((obstack), (nelts), sizeof(type) * (nelts))   \
00110    : (type *)arr_mt_descr.elts)
00111 
00126 #define CLONE_ARR_D(type, obstack, arr) \
00127   NEW_ARR_D(type, (obstack), ARR_LEN((arr)))
00128 
00142 #define DUP_ARR_D(type, obstack, arr) \
00143   ((type*)memcpy(CLONE_ARR_D(type, (obstack), (arr)), (arr), sizeof(type) * ARR_LEN ((arr))))
00144 
00150 #define ARR_LEN(arr) (ARR_VRFY((arr)), ARR_DESCR((arr))->nelts)
00151 
00162 #define ARR_RESIZE(type, arr, n) \
00163   ((arr) = (type*) ir_arr_resize((void *)(arr), (n), sizeof(type)))
00164 
00174 #define ARR_SETLEN(type, arr, n) \
00175   ((arr) = (type*) ir_arr_setlen((void *)(arr), (n), sizeof(type) * (n)))
00176 
00186 #define ARR_EXTEND(type, arr, delta) \
00187   ARR_RESIZE(type, (arr), ARR_LEN((arr)) + (delta))
00188 
00199 #define ARR_EXTO(type, arr, n) \
00200     do { \
00201         if ((n) >= ARR_LEN(arr)) { ARR_RESIZE(type, arr, (n)+1); } \
00202     } while(0)
00203 
00211 #define ARR_APP1(type, arr, elt) \
00212   (ARR_EXTEND(type, (arr), 1), (arr)[ARR_LEN((arr))-1] = (elt))
00213 
00214 #ifdef NDEBUG
00215 # define ARR_VRFY(arr)          ((void)0)
00216 # define ARR_IDX_VRFY(arr, idx) ((void)0)
00217 #else
00218 
00219 # define ARR_VRFY(arr)          ir_verify_arr(arr)
00220 
00221 # define ARR_IDX_VRFY(arr, idx) \
00222     assert((0 <= (idx)) && ((idx) < ARR_LEN((arr))))
00223 #endif
00224 
00228 typedef union {
00229   long double d;
00230   void *p;
00231   long l;
00232 } aligned_type;
00233 
00237 typedef struct {
00238     int magic;                    
00239     size_t eltsize;               
00240     union {
00241         struct obstack *obstack;  
00242         size_t allocated;         
00243     } u;
00244     size_t nelts;                 
00245     aligned_type elts[1];         
00246 } ir_arr_descr;
00247 
00248 extern ir_arr_descr arr_mt_descr;
00249 
00250 FIRM_API void *ir_new_arr_f(size_t nelts, size_t elts_size);
00251 FIRM_API void ir_del_arr_f(void *elts);
00252 FIRM_API void *ir_new_arr_d(struct obstack *obstack, size_t nelts, size_t elts_size);
00253 FIRM_API void *ir_arr_resize(void *elts, size_t nelts, size_t elts_size);
00254 FIRM_API void *ir_arr_setlen(void *elts, size_t nelts, size_t elts_size);
00255 FIRM_API void ir_verify_arr(const void *elts);
00256 
00257 #define ARR_ELTS_OFFS offsetof(ir_arr_descr, elts)
00258 #define ARR_DESCR(elts) ((ir_arr_descr *)(void *)((char *)(elts) - ARR_ELTS_OFFS))
00259 
00262 static inline void ARR_SHRINKLEN(void *arr, size_t new_len)
00263 {
00264     ARR_VRFY(arr);
00265     assert(ARR_DESCR(arr)->nelts >= new_len);
00266     ARR_DESCR(arr)->nelts = new_len;
00267 }
00268 
00273 #include "../end.h"
00274 
00275 #endif