2 * Copyright 2013 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_PORTABILITY_H_
18 #define FOLLY_PORTABILITY_H_
20 #ifndef FOLLY_NO_CONFIG
21 #include "folly-config.h"
24 #if FOLLY_HAVE_FEATURES_H
29 #if FOLLY_HAVE_SCHED_H
31 #ifndef FOLLY_HAVE_PTHREAD_YIELD
32 #define pthread_yield sched_yield
37 // MaxAlign: max_align_t isn't supported by gcc
39 struct MaxAlign { char c; } __attribute__((aligned));
41 # error Cannot define MaxAlign on this platform
46 #if defined(__clang__) || defined(__GNUC__)
47 # define FOLLY_NORETURN __attribute__((noreturn))
49 # define FOLLY_NORETURN
53 // portable version check
55 # if defined __GNUC__ && defined __GNUC_MINOR__
56 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
57 ((maj) << 16) + (min))
59 # define __GNUC_PREREQ(maj, min) 0
64 /* Define macro wrappers for C++11's "final" and "override" keywords, which
65 * are supported in gcc 4.7 but not gcc 4.6. */
66 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
67 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
68 # define FOLLY_FINAL final
69 # define FOLLY_OVERRIDE override
71 # define FOLLY_FINAL /**/
72 # define FOLLY_OVERRIDE /**/
77 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
78 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
79 # if defined(__GLIBC_PREREQ)
80 # if __GLIBC_PREREQ(2, 10)
81 # define FOLLY_HAVE_PREADV 1
82 # define FOLLY_HAVE_PWRITEV 1
88 /* Define a convenience macro to test when address sanitizer is being used
89 * across the different compilers (e.g. clang, gcc) */
90 #if defined(__clang__)
91 # if __has_feature(address_sanitizer)
92 # define FOLLY_SANITIZE_ADDRESS 1
94 #elif defined (__GNUC__) && \
95 (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
97 # define FOLLY_SANITIZE_ADDRESS 1
100 /* Define attribute wrapper for function attribute used to disable
101 * address sanitizer instrumentation. Unfortunately, this attribute
102 * has issues when inlining is used, so disable that as well. */
103 #ifdef FOLLY_SANITIZE_ADDRESS
104 # if defined(__clang__)
105 # if __has_attribute(__no_address_safety_analysis__)
106 # define FOLLY_DISABLE_ADDRESS_SANITIZER \
107 __attribute__((__no_address_safety_analysis__, __noinline__))
108 # elif __has_attribute(__no_sanitize_address__)
109 # define FOLLY_DISABLE_ADDRESS_SANITIZER \
110 __attribute__((__no_sanitize_address__, __noinline__))
112 # elif defined(__GNUC__)
113 # define FOLLY_DISABLE_ADDRESS_SANITIZER \
114 __attribute__((__no_address_safety_analysis__, __noinline__))
117 #ifndef FOLLY_DISABLE_ADDRESS_SANITIZER
118 # define FOLLY_DISABLE_ADDRESS_SANITIZER
121 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
122 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
123 // up in a macro to make forward-declarations easier.
125 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
126 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
128 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
129 #define FOLLY_NAMESPACE_STD_END }
132 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
133 // versions of these into the global namespace.
134 #if FOLLY_HAVE_CLOCK_GETTIME
137 #include "folly/detail/Clock.h"
140 #if defined(__cplusplus)
141 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
142 // usage of __has_trivial_copy(), so we can't use it as a
143 // least-common-denominator for C++11 implementations that don't support
144 // std::is_trivially_copyable<T>.
146 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
148 // As a result, use std::is_trivially_copyable() where it exists, and fall back
149 // to Boost otherwise.
150 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
151 #include <type_traits>
152 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) (std::is_trivially_copyable<T>::value)
154 #include <boost/type_traits.hpp>
155 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) (boost::has_trivial_copy<T>::value)
157 #endif // __cplusplus
159 #endif // FOLLY_PORTABILITY_H_