fix a bunch of warnings reported by clang analyzer
[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 #include "fourcc.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     size_t nelts = (n);                                            \
55     (var) = (type *)((ir_arr_descr *)alloca(ARR_ELTS_OFFS + sizeof(type) * nelts))->elts; \
56     ARR_SET_DBGINF(ARR_DESCR ((var)), ARR_A_MAGIC, sizeof (type)); \
57     (void)(ARR_DESCR((var))->nelts = nelts);                       \
58   } while (0)
59
60 /**
61  * Creates a new automatic array with the same number of elements as a
62  * given one.
63  *
64  * @param type     The element type of the new array.
65  * @param var      A lvalue of type (type *) which will hold the new array.
66  * @param arr      An array from which the elements will be duplicated
67  *
68  * This macro creates a dynamic array of a given type at runtime.
69  * The size of the array cannot be changed later.
70  *
71  * @return A pointer to the dynamic array (can be used as a pointer to the
72  *         first element of this array).
73  */
74 #define CLONE_ARR_A(type, var, arr)        \
75   NEW_ARR_A(type, (var), ARR_LEN((arr)))
76
77 /**
78  * Duplicates an array and returns a new automatic one.
79  *
80  * @param type     The element type of the new array.
81  * @param var      A lvalue of type (type *) which will hold the new array.
82  * @param arr      An array from with the number of elements will be taken
83  *
84  * This macro creates a dynamic array of a given type at runtime.
85  * The size of the array cannot be changed later.
86  *
87  * @return A pointer to the dynamic array (can be used as a pointer to the
88  *         first element of this array).
89  */
90 #define DUP_ARR_A(type, var, arr)                               \
91   do { CLONE_ARR_A(type, (var), (arr));                         \
92        memcpy((var), (arr), sizeof (type) * ARR_LEN((arr))); }  \
93   while (0)
94
95 #endif