2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
9 * @author Sebastian Hack
10 * @brief Miscellaneous utility macros.
12 #ifndef FIRM_ADT_UTIL_H
13 #define FIRM_ADT_UTIL_H
18 * Make pointer to the struct from a pointer to a member of that struct.
19 * @param ptr The pointer to the member.
20 * @param type The type of the struct.
21 * @param member The name of the member.
22 * @return A pointer to the struct member is in.
24 #define firm_container_of(ptr, type, member) \
25 ((type *) ((char *) (ptr) - offsetof(type, member)))
28 * Returns size of a static array. Warning: This returns invalid values for
29 * dynamically allocated arrays.
31 * @param a static array
33 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
37 #define MAX(x, y) ((x) > (y) ? (x) : (y))
38 #define MIN(x, y) ((x) < (y) ? (x) : (y))
41 * Three valued compare as demanded by e.g. qsort(3)
43 * @param d Another number.
44 * @return 0 if c == d, -1 if c < d, 1 if c > d.
46 #define QSORT_CMP(c, d) (((c) > (d)) - ((c) < (d)))
49 * convert an integer into pointer
51 #define INT_TO_PTR(v) ((void *)((char *)0 + (v)))
54 * convert a pointer into an integer
56 #define PTR_TO_INT(v) (((char *)(v) - (char *)0))