Common macros and compiler attributes/pragmas configuration. More...
Common macros and compiler attributes/pragmas configuration.
Definition in file kernel_defines.h.
#include <stddef.h>
#include <stdint.h>
Go to the source code of this file.
#define | container_of(PTR, TYPE, MEMBER) ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))) |
Returns the container of a pointer to a member. More... | |
#define | NORETURN |
The NORETURN keyword tells the compiler to assume that the function cannot return. | |
#define | CONST |
A function declared as CONST is PURE and also not allowed to examine global memory. I.e. a CONST function cannot even dereference a pointer parameter. | |
#define | PURE |
The function has no effects except the return value and its return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. | |
#define | UNREACHABLE() do { /* nothing */ } while (1) |
Tell the compiler that this line of code cannot be reached. More... | |
#define | ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0])) |
Calculate the number of elements in a static array. More... | |
#define | ALIGN_OF(T) (offsetof(struct { char c; T t; }, t)) |
Calculate the minimal alignment for type T. More... | |
#define | BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)])) |
Forces a compilation error if condition is true. This trick is only needed if the condition can't be evaluated before compile time (i.e. sizeof(sometype_t) < 42 ) For more details on this see for example: https://git.kernel.org/pub/scm/linux/kernel/git/stable/ linux-stable.git/tree/include/linux/bug.h. More... | |
#define | IS_ACTIVE(macro) __is_active(macro) |
Allows to verify a macro definition outside the preprocessor. More... | |
#define | IS_USED(module) IS_ACTIVE(module) |
Checks whether a module is being used or not. Can be used in C conditionals. More... | |
#define ALIGN_OF | ( | T | ) | (offsetof(struct { char c; T t; }, t)) |
Calculate the minimal alignment for type T.
[in] | T | Type to examine |
Definition at line 131 of file kernel_defines.h.
#define ARRAY_SIZE | ( | a | ) | (sizeof((a)) / sizeof((a)[0])) |
Calculate the number of elements in a static array.
[in] | a | Array to examine |
Definition at line 122 of file kernel_defines.h.
#define BUILD_BUG_ON | ( | condition | ) | ((void)sizeof(char[1 - 2 * !!(condition)])) |
Forces a compilation error if condition is true. This trick is only needed if the condition can't be evaluated before compile time (i.e. sizeof(sometype_t) < 42 ) For more details on this see for example: https://git.kernel.org/pub/scm/linux/kernel/git/stable/ linux-stable.git/tree/include/linux/bug.h.
[in] | condition | A condition that will be evaluated at compile time |
Definition at line 143 of file kernel_defines.h.
#define container_of | ( | PTR, | |
TYPE, | |||
MEMBER | |||
) | ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))) |
Returns the container of a pointer to a member.
For a struct TYPE
with a member MEMBER
, given a pointer PTR
to TYPE::MEMBER
this function returns a pointer to the instance of TYPE
.
E.g. for struct my_struct_t { ...; something_t n; ... } my_struct;
, &my_struct == container_of(&my_struct.n, struct my_struct_t, n)
.
[in] | PTR | pointer to a member |
[in] | TYPE | a type name (a struct or union), container of PTR |
[in] | MEMBER | name of the member of TYPE which PTR points to |
Definition at line 62 of file kernel_defines.h.
#define IS_ACTIVE | ( | macro | ) | __is_active(macro) |
Allows to verify a macro definition outside the preprocessor.
This macro is based on Linux's clever 'IS_BUILTIN' (https://github.com/torvalds/linux/blob/master/include/linux/kconfig.h). It takes a macro
value that may be defined to 1 or not even defined (e.g. FEATURE_FOO) and then expands it to an expression that can be used in C code, either 1 or 0.
The advantage of using this is that the compiler sees all the code, so checks can be performed, sections that would not be executed are removed during optimization. For example:
[in] | macro | Macro to evaluate |
Definition at line 177 of file kernel_defines.h.
#define IS_USED | ( | module | ) | IS_ACTIVE(module) |
Checks whether a module is being used or not. Can be used in C conditionals.
[in] | module | Module to check |
Definition at line 188 of file kernel_defines.h.
#define UNREACHABLE | ( | ) | do { /* nothing */ } while (1) |
Tell the compiler that this line of code cannot be reached.
Most useful in junction with NORETURN. Use this if the compiler cannot tell that e.g. an assembler instruction causes a longjmp, or a write causes a reboot.
Definition at line 112 of file kernel_defines.h.