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))
35#define NORETURN __attribute__((noreturn))
37#define PURE __attribute__((pure))
40#define CONST __attribute__((const))
42#define ALWAYS_INLINE __attribute__((always_inline))
44#define NEVER_INLINE __attribute__((noinline))
46#define ALIGN(x) __attribute__((aligned(x)))
48#define PACKED __attribute__((packed))
50#define MAY_ALIAS __attribute__((may_alias))
52#define CONSTRUCTOR __attribute__((constructor))
54#define LIKELY(exp) __builtin_expect(!!(exp), 1)
56#define UNLIKELY(exp) __builtin_expect(!!(exp), 0)
58#define UNREACHABLE __builtin_unreachable()
60#define FORMAT(type, idx, first) __attribute__((format(type, idx, first)))
62#define IS_CONSTANT(x) __builtin_constant_p(x)
64#ifdef __MACH__ // Mach targets have special section specifications.
65#define SECTION(x)
66#else
67#define SECTION(x) __attribute__((__section__(x)))
68#endif
70#define MUST_USE_RESULT __attribute__((warn_unused_result))
72#define USED __attribute__((used))
74#define WEAK __attribute__((weak))
76#if defined(__GNUC__) && __GNUC__ >= 7
77#define FALLSTHROUGH __attribute__((fallthrough));
78#else
79#define FALLSTHROUGH
80#endif
83#define MALLOC __attribute__((malloc))
85#define ALLOC_SIZE(...) __attribute__((alloc_size(__VA_ARGS__)))
87#define C_NOTHROW __attribute__((nothrow))
89#define EMIT_IF(...) if constexpr (__VA_ARGS__)
90
95#define EXPORTED_PUBLIC __attribute__((visibility("default")))
96#define SYMBOL_HIDDEN __attribute__((visibility("hidden")))
97
98// Builtin checks.
99#ifndef __has_builtin
100#define __has_builtin(x) 0
101#endif
102
103#ifndef __has_feature
104#define __has_feature(x) 0
105#endif
106
107#if __has_builtin(__builtin_assume_aligned) || !defined(__clang__)
108#define ASSUME_ALIGNMENT(b, sz) __builtin_assume_aligned((b), sz)
109#else
110#define ASSUME_ALIGNMENT(b, sz) \
111 ((reinterpret_cast<uintptr_t>(b) % (sz)) == 0) ? (b) : (UNREACHABLE, (b))
112#endif
113
116#define INITIALISATION_ONLY SECTION(".init.text")
117#define INITIALISATION_ONLY_DATA SECTION(".init.data")
118
119// Detect sanitizer builds so hosted signal and memory primitives can defer to
120// the runtime where necessary.
121#if defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer) || \
122 __has_feature(memory_sanitizer)
123#define HAS_ADDRESS_SANITIZER 1
124#else
125#define HAS_ADDRESS_SANITIZER 0
126#endif
127
128#if defined(__SANITIZE_THREAD__) || __has_feature(thread_sanitizer)
129#define HAS_THREAD_SANITIZER 1
130#else
131#define HAS_THREAD_SANITIZER 0
132#endif
133
134#if HAS_ADDRESS_SANITIZER || HAS_THREAD_SANITIZER
135#define HAS_SANITIZERS 1
136#endif
137
138#ifdef __cplusplus
139#define NOT_COPY_CONSTRUCTIBLE(Type) Type(const Type&) = delete
140#define NOT_ASSIGNABLE(Type) Type& operator=(const Type&) = delete
141#define NOT_COPYABLE_OR_ASSIGNABLE(Type) \
142 NOT_COPY_CONSTRUCTIBLE(Type); \
143 NOT_ASSIGNABLE(Type)
144
145#define WITHOUT_IMPLICIT_CONSTRUCTORS(Type) \
146 Type() = delete; \
147 NOT_COPYABLE_OR_ASSIGNABLE(Type)
148#endif
149
150// BARRIER forces GCC to not reorder code across the barrier.
151#define BARRIER() __asm__ __volatile__("" ::: "memory")
152
153// FENCE performs a full load/store hardware memory fence.
154#define FENCE() __sync_synchronize()
155
156// Export thread-safety annotations if we're being compiled by clang
157#include "pedigree/kernel/utilities/threadsafety.h" // IWYU: export
158
161#endif