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