kernel_defines.h File Reference

Common macros and compiler attributes/pragmas configuration. More...

Detailed Description

Common macros and compiler attributes/pragmas configuration.

Author
René Kijewski rene..nosp@m.kije.nosp@m.wski@.nosp@m.fu-b.nosp@m.erlin.nosp@m..de
Michel Rottleuthner miche.nosp@m.l.ro.nosp@m.ttleu.nosp@m.thne.nosp@m.r@haw.nosp@m.-ham.nosp@m.burg..nosp@m.de

Definition in file kernel_defines.h.

#include <stddef.h>
+ Include dependency graph for kernel_defines.h:
+ This graph shows which files directly or indirectly include this file:

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. More...
 
#define PURE
 The function has no effects except the return value and its return value depends only on the parameters and/or global variables. More...
 
#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. More...
 

Macro Definition Documentation

◆ ALIGN_OF

#define ALIGN_OF (   T)    (offsetof(struct { char c; T t; }, t))

Calculate the minimal alignment for type T.

Parameters
[in]TType to examine
Returns
The minimal alignment of T.

Definition at line 127 of file kernel_defines.h.

◆ ARRAY_SIZE

#define ARRAY_SIZE (   a)    (sizeof((a)) / sizeof((a)[0]))

Calculate the number of elements in a static array.

Parameters
[in]aArray to examine
Returns
The number of elements in the array a.

Definition at line 118 of file kernel_defines.h.

◆ BUILD_BUG_ON

#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

Parameters
[in]conditionA condition that will be evaluated at compile time

Definition at line 139 of file kernel_defines.h.

◆ CONST

#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.

Definition at line 82 of file kernel_defines.h.

◆ container_of

#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).

Parameters
[in]PTRpointer to a member
[in]TYPEa type name (a struct or union), container of PTR
[in]MEMBERname of the member of TYPE which PTR points to
Returns
Pointer to the container of PTR.

Definition at line 58 of file kernel_defines.h.

◆ PURE

#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.

Definition at line 95 of file kernel_defines.h.

◆ UNREACHABLE

#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 108 of file kernel_defines.h.