2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Array --- dynamic & flexible arrays.
10 #ifndef FIRM_ADT_ARRAY_T_H
11 #define FIRM_ADT_ARRAY_T_H
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')
21 # define ARR_SET_DBGINF(descr, co)
23 # define ARR_SET_DBGINF(descr, co) \
24 ((descr)->magic = (co))
28 * Create an automatic array which will be deleted at return from function.
29 * Beware, the data will be allocated on the function stack!
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.
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.
38 #define NEW_ARR_A(type, var, 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); \
47 * Creates a new automatic array with the same number of elements as a
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
54 * This macro creates a dynamic array of a given type at runtime.
55 * The size of the array cannot be changed later.
57 * @return A pointer to the dynamic array (can be used as a pointer to the
58 * first element of this array).
60 #define CLONE_ARR_A(type, var, arr) \
61 NEW_ARR_A(type, (var), ARR_LEN((arr)))
64 * Duplicates an array and returns a new automatic one.
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
70 * This macro creates a dynamic array of a given type at runtime.
71 * The size of the array cannot be changed later.
73 * @return A pointer to the dynamic array (can be used as a pointer to the
74 * first element of this array).
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))); } \