2012-12-24 15:36:14 +04:00
|
|
|
#ifndef __CR_COMPILER_H__
|
|
|
|
#define __CR_COMPILER_H__
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Various definitions for success build,
|
|
|
|
* picked from various places, mostly from
|
|
|
|
* the linux kernel.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
2021-07-19 07:39:51 +00:00
|
|
|
#define NELEMS_AS_ARRAY(x, y) (sizeof(x) / sizeof((y)[0]))
|
|
|
|
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2021-07-19 07:39:51 +00:00
|
|
|
#define ASSIGN_TYPED(a, b) \
|
|
|
|
do { \
|
|
|
|
(a) = (typeof(a))(b); \
|
|
|
|
} while (0)
|
|
|
|
#define ASSIGN_MEMBER(a, b, m) \
|
|
|
|
do { \
|
|
|
|
ASSIGN_TYPED((a)->m, (b)->m); \
|
|
|
|
} while (0)
|
2016-06-17 17:47:00 +03:00
|
|
|
|
2021-07-19 07:39:51 +00:00
|
|
|
#define __stringify_1(x...) #x
|
|
|
|
#define __stringify(x...) __stringify_1(x)
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2021-07-19 07:39:51 +00:00
|
|
|
#define NORETURN __attribute__((__noreturn__))
|
|
|
|
#define __packed __attribute__((__packed__))
|
|
|
|
#define __used __attribute__((__used__))
|
|
|
|
#define __maybe_unused __attribute__((unused))
|
|
|
|
#define __always_unused __attribute__((unused))
|
|
|
|
#define __must_check __attribute__((__warn_unused_result__))
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2022-02-14 11:47:30 +00:00
|
|
|
#ifndef __has_attribute
|
|
|
|
#define __has_attribute(x) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Not supported by clang */
|
|
|
|
#if __has_attribute(__externally_visible__)
|
|
|
|
#define __visible __attribute__((__externally_visible__))
|
|
|
|
#else
|
|
|
|
#define __visible
|
|
|
|
#endif
|
|
|
|
|
2021-07-19 07:39:51 +00:00
|
|
|
#define __section(S) __attribute__((__section__(#S)))
|
2011-09-23 12:00:45 +04:00
|
|
|
|
|
|
|
#ifndef __always_inline
|
2021-07-19 07:39:51 +00:00
|
|
|
#define __always_inline inline __attribute__((always_inline))
|
2011-09-23 12:00:45 +04:00
|
|
|
#endif
|
|
|
|
|
2021-07-19 07:39:51 +00:00
|
|
|
#define likely(x) __builtin_expect(!!(x), 1)
|
|
|
|
#define unlikely(x) __builtin_expect(!!(x), 0)
|
2012-02-24 15:12:38 +04:00
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
#ifndef always_inline
|
2021-07-19 07:39:51 +00:00
|
|
|
#define always_inline __always_inline
|
2011-09-23 12:00:45 +04:00
|
|
|
#endif
|
|
|
|
|
2012-02-13 21:37:18 +04:00
|
|
|
#ifndef noinline
|
2021-07-19 07:39:51 +00:00
|
|
|
#define noinline __attribute__((noinline))
|
2012-02-13 21:37:18 +04:00
|
|
|
#endif
|
|
|
|
|
2022-06-20 20:36:28 +02:00
|
|
|
#ifndef __aligned
|
2021-07-19 07:39:51 +00:00
|
|
|
#define __aligned(x) __attribute__((aligned(x)))
|
2022-06-20 20:36:28 +02:00
|
|
|
#endif
|
2011-11-12 19:26:40 +04:00
|
|
|
|
2015-12-30 19:56:00 +03:00
|
|
|
/*
|
2021-04-22 17:48:08 -07:00
|
|
|
* Macro to define stack alignment.
|
2015-12-30 19:56:00 +03:00
|
|
|
* aarch64 requires stack to be aligned to 16 bytes.
|
|
|
|
*/
|
2021-07-19 07:39:51 +00:00
|
|
|
#define __stack_aligned__ __attribute__((aligned(16)))
|
2015-12-30 19:56:00 +03:00
|
|
|
|
2011-09-23 12:00:45 +04:00
|
|
|
#ifndef offsetof
|
2021-07-19 07:39:51 +00:00
|
|
|
#define offsetof(TYPE, MEMBER) ((size_t) & ((TYPE *)0)->MEMBER)
|
2011-09-23 12:00:45 +04:00
|
|
|
#endif
|
|
|
|
|
2021-07-19 07:39:51 +00:00
|
|
|
#define barrier() asm volatile("" ::: "memory")
|
2012-02-13 21:37:18 +04:00
|
|
|
|
2021-07-19 07:39:51 +00:00
|
|
|
#define container_of(ptr, type, member) \
|
|
|
|
({ \
|
|
|
|
const typeof(((type *)0)->member) *__mptr = (ptr); \
|
|
|
|
(type *)((char *)__mptr - offsetof(type, member)); \
|
|
|
|
})
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2018-07-19 15:47:30 +03:00
|
|
|
#ifndef FIELD_SIZEOF
|
2021-07-19 07:39:51 +00:00
|
|
|
#define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f))
|
2018-07-19 15:47:30 +03:00
|
|
|
#endif
|
|
|
|
|
2021-07-19 07:39:51 +00:00
|
|
|
#define __round_mask(x, y) ((__typeof__(x))((y)-1))
|
|
|
|
#define round_up(x, y) ((((x)-1) | __round_mask(x, y)) + 1)
|
|
|
|
#define round_down(x, y) ((x) & ~__round_mask(x, y))
|
|
|
|
#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
|
|
|
|
#define ALIGN(x, a) (((x) + (a)-1) & ~((a)-1))
|
2022-05-25 20:04:59 +03:00
|
|
|
#define ALIGN_DOWN(x, a) ALIGN((x) - ((a) - 1), (a))
|
2021-07-19 07:39:51 +00:00
|
|
|
|
|
|
|
#define min(x, y) \
|
|
|
|
({ \
|
|
|
|
typeof(x) _min1 = (x); \
|
|
|
|
typeof(y) _min2 = (y); \
|
|
|
|
(void)(&_min1 == &_min2); \
|
|
|
|
_min1 < _min2 ? _min1 : _min2; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define max(x, y) \
|
|
|
|
({ \
|
|
|
|
typeof(x) _max1 = (x); \
|
|
|
|
typeof(y) _max2 = (y); \
|
|
|
|
(void)(&_max1 == &_max2); \
|
|
|
|
_max1 > _max2 ? _max1 : _max2; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define min_t(type, x, y) \
|
|
|
|
({ \
|
|
|
|
type __min1 = (x); \
|
|
|
|
type __min2 = (y); \
|
|
|
|
__min1 < __min2 ? __min1 : __min2; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define max_t(type, x, y) \
|
|
|
|
({ \
|
|
|
|
type __max1 = (x); \
|
|
|
|
type __max2 = (y); \
|
|
|
|
__max1 > __max2 ? __max1 : __max2; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define SWAP(x, y) \
|
|
|
|
do { \
|
|
|
|
typeof(x) ____val = x; \
|
|
|
|
x = y; \
|
|
|
|
y = ____val; \
|
2017-06-28 18:54:07 +03:00
|
|
|
} while (0)
|
|
|
|
|
2021-07-19 07:39:51 +00:00
|
|
|
#define is_log2(v) (((v) & ((v)-1)) == 0)
|
2011-09-23 12:00:45 +04:00
|
|
|
|
2019-11-09 22:20:45 +00:00
|
|
|
/*
|
|
|
|
* Use "__ignore_value" to avoid a warning when using a function declared with
|
|
|
|
* gcc's warn_unused_result attribute, but for which you really do want to
|
|
|
|
* ignore the result. Traditionally, people have used a "(void)" cast to
|
|
|
|
* indicate that a function's return value is deliberately unused. However,
|
|
|
|
* if the function is declared with __attribute__((warn_unused_result)),
|
|
|
|
* gcc issues a warning even with the cast.
|
|
|
|
*
|
|
|
|
* Caution: most of the time, you really should heed gcc's warning, and
|
|
|
|
* check the return value. However, in those exceptional cases in which
|
|
|
|
* you're sure you know what you're doing, use this function.
|
|
|
|
*
|
|
|
|
* Normally casting an expression to void discards its value, but GCC
|
|
|
|
* versions 3.4 and newer have __attribute__ ((__warn_unused_result__))
|
|
|
|
* which may cause unwanted diagnostics in that case. Use __typeof__
|
|
|
|
* and __extension__ to work around the problem, if the workaround is
|
|
|
|
* known to be needed.
|
|
|
|
* Written by Jim Meyering, Eric Blake and Pádraig Brady.
|
|
|
|
* (See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 for the details)
|
|
|
|
*/
|
|
|
|
#if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
|
2021-07-19 07:39:51 +00:00
|
|
|
#define __ignore_value(x) \
|
|
|
|
({ \
|
|
|
|
__typeof__(x) __x = (x); \
|
|
|
|
(void)__x; \
|
|
|
|
})
|
2019-11-09 22:20:45 +00:00
|
|
|
#else
|
2021-07-19 07:39:51 +00:00
|
|
|
#define __ignore_value(x) ((void)(x))
|
2019-11-09 22:20:45 +00:00
|
|
|
#endif
|
|
|
|
|
2012-12-25 22:40:24 +04:00
|
|
|
#endif /* __CR_COMPILER_H__ */
|