Unconditionally include string.h
[libfirm] / ir / adt / array_t.h
1 /*
2  * Copyright (C) 1995-2008 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  * @version     $Id: array.c 17964 2008-03-05 09:48:51Z matze $
24  */
25 #ifndef FIRM_ADT_ARRAY_T_H
26 #define FIRM_ADT_ARRAY_T_H
27
28 #include "array.h"
29
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')
33
34 #ifdef NDEBUG
35 # define ARR_SET_DBGINF(descr, co, es)
36 #else
37 # define ARR_SET_DBGINF(descr, co, es)                                  \
38     ( (descr)->magic = (co), (descr)->eltsize = (es) )
39 #endif
40
41 /**
42  * Create an automatic array which will be deleted at return from function.
43  * Beware, the data will be allocated on the function stack!
44  *
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.
48  *
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.
51  */
52 #define NEW_ARR_A(type, var, n)                                                                 \
53   do {                                                                                          \
54     int nelts = (n);                                                                            \
55     assert(nelts >= 0);                                                                 \
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);                                                    \
59   } while (0)
60
61 /**
62  * Creates a new automatic array with the same number of elements as a
63  * given one.
64  *
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
68  *
69  * This macro creates a dynamic array of a given type at runtime.
70  * The size of the array cannot be changed later.
71  *
72  * @return A pointer to the dynamic array (can be used as a pointer to the
73  *         first element of this array).
74  */
75 #define CLONE_ARR_A(type, var, arr)             \
76   NEW_ARR_A(type, (var), ARR_LEN((arr)))
77
78 /**
79  * Duplicates an array and returns a new automatic one.
80  *
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
84  *
85  * This macro creates a dynamic array of a given type at runtime.
86  * The size of the array cannot be changed later.
87  *
88  * @return A pointer to the dynamic array (can be used as a pointer to the
89  *         first element of this array).
90  */
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))); }  \
94   while (0)
95
96 #endif