1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #ifndef __ASSEMBLER__
8 
9 #if !defined(__GNUC__) && !defined(__clang__)
10 #error "Unrecognized compiler!"
11 #endif
12 
13 #define likely(x)       __builtin_expect(!!(x), 1)
14 #define unlikely(x)     __builtin_expect(!!(x), 0)
15 #define __UNUSED __attribute__((__unused__))
16 #define __USED __attribute__((__used__))
17 #define __PACKED __attribute__((packed))
18 #define __ALIGNED(x) __attribute__((aligned(x)))
19 #define __PRINTFLIKE(__fmt,__varargs) __attribute__((__format__ (__printf__, __fmt, __varargs)))
20 #define __SCANFLIKE(__fmt,__varargs) __attribute__((__format__ (__scanf__, __fmt, __varargs)))
21 #define __SECTION(x) __attribute__((__section__(x)))
22 #define __PURE __attribute__((__pure__))
23 #define __CONST __attribute__((__const__))
24 #define __NO_RETURN __attribute__((__noreturn__))
25 #define __MALLOC __attribute__((__malloc__))
26 #define __WEAK __attribute__((__weak__))
27 #define __GNU_INLINE __attribute__((__gnu_inline__))
28 #define __GET_CALLER(x) __builtin_return_address(0)
29 #define __GET_FRAME(x) __builtin_frame_address(0)
30 #define __NAKED __attribute__((__naked__))
31 #define __ISCONSTANT(x) __builtin_constant_p(x)
32 #define __NO_INLINE __attribute__((__noinline__))
33 #define __SRAM __NO_INLINE __SECTION(".sram.text")
34 #define __CONSTRUCTOR __attribute__((__constructor__))
35 #define __DESTRUCTOR __attribute__((__destructor__))
36 #define __RESTRICT __restrict
37 
38 #ifndef __clang__
39 #define __LEAF_FN __attribute__((__leaf__))
40 #define __OPTIMIZE(x) __attribute__((__optimize__(x)))
41 #define __EXTERNALLY_VISIBLE __attribute__((__externally_visible__))
42 #define __THREAD_ANNOTATION(x)
43 #define __NO_SAFESTACK
44 #else
45 #define __LEAF_FN
46 #define __OPTIMIZE(x)
47 #define __EXTERNALLY_VISIBLE
48 #define __THREAD_ANNOTATION(x) __attribute__((x))
49 #define __NO_SAFESTACK __attribute__((__no_sanitize__("safe-stack")))
50 #endif
51 
52 #ifndef __has_feature
53 #define __has_feature(x) 0
54 #endif
55 
56 #define __ALWAYS_INLINE __attribute__((__always_inline__))
57 #define __MAY_ALIAS __attribute__((__may_alias__))
58 #define __NONNULL(x) __attribute__((__nonnull__ x))
59 #define __WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
60 #define __UNREACHABLE __builtin_unreachable()
61 #define __WEAK_ALIAS(x) __attribute__((__weak__, __alias__(x)))
62 #define __ALIAS(x) __attribute__((__alias__(x)))
63 #define __EXPORT __attribute__ ((__visibility__("default")))
64 #define __LOCAL  __attribute__ ((__visibility__("hidden")))
65 #define __THREAD __thread
66 #define __offsetof(type, field) __builtin_offsetof(type, field)
67 
68 #if defined(__cplusplus) && __cplusplus >= 201703L
69 #define __FALLTHROUGH [[fallthrough]]
70 #elif defined(__cplusplus) && defined(__clang__)
71 #define __FALLTHROUGH [[clang::fallthrough]]
72 #elif __GNUC__ >= 7
73 #define __FALLTHROUGH __attribute__((__fallthrough__))
74 #else
75 #define __FALLTHROUGH do {} while (0)
76 #endif
77 
78 // Publicly exposed thread annotation macros. These have a long and ugly name to
79 // minimize the chance of collision with consumers of Zircon's public headers.
80 #define __TA_CAPABILITY(x) __THREAD_ANNOTATION(__capability__(x))
81 #define __TA_GUARDED(x) __THREAD_ANNOTATION(__guarded_by__(x))
82 #define __TA_ACQUIRE(...) __THREAD_ANNOTATION(__acquire_capability__(__VA_ARGS__))
83 #define __TA_TRY_ACQUIRE(...) __THREAD_ANNOTATION(__try_acquire_capability__(__VA_ARGS__))
84 #define __TA_ACQUIRED_BEFORE(...) __THREAD_ANNOTATION(__acquired_before__(__VA_ARGS__))
85 #define __TA_ACQUIRED_AFTER(...) __THREAD_ANNOTATION(__acquired_after__(__VA_ARGS__))
86 #define __TA_RELEASE(...) __THREAD_ANNOTATION(__release_capability__(__VA_ARGS__))
87 #define __TA_REQUIRES(...) __THREAD_ANNOTATION(__requires_capability__(__VA_ARGS__))
88 #define __TA_EXCLUDES(...) __THREAD_ANNOTATION(__locks_excluded__(__VA_ARGS__))
89 #define __TA_RETURN_CAPABILITY(x) __THREAD_ANNOTATION(__lock_returned__(x))
90 #define __TA_SCOPED_CAPABILITY __THREAD_ANNOTATION(__scoped_lockable__)
91 #define __TA_NO_THREAD_SAFETY_ANALYSIS __THREAD_ANNOTATION(__no_thread_safety_analysis__)
92 
93 #endif  // ifndef __ASSEMBLER__
94 
95 #if !defined(__DEPRECATE)
96 #define __DEPRECATE __attribute__((__deprecated__))
97 #endif
98 
99 /* TODO: add type check */
100 #if !defined(countof)
101 #define countof(a) (sizeof(a) / sizeof((a)[0]))
102 #endif
103 
104 /* CPP header guards */
105 #ifdef __cplusplus
106 #define __BEGIN_CDECLS  extern "C" {
107 #define __END_CDECLS    }
108 #else
109 #define __BEGIN_CDECLS
110 #define __END_CDECLS
111 #endif
112 
113 // constexpr annotation for use in static inlines usable in both C and C++
114 #ifdef __cplusplus
115 #define __CONSTEXPR constexpr
116 #else
117 #define __CONSTEXPR
118 #endif
119 
120 #define add_overflow(a, b, c) __builtin_add_overflow(a, b, c)
121 #define sub_overflow(a, b, c) __builtin_sub_overflow(a, b, c)
122 #define mul_overflow(a, b, c) __builtin_mul_overflow(a, b, c)
123