My Kernel v0.1.0
compiler.h
1
13#ifndef UTILS_COMPILER_H
14#define UTILS_COMPILER_H
15
16#include "stringify.h"
17
19#define static_assert(cond, ...) \
20 _Static_assert(cond, stringify(cond) __VA_OPT__(": ") __VA_ARGS__)
21
22#define assert_not_reached() \
23 do { \
24 log_warn("unreachable code reached: %s:%d", __FILE__, __LINE__); \
25 __builtin_unreachable(); \
26 } while (0)
27
28#define ASM __asm__ volatile
29
30#define ALWAYS_INLINE __attribute__((always_inline)) inline
31#define PACKED __attribute__((__packed__))
32#define SECTION(_section) __attribute__((section(_section)))
33#define NO_DISCARD __attribute__((warn_unused_result))
34#define MAYBE_UNUSED __attribute__((unused))
35#define ALIAS(_function) __attribute__((alias(_function)))
36#define ALIGNED(_alignment) __attribute__((aligned(_alignment)))
37#define FORMAT(_type, _fmt, _args) __attribute__((format(_type, _fmt, _args)))
38#define NO_RETURN __attribute__((noreturn))
39#define CLEANUP(_dtor) __attribute__((cleanup(_dtor)))
40#define PURE __attribute__((pure))
41
45#define NON_ZERO(_x) (1 + sizeof(struct { char size[(_x)-1]; }))
46
47#define likely(x) __builtin_expect(!!(x), 1)
48#define unlikely(x) __builtin_expect(!!(x), 0)
49
50#ifndef typeof
51#define typeof __typeof__
52#endif
53
55#define is_native_word(_x) \
56 (sizeof(_x) == 1 || sizeof(_x) == 2 || sizeof(_x) == 4)
57
61#define same_type(_a, _b) __builtin_types_compatible_p(typeof(_a), typeof(_b))
62
63#define same_size(_a, _b) (sizeof(_a) == sizeof(_b))
64
65#endif /* UTILS_COMPILER_H */