2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Array --- dynamic & flexible arrays.
23 * @version $Id: array.c 17964 2008-03-05 09:48:51Z matze $
25 #ifndef FIRM_ADT_ARRAY_T_H
26 #define FIRM_ADT_ARRAY_T_H
30 #define ARR_D_MAGIC FOURCC('A','R','R','D')
31 #define ARR_A_MAGIC FOURCC('A','R','R','A')
32 #define ARR_F_MAGIC FOURCC('A','R','R','F')
35 # define ARR_SET_DBGINF(descr, co, es)
37 # define ARR_SET_DBGINF(descr, co, es) \
38 ( (descr)->magic = (co), (descr)->eltsize = (es) )
42 * Create an automatic array which will be deleted at return from function.
43 * Beware, the data will be allocated on the function stack!
45 * @param type The element type of the new array.
46 * @param var A lvalue of type (type *) which will hold the new array.
47 * @param n number of elements in this array.
49 * This macro creates a dynamic array on the functions stack of a given type at runtime.
50 * The size of the array cannot be changed later.
52 #define NEW_ARR_A(type, var, n) \
56 (var) = (void *)((ir_arr_descr *)alloca(ARR_ELTS_OFFS + sizeof(type) * nelts))->v.elts; \
57 ARR_SET_DBGINF(ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \
58 (void)(ARR_DESCR((var))->nelts = nelts); \
62 * Creates a new automatic array with the same number of elements as a
65 * @param type The element type of the new array.
66 * @param var A lvalue of type (type *) which will hold the new array.
67 * @param arr An array from which the elements will be duplicated
69 * This macro creates a dynamic array of a given type at runtime.
70 * The size of the array cannot be changed later.
72 * @return A pointer to the dynamic array (can be used as a pointer to the
73 * first element of this array).
75 #define CLONE_ARR_A(type, var, arr) \
76 NEW_ARR_A(type, (var), ARR_LEN((arr)))
79 * Duplicates an array and returns a new automatic one.
81 * @param type The element type of the new array.
82 * @param var A lvalue of type (type *) which will hold the new array.
83 * @param arr An array from with the number of elements will be taken
85 * This macro creates a dynamic array of a given type at runtime.
86 * The size of the array cannot be changed later.
88 * @return A pointer to the dynamic array (can be used as a pointer to the
89 * first element of this array).
91 #define DUP_ARR_A(type, var, arr) \
92 do { CLONE_ARR_A(type, (var), (arr)); \
93 memcpy((var), (arr), sizeof (type) * ARR_LEN((arr))); } \