4 * @author Sebastian Hack
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
16 * Get the offset of a member of a struct.
17 * @param type The type of the struct member is in.
18 * @param member The name of the member.
19 * @return The offset of member in type in bytes.
21 #define offset_of(type, member) \
22 ((char *) &(((type *) 0)->member) - (char *) 0)
25 * Make pointer to the struct from a pointer to a member of that struct.
26 * @param ptr The pointer to the member.
27 * @param type The type of the struct.
28 * @param member The name of the member.
29 * @return A pointer to the struct member is in.
31 #define container_of(ptr, type, member) \
32 ((type *) ((char *) (ptr) - offset_of(type, member)))
35 * Get the number of elements of a static array.
36 * @param arr The static array.
37 * @return The number of elements in that array.
39 #define array_size(arr) \
40 (sizeof(arr) / sizeof((arr)[0]))
43 * Asserts that the constant expression x is not zero at compiletime. name has
44 * to be a unique identifier.
46 * @note This uses the fact, that double case labels are not allowed.
48 #define COMPILETIME_ASSERT(x, name) \
49 static __attribute__((unused)) void compiletime_assert_##name (int h) { \
50 switch(h) { case 0: case (x): ; } \
55 * Indicates to the compiler that the value of x is very likely 1
56 * @note Only use this in speed critical code and when you are sure x is often 1
58 #define LIKELY(x) __builtin_expect((x), 1)
61 * Indicates to the compiler that it's very likely that x is 0
62 * @note Only use this in speed critical code and when you are sure x is often 0
64 #define UNLIKELY(x) __builtin_expect((x), 0)