Include cpp only when needed
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2013 Facebook, Inc.
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 FOLLY_PORTABILITY_H_
18 #define FOLLY_PORTABILITY_H_
19
20 #ifndef FOLLY_NO_CONFIG
21 #include "folly-config.h"
22 #endif
23
24 #if FOLLY_HAVE_FEATURES_H
25 #include <features.h>
26 #endif
27
28
29 #if FOLLY_HAVE_SCHED_H
30  #include <sched.h>
31  #ifndef FOLLY_HAVE_PTHREAD_YIELD
32   #define pthread_yield sched_yield
33  #endif
34 #endif
35
36
37 // MaxAlign: max_align_t isn't supported by gcc
38 #ifdef __GNUC__
39 struct MaxAlign { char c; } __attribute__((aligned));
40 #else /* !__GNUC__ */
41 # error Cannot define MaxAlign on this platform
42 #endif
43
44
45 // noreturn
46 #if defined(__clang__) || defined(__GNUC__)
47 # define FOLLY_NORETURN __attribute__((noreturn))
48 #else
49 # define FOLLY_NORETURN
50 #endif
51
52
53 // portable version check
54 #ifndef __GNUC_PREREQ
55 # if defined __GNUC__ && defined __GNUC_MINOR__
56 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
57                                    ((maj) << 16) + (min))
58 # else
59 #  define __GNUC_PREREQ(maj, min) 0
60 # endif
61 #endif
62
63
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
70 # else
71 #  define FOLLY_FINAL /**/
72 #  define FOLLY_OVERRIDE /**/
73 # endif
74 #endif
75
76
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
83 #  endif
84 # endif
85 #endif
86
87
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
93 # endif
94 #elif defined (__GNUC__) && \
95       (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
96       __SANITIZE_ADDRESS__
97 # define FOLLY_SANITIZE_ADDRESS 1
98 #endif
99
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__))
111 #  endif
112 # elif defined(__GNUC__)
113 #  define FOLLY_DISABLE_ADDRESS_SANITIZER \
114      __attribute__((__no_address_safety_analysis__, __noinline__))
115 # endif
116 #endif
117 #ifndef FOLLY_DISABLE_ADDRESS_SANITIZER
118 # define FOLLY_DISABLE_ADDRESS_SANITIZER
119 #endif
120
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.
124 #if FOLLY_USE_LIBCPP
125 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
126 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
127 #else
128 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
129 #define FOLLY_NAMESPACE_STD_END       }
130 #endif
131
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
135 #include <time.h>
136 #else
137 #include "folly/detail/Clock.h"
138 #endif
139
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>.
145 //
146 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
147 //
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)
153 #else
154 #include <boost/type_traits.hpp>
155 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)      (boost::has_trivial_copy<T>::value)
156 #endif
157 #endif // __cplusplus
158
159 #endif // FOLLY_PORTABILITY_H_