source: rtems/cpukit/score/include/rtems/score/basedefs.h @ 97eaefd4

5
Last change on this file since 97eaefd4 was 97eaefd4, checked in by Sebastian Huber <sebastian.huber@…>, on 10/12/16 at 08:44:51

score: Add RTEMS_OBFUSCATE_POINTER()

Update #2790.

  • Property mode set to 100644
File size: 10.0 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup Score
5 *
6 * @brief Basic Definitions
7 */
8
9/*
10 *  COPYRIGHT (c) 1989-2007.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  Copyright (c) 2010, 2016 embedded brains GmbH.
14 *
15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
17 *  http://www.rtems.org/license/LICENSE.
18 */
19
20#ifndef _RTEMS_BASEDEFS_H
21#define _RTEMS_BASEDEFS_H
22
23/**
24 *  @defgroup ScoreBaseDefs Basic Definitions
25 *
26 *  @ingroup Score
27 */
28/**@{*/
29
30#include <rtems/score/cpuopts.h>
31
32#ifndef ASM
33  #include <stddef.h>
34  #include <stdbool.h>
35  #include <stdint.h>
36
37  /*
38   * FIXME: This include should not be present.  In older RTEMS versions
39   * <rtems.h> provided <limits.h> indirectly.  This include is here to not
40   * break application source files that relied on this accidentally.
41   */
42  #include <limits.h>
43
44  /*
45   * FIXME: This include should not be present.  In older RTEMS versions
46   * <rtems.h> provided <string.h> indirectly.  This include is here to not
47   * break application source files that relied on this accidentally.
48   */
49  #include <string.h>
50#endif
51
52#ifndef TRUE
53  /**
54   *  This ensures that RTEMS has TRUE defined in all situations.
55   */
56  #define TRUE 1
57#endif
58
59#ifndef FALSE
60  /**
61   *  This ensures that RTEMS has FALSE defined in all situations.
62   */
63  #define FALSE 0
64#endif
65
66#if TRUE == FALSE
67  #error "TRUE equals FALSE"
68#endif
69
70/**
71 *  The following (in conjunction with compiler arguments) are used
72 *  to choose between the use of static inline functions and macro
73 *  functions.   The static inline implementation allows better
74 *  type checking with no cost in code size or execution speed.
75 */
76#ifdef __GNUC__
77  #define RTEMS_INLINE_ROUTINE static __inline__
78#else
79  #define RTEMS_INLINE_ROUTINE static inline
80#endif
81
82/**
83 *  The following macro is a compiler specific way to ensure that memory
84 *  writes are not reordered around certian points.  This specifically can
85 *  impact interrupt disable and thread dispatching critical sections.
86 */
87#ifdef __GNUC__
88  #define RTEMS_COMPILER_MEMORY_BARRIER() __asm__ volatile("" ::: "memory")
89#else
90  #define RTEMS_COMPILER_MEMORY_BARRIER()
91#endif
92
93/**
94 *  The following macro is a compiler specific way to indicate that
95 *  the method will NOT return to the caller.  This can assist the
96 *  compiler in code generation and avoid unreachable paths.  This
97 *  can impact the code generated following calls to
98 *  rtems_fatal_error_occurred and _Terminate.
99 */
100#if defined(RTEMS_SCHEDSIM)
101  #define RTEMS_NO_RETURN
102#elif defined(__GNUC__) && !defined(RTEMS_DEBUG)
103  #define RTEMS_NO_RETURN __attribute__((__noreturn__))
104#else
105  #define RTEMS_NO_RETURN
106#endif
107
108/* Provided for backward compatibility */
109#define RTEMS_COMPILER_NO_RETURN_ATTRIBUTE RTEMS_NO_RETURN
110
111/**
112 *  The following defines a compiler specific attribute which informs
113 *  the compiler that the method has no effect except the return value
114 *  and that the return value depends only on parameters and/or global
115 *  variables.
116 */
117#ifdef __GNUC__
118  #define RTEMS_PURE __attribute__((__pure__))
119#else
120  #define RTEMS_PURE
121#endif
122
123/* Provided for backward compatibility */
124#define RTEMS_COMPILER_PURE_ATTRIBUTE RTEMS_PURE
125
126/**
127 *  Instructs the compiler to issue a warning whenever a variable or function
128 *  with this attribute will be used.
129 */
130#ifdef __GNUC__
131  #define RTEMS_DEPRECATED __attribute__((__deprecated__))
132#else
133  #define RTEMS_DEPRECATED
134#endif
135
136/* Provided for backward compatibility */
137#define RTEMS_COMPILER_DEPRECATED_ATTRIBUTE RTEMS_DEPRECATED
138
139/**
140 * @brief Instructs the compiler to place a specific variable or function in
141 * the specified section.
142 */
143#if defined(__GNUC__)
144  #define RTEMS_SECTION( _section ) __attribute__((__section__(_section)))
145#else
146  #define RTEMS_SECTION( _section )
147#endif
148
149/**
150 * @brief Instructs the compiler that a specific variable or function is used.
151 */
152#if defined(__GNUC__)
153  #define RTEMS_USED __attribute__((__used__))
154#else
155  #define RTEMS_USED
156#endif
157
158/**
159 *  Instructs the compiler that a specific variable is deliberately unused.
160 *  This can occur when reading volatile device memory or skipping arguments
161 *  in a variable argument method.
162 */
163#if defined(__GNUC__)
164  #define RTEMS_UNUSED __attribute__((__unused__))
165#else
166  #define RTEMS_UNUSED
167#endif
168
169/* Provided for backward compatibility */
170#define RTEMS_COMPILER_UNUSED_ATTRIBUTE RTEMS_UNUSED
171
172/**
173 *  Instructs the compiler that a specific structure or union members will be
174 *  placed so that the least memory is used.
175 */
176#if defined(__GNUC__)
177  #define RTEMS_PACKED __attribute__((__packed__))
178#else
179  #define RTEMS_PACKED
180#endif
181
182/**
183 * @brief Instructs the compiler to enforce the specified alignment.
184 */
185#if defined(__GNUC__)
186  #define RTEMS_ALIGNED( _alignment ) __attribute__((__aligned__(_alignment)))
187#else
188  #define RTEMS_ALIGNED( _alignment )
189#endif
190
191/* Provided for backward compatibility */
192#define RTEMS_COMPILER_PACKED_ATTRIBUTE RTEMS_PACKED
193
194#if defined(RTEMS_DEBUG) && !defined(RTEMS_SCHEDSIM)
195  #define _Assert_Unreachable() _Assert( 0 )
196#else
197  #define _Assert_Unreachable() do { } while ( 0 )
198#endif
199
200/**
201 * @brief Tells the compiler that this program point is unreachable.
202 */
203#if defined(__GNUC__) && !defined(RTEMS_SCHEDSIM)
204  #define RTEMS_UNREACHABLE() \
205    do { \
206      __builtin_unreachable(); \
207      _Assert_Unreachable(); \
208    } while ( 0 )
209#else
210  #define RTEMS_UNREACHABLE() _Assert_Unreachable()
211#endif
212
213/**
214 * @brief Tells the compiler that this function expects printf()-like
215 * arguments.
216 */
217#if defined(__GNUC__)
218  #define RTEMS_PRINTFLIKE( _format_pos, _ap_pos ) \
219    __attribute__((__format__(__printf__, _format_pos, _ap_pos)))
220#else
221  #define RTEMS_PRINTFLIKE( _format_pos, _ap_pos )
222#endif
223
224/**
225 * @brief Obfuscates the pointer so that the compiler cannot perform
226 * optimizations based on the pointer value.
227 */
228#if defined(__GNUC__)
229  #define RTEMS_OBFUSCATE_POINTER( _ptr ) __asm__("" : "+r" (_ptr))
230#else
231  #define RTEMS_OBFUSCATE_POINTER( _ptr ) (void) (_ptr)
232#endif
233
234#if __cplusplus >= 201103L
235  #define RTEMS_STATIC_ASSERT(cond, msg) \
236    static_assert(cond, # msg)
237#elif __STDC_VERSION__ >= 201112L
238  #define RTEMS_STATIC_ASSERT(cond, msg) \
239    _Static_assert(cond, # msg)
240#else
241  #define RTEMS_STATIC_ASSERT(cond, msg) \
242    typedef int rtems_static_assert_ ## msg [(cond) ? 1 : -1]
243#endif
244
245#define RTEMS_ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
246
247/*
248 * Zero-length arrays are valid in C99 as flexible array members.  C++11
249 * doesn't allow flexible array members.  Use the GNU extension which is also
250 * supported by other compilers.
251 */
252#define RTEMS_ZERO_LENGTH_ARRAY 0
253
254/**
255 * @brief Returns a pointer to the container of a specified member pointer.
256 *
257 * @param[in] _m The pointer to a member of the container.
258 * @param[in] _type The type of the container.
259 * @param[in] _member_name The designator name of the container member.
260 */
261#define RTEMS_CONTAINER_OF( _m, _type, _member_name ) \
262  ( (_type *) ( (uintptr_t) ( _m ) - offsetof( _type, _member_name ) ) )
263
264#ifdef __cplusplus
265#define RTEMS_DEQUALIFY_DEPTHX( _ptr_level, _type, _var ) \
266            (const_cast<_type>( _var ))
267#else /* Standard C code */
268
269/* The reference type idea based on libHX by Jan Engelhardt */
270#define RTEMS_TYPEOF_REFX(_ptr_level, _ptr_type) \
271  typeof(_ptr_level(union { int z; typeof(_ptr_type) x; }){0}.x)
272
273#if defined(__GNUC__) && !defined(ASM)
274#if  ((__GNUC__ * 1000 + __GNUC_MINOR__) >= 4004)
275extern void* RTEMS_DEQUALIFY_types_not_compatible(void)
276  __attribute__((error ("RTEMS_DEQUALIFY types differ not only by volatile and const")));
277#else
278extern void RTEMS_DEQUALIFY_types_not_compatible(void);
279#endif
280#define RTEMS_DEQUALIFY_DEPTHX( _ptr_level, _type, _var ) ( \
281    __builtin_choose_expr( __builtin_types_compatible_p ( \
282        RTEMS_TYPEOF_REFX( _ptr_level, _var ), \
283        RTEMS_TYPEOF_REFX( _ptr_level, _type ) \
284      ) || __builtin_types_compatible_p ( _type, void * ), \
285    (_type)(_var), \
286    RTEMS_DEQUALIFY_types_not_compatible() \
287  ) \
288)
289#endif /*__GNUC__*/
290#endif /*__cplusplus*/
291
292#ifndef RTEMS_DECONST
293#ifdef RTEMS_DEQUALIFY_DEPTHX
294#define RTEMS_DECONST( _type, _var ) \
295  RTEMS_DEQUALIFY_DEPTHX( *, _type, _var )
296#else /*RTEMS_DEQUALIFY_DEPTHX*/
297/**
298 * @brief Removes the const qualifier from a type of a variable.
299 *
300 * @param[in] _type The target type for the variable.
301 * @param[in] _var The variable.
302 */
303#define RTEMS_DECONST( _type, _var ) \
304  ((_type)(uintptr_t)(const void *) ( _var ))
305
306#endif /*RTEMS_DEQUALIFY_DEPTHX*/
307#endif /*RTEMS_DECONST*/
308
309#ifndef RTEMS_DEVOLATILE
310#ifdef RTEMS_DEQUALIFY_DEPTHX
311#define RTEMS_DEVOLATILE( _type, _var ) \
312  RTEMS_DEQUALIFY_DEPTHX( *, _type, _var )
313#else /*RTEMS_DEQUALIFY_DEPTHX*/
314/**
315 * @brief Removes the volatile qualifier from a type of a variable.
316 *
317 * @param[in] _type The target type for the variable.
318 * @param[in] _var The variable.
319 */
320#define RTEMS_DEVOLATILE( _type, _var ) \
321  ((_type)(uintptr_t)(volatile void *) ( _var ))
322
323#endif /*RTEMS_DEQUALIFY_DEPTHX*/
324#endif /*RTEMS_DEVOLATILE*/
325
326#ifndef RTEMS_DEQUALIFY
327#ifdef RTEMS_DEQUALIFY_DEPTHX
328#define RTEMS_DEQUALIFY( _type, _var ) \
329  RTEMS_DEQUALIFY_DEPTHX( *, _type, _var )
330#else /*RTEMS_DEQUALIFY_DEPTHX*/
331/**
332 * @brief Removes the all qualifiers from a type of a variable.
333 *
334 * @param[in] _type The target type for the variable.
335 * @param[in] _var The variable.
336 */
337#define RTEMS_DEQUALIFY( _type, _var ) \
338  ((_type)(uintptr_t)(const volatile void *) ( _var ))
339
340#endif /*RTEMS_DEQUALIFY_DEPTHX*/
341#endif /*RTEMS_DEQUALIFY*/
342
343/**
344 * @brief Concatenates _x and _y without expanding.
345 */
346#define RTEMS_CONCAT( _x, _y ) _x##_y
347
348/**
349 * @brief Concatenates expansion of _x and expansion of _y.
350 */
351#define RTEMS_XCONCAT( _x, _y ) RTEMS_CONCAT( _x, _y )
352
353/**
354 * @brief Stringifies _x  without expanding.
355 */
356#define RTEMS_STRING( _x ) #_x
357
358/**
359 * @brief Stringifies expansion of _x.
360 */
361#define RTEMS_XSTRING( _x ) RTEMS_STRING( _x )
362
363#ifndef ASM
364  #ifdef RTEMS_DEPRECATED_TYPES
365    typedef bool boolean;
366    typedef float single_precision;
367    typedef double double_precision;
368  #endif
369
370  /**
371   * XXX: Eventually proc_ptr needs to disappear!!!
372   */
373  typedef void * proc_ptr;
374#endif
375
376/**@}*/
377
378#endif /* _RTEMS_BASEDEFS_H */
Note: See TracBrowser for help on using the repository browser.