remove $Id$, it doesn't work with git anyway
[libfirm] / ir / adt / array_t.h
1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Array --- dynamic & flexible arrays.
23  */
24 #ifndef FIRM_ADT_ARRAY_T_H
25 #define FIRM_ADT_ARRAY_T_H
26
27 #include "array.h"
28
29 #define ARR_D_MAGIC FOURCC('A','R','R','D')
30 #define ARR_A_MAGIC FOURCC('A','R','R','A')
31 #define ARR_F_MAGIC FOURCC('A','R','R','F')
32
33 #ifdef NDEBUG
34 # define ARR_SET_DBGINF(descr, co, es)
35 #else
36 # define ARR_SET_DBGINF(descr, co, es) \
37     ( (descr)->magic = (co), (descr)->eltsize = (es) )
38 #endif
39
40 /**
41  * Create an automatic array which will be deleted at return from function.
42  * Beware, the data will be allocated on the function stack!
43  *
44  * @param type     The element type of the new array.
45  * @param var      A lvalue of type (type *) which will hold the new array.
46  * @param n        number of elements in this array.
47  *
48  * This macro creates a dynamic array on the functions stack of a given type at runtime.
49  * The size of the array cannot be changed later.
50  */
51 #define NEW_ARR_A(type, var, n)                                    \
52   do {                                                             \
53     size_t nelts = (n);                                            \
54     (var) = (type *)((ir_arr_descr *)alloca(ARR_ELTS_OFFS + sizeof(type) * nelts))->elts; \
55     ARR_SET_DBGINF(ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \
56     (void)(ARR_DESCR((var))->nelts = nelts);                       \
57   } while (0)
58
59 /**
60  * Creates a new automatic array with the same number of elements as a
61  * given one.
62  *
63  * @param type     The element type of the new array.
64  * @param var      A lvalue of type (type *) which will hold the new array.
65  * @param arr      An array from which the elements will be duplicated
66  *
67  * This macro creates a dynamic array of a given type at runtime.
68  * The size of the array cannot be changed later.
69  *
70  * @return A pointer to the dynamic array (can be used as a pointer to the
71  *         first element of this array).
72  */
73 #define CLONE_ARR_A(type, var, arr)        \
74   NEW_ARR_A(type, (var), ARR_LEN((arr)))
75
76 /**
77  * Duplicates an array and returns a new automatic one.
78  *
79  * @param type     The element type of the new array.
80  * @param var      A lvalue of type (type *) which will hold the new array.
81  * @param arr      An array from with the number of elements will be taken
82  *
83  * This macro creates a dynamic array of a given type at runtime.
84  * The size of the array cannot be changed later.
85  *
86  * @return A pointer to the dynamic array (can be used as a pointer to the
87  *         first element of this array).
88  */
89 #define DUP_ARR_A(type, var, arr)                               \
90   do { CLONE_ARR_A(type, (var), (arr));                         \
91        memcpy((var), (arr), sizeof (type) * ARR_LEN((arr))); }  \
92   while (0)
93
94 #endif