The Pedigree Project  0.1
system/include/pedigree/kernel/compiler.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef KERNEL_COMPILER_H
21 #define KERNEL_COMPILER_H
22 
26 // NOTE: See http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
27 // http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html
28 // http://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
29 // http://www.imodulo.com/gnu/gcc/Other-Builtins.html
30 // for more information.
31 
33 #define DEPRECATED __attribute__((deprecated))
34 
35 #define NORETURN __attribute__((noreturn))
36 
37 #define PURE __attribute__((pure))
38 
40 #define CONST __attribute__((const))
41 
42 #define ALWAYS_INLINE __attribute__((always_inline))
43 
44 #define NEVER_INLINE __attribute__((noinline))
45 
46 #define ALIGN(x) __attribute__((aligned(x)))
47 
48 #define PACKED __attribute__((packed))
49 
50 #define MAY_ALIAS __attribute__((may_alias))
51 
52 #define LIKELY(exp) __builtin_expect(!!(exp), 1)
53 
54 #define UNLIKELY(exp) __builtin_expect(!!(exp), 0)
55 
56 #define UNREACHABLE __builtin_unreachable()
57 
58 #define FORMAT(type, idx, first) __attribute__((format(type, idx, first)))
59 
60 #define IS_CONSTANT(x) __builtin_constant_p(x)
61 
62 #ifdef __MACH__ // Mach targets have special section specifications.
63 #define SECTION(x)
64 #else
65 #define SECTION(x) __attribute__((__section__(x)))
66 #endif
67 
68 #define MUST_USE_RESULT __attribute__((warn_unused_result))
69 
70 #define USED __attribute__((used))
71 
72 #define WEAK __attribute__((weak))
73 
78 #define EXPORTED_PUBLIC __attribute__((visibility("default")))
79 
80 // Builtin checks.
81 #ifndef __has_builtin
82 #define __has_builtin(x) 0
83 #endif
84 
85 #ifndef __has_feature
86 #define __has_feature(x) 0
87 #endif
88 
89 #if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
90 #define ASSUME_ALIGNMENT(b, sz) __builtin_assume_aligned((b), sz)
91 #else
92 #define ASSUME_ALIGNMENT(b, sz) \
93  ((reinterpret_cast<uintptr_t>(b) % (sz)) == 0) ? (b) : (UNREACHABLE, (b))
94 #endif
95 
98 #define INITIALISATION_ONLY SECTION(".init.text")
99 #define INITIALISATION_ONLY_DATA SECTION(".init.data")
100 
101 // We don't use a custom allocator if asan is enabled.
102 #if __has_feature(address_sanitizer) || __has_feature(memory_sanitizer)
103 #define HAS_ADDRESS_SANITIZER 1
104 #else
105 #define HAS_ADDRESS_SANITIZER 0
106 #endif
107 
108 #if __has_feature(thread_sanitizer)
109 #define HAS_THREAD_SANITIZER 1
110 #else
111 #define HAS_THREAD_SANITIZER 0
112 #endif
113 
114 #if HAS_ADDRESS_SANITIZER || HAS_THREAD_SANITIZER
115 #define HAS_SANITIZERS 1
116 #endif
117 
118 #ifdef __cplusplus
119 #define NOT_COPY_CONSTRUCTIBLE(Type) Type(const Type &) = delete
120 #define NOT_ASSIGNABLE(Type) Type &operator=(const Type &) = delete
121 #define NOT_COPYABLE_OR_ASSIGNABLE(Type) \
122  NOT_COPY_CONSTRUCTIBLE(Type); \
123  NOT_ASSIGNABLE(Type)
124 
125 #define WITHOUT_IMPLICIT_CONSTRUCTORS(Type) \
126  Type() = delete; \
127  NOT_COPYABLE_OR_ASSIGNABLE(Type)
128 #endif
129 
130 // BARRIER forces GCC to not reorder code across the barrier.
131 #define BARRIER() __asm__ __volatile__("" ::: "memory")
132 
133 // FENCE performs a full load/store hardware memory fence.
134 #define FENCE() __sync_synchronize()
135 
136 // Export thread-safety annotations if we're being compiled by clang
137 #include "pedigree/kernel/utilities/threadsafety.h" // IWYU: export
138 
141 #endif