1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef INCLUDE_PERFETTO_BASE_COMPILER_H_
18 #define INCLUDE_PERFETTO_BASE_COMPILER_H_
19
20 #include <type_traits>
21
22 #include "perfetto/base/build_config.h"
23
24 #define PERFETTO_LIKELY(_x) __builtin_expect(!!(_x), 1)
25 #define PERFETTO_UNLIKELY(_x) __builtin_expect(!!(_x), 0)
26
27 #if defined(__GNUC__) || defined(__clang__)
28 #define PERFETTO_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
29 #else
30 #define PERFETTO_WARN_UNUSED_RESULT
31 #endif
32
33 #if defined(__clang__)
34 #define PERFETTO_ALWAYS_INLINE __attribute__((__always_inline__))
35 #define PERFETTO_NO_INLINE __attribute__((__noinline__))
36 #else
37 // GCC is too pedantic and often fails with the error:
38 // "always_inline function might not be inlinable"
39 #define PERFETTO_ALWAYS_INLINE
40 #define PERFETTO_NO_INLINE
41 #endif
42
43 #if defined(__GNUC__) || defined(__clang__)
44 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() __PRETTY_FUNCTION__
45 #elif defined(_MSC_VER)
46 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() __FUNCSIG__
47 #else
48 #define PERFETTO_DEBUG_FUNCTION_IDENTIFIER() \
49 static_assert(false, "Not implemented for this compiler")
50 #endif
51
52 #if defined(__GNUC__) || defined(__clang__)
53 #define PERFETTO_PRINTF_FORMAT(x, y) \
54 __attribute__((__format__(__printf__, x, y)))
55 #else
56 #define PERFETTO_PRINTF_FORMAT(x, y)
57 #endif
58
59 #if PERFETTO_BUILDFLAG(PERFETTO_OS_IOS)
60 // TODO(b/158814068): For iOS builds, thread_local is only supported since iOS
61 // 8. We'd have to use pthread for thread local data instead here. For now, just
62 // define it to nothing since we don't support running perfetto or the client
63 // lib on iOS right now.
64 #define PERFETTO_THREAD_LOCAL
65 #else
66 #define PERFETTO_THREAD_LOCAL thread_local
67 #endif
68
69 #if defined(__clang__)
70 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
71 extern "C" void __asan_poison_memory_region(void const volatile*, size_t);
72 extern "C" void __asan_unpoison_memory_region(void const volatile*, size_t);
73 #define PERFETTO_ASAN_POISON(a, s) __asan_poison_memory_region((a), (s))
74 #define PERFETTO_ASAN_UNPOISON(a, s) __asan_unpoison_memory_region((a), (s))
75 #else
76 #define PERFETTO_ASAN_POISON(addr, size)
77 #define PERFETTO_ASAN_UNPOISON(addr, size)
78 #endif // __has_feature(address_sanitizer)
79 #else
80 #define PERFETTO_ASAN_POISON(addr, size)
81 #define PERFETTO_ASAN_UNPOISON(addr, size)
82 #endif // __clang__
83
84 namespace perfetto {
85 namespace base {
86
87 template <typename... T>
ignore_result(const T &...)88 inline void ignore_result(const T&...) {}
89
90 } // namespace base
91 } // namespace perfetto
92
93 #endif // INCLUDE_PERFETTO_BASE_COMPILER_H_
94