Blame view

RIOT/core/include/kernel_defines.h 3.63 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  /*
   * Copyright (C) 2014 Freie Universität Berlin
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @addtogroup  core_internal
   * @{
   *
   * @file
   * @brief       Common macros and compiler attributes/pragmas configuration
   *
   * @author      René Kijewski <rene.kijewski@fu-berlin.de>
   */
  
  #ifndef KERNEL_DEFINES_H
  #define KERNEL_DEFINES_H
  
  #include <stddef.h>
  
  #ifdef __cplusplus
   extern "C" {
  #endif
  
  /**
   * @def         container_of(PTR, TYPE, MEMBER)
   * @brief       Returns the container of a pointer to a member.
   * @details     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`.
   * @details     E.g. for `struct my_struct_t { ...; something_t n; ... } my_struct;`,
   *              `&my_struct == container_of(&my_struct.n, struct my_struct_t, n)`.
   * @param[in]   PTR      pointer to a member
   * @param[in]   TYPE     a type name (a struct or union), container of PTR
   * @param[in]   MEMBER   name of the member of TYPE which PTR points to
   * @return      Pointer to the container of PTR.
   */
  #if __STDC_VERSION__ >= 201112L
  #   define container_of(PTR, TYPE, MEMBER) \
          (_Generic((PTR), \
              const __typeof__ (((TYPE *) 0)->MEMBER) *: \
                  ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))), \
              __typeof__ (((TYPE *) 0)->MEMBER) *: \
                  ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER))) \
          ))
  #elif defined __GNUC__
  #   define container_of(PTR, TYPE, MEMBER) \
          (__extension__ ({ \
              __extension__ const __typeof__ (((TYPE *) 0)->MEMBER) *__m____ = (PTR); \
              ((TYPE *) ((char *) __m____ - offsetof(TYPE, MEMBER))); \
          }))
  #else
  #   define container_of(PTR, TYPE, MEMBER) \
          ((TYPE *) ((char *) (PTR) - offsetof(TYPE, MEMBER)))
  #endif
  
  /**
   * @def NORETURN
   * @brief The *NORETURN* keyword tells the compiler to assume that the function
   *        cannot return.
   */
  #ifdef __GNUC__
  #define NORETURN  __attribute__((noreturn))
  #else
  #define NORETURN
  #endif
  
  /**
   * @def CONST
   * @brief 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.
   */
  #ifdef __GNUC__
  #define CONST  __attribute__((const))
  #else
  #define CONST
  #endif
  
  /**
   * @def PURE
   * @brief 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.
   */
  #ifdef __GNUC__
  #define PURE  __attribute__((pure))
  #else
  #define PURE
  #endif
  
  /**
   * @def       UNREACHABLE()
   * @brief     Tell the compiler that this line of code cannot be reached.
   * @details   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.
   */
  #if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ >= 5)
  #define UNREACHABLE() __builtin_unreachable()
  #else
  #define UNREACHABLE() do { /* nothing */ } while (1)
  #endif
  
  /**
   * @def         ALIGN_OF(T)
   * @brief       Calculate the minimal alignment for type T.
   * @param[in]   T   Type to examine
   * @returns     The minimal alignment of T.
   */
  #define ALIGN_OF(T) (offsetof(struct { char c; T t; }, t))
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* KERNEL_DEFINES_H */
  /** @} */