folly: fixes for use with `-fvisibility-inlines-hidden`
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2016 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 #pragma once
18
19 #include <string.h>
20
21 #include <cstddef>
22
23 #include <folly/portability/Config.h>
24
25 #include <folly/CPortability.h>
26
27 #if FOLLY_HAVE_SCHED_H
28  #include <sched.h>
29 #endif
30
31 // Unaligned loads and stores
32 namespace folly {
33 #if FOLLY_HAVE_UNALIGNED_ACCESS
34 constexpr bool kHasUnalignedAccess = true;
35 #else
36 constexpr bool kHasUnalignedAccess = false;
37 #endif
38 }
39
40 // compiler specific attribute translation
41 // msvc should come first, so if clang is in msvc mode it gets the right defines
42
43 #if defined(__clang__) || defined(__GNUC__)
44 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
45 #elif defined(_MSC_VER)
46 # define FOLLY_ALIGNED(size) __declspec(align(size))
47 #else
48 # error Cannot define FOLLY_ALIGNED on this platform
49 #endif
50 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
51
52 // NOTE: this will only do checking in msvc with versions that support /analyze
53 #if _MSC_VER
54 # ifdef _USE_ATTRIBUTES_FOR_SAL
55 #    undef _USE_ATTRIBUTES_FOR_SAL
56 # endif
57 /* nolint */
58 # define _USE_ATTRIBUTES_FOR_SAL 1
59 # include <sal.h>
60 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
61 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
62 #else
63 # define FOLLY_PRINTF_FORMAT /**/
64 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
65   __attribute__((__format__(__printf__, format_param, dots_param)))
66 #endif
67
68 // deprecated
69 #if defined(__clang__) || defined(__GNUC__)
70 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
71 #elif defined(_MSC_VER)
72 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
73 #else
74 # define FOLLY_DEPRECATED(msg)
75 #endif
76
77 // noinline
78 #ifdef _MSC_VER
79 # define FOLLY_NOINLINE __declspec(noinline)
80 #elif defined(__clang__) || defined(__GNUC__)
81 # define FOLLY_NOINLINE __attribute__((__noinline__))
82 #else
83 # define FOLLY_NOINLINE
84 #endif
85
86 // always inline
87 #ifdef _MSC_VER
88 # define FOLLY_ALWAYS_INLINE __forceinline
89 #elif defined(__clang__) || defined(__GNUC__)
90 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
91 #else
92 # define FOLLY_ALWAYS_INLINE inline
93 #endif
94
95 // warn unused result
96 #if defined(_MSC_VER) && (_MSC_VER >= 1700)
97 #define FOLLY_WARN_UNUSED_RESULT _Check_return_
98 #elif defined(__clang__) || defined(__GNUC__)
99 #define FOLLY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
100 #else
101 #define FOLLY_WARN_UNUSED_RESULT
102 #endif
103
104 // target
105 #ifdef _MSC_VER
106 # define FOLLY_TARGET_ATTRIBUTE(target)
107 #else
108 # define FOLLY_TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
109 #endif
110
111 // detection for 64 bit
112 #if defined(__x86_64__) || defined(_M_X64)
113 # define FOLLY_X64 1
114 #else
115 # define FOLLY_X64 0
116 #endif
117
118 #if defined(__aarch64__)
119 # define FOLLY_A64 1
120 #else
121 # define FOLLY_A64 0
122 #endif
123
124 #if defined (__powerpc64__)
125 # define FOLLY_PPC64 1
126 #else
127 # define FOLLY_PPC64 0
128 #endif
129
130 // packing is very ugly in msvc
131 #ifdef _MSC_VER
132 # define FOLLY_PACK_ATTR /**/
133 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
134 # define FOLLY_PACK_POP __pragma(pack(pop))
135 #elif defined(__clang__) || defined(__GNUC__)
136 # define FOLLY_PACK_ATTR __attribute__((__packed__))
137 # define FOLLY_PACK_PUSH /**/
138 # define FOLLY_PACK_POP /**/
139 #else
140 # define FOLLY_PACK_ATTR /**/
141 # define FOLLY_PACK_PUSH /**/
142 # define FOLLY_PACK_POP /**/
143 #endif
144
145 // Generalize warning push/pop.
146 #if defined(_MSC_VER)
147 # define FOLLY_PUSH_WARNING __pragma(warning(push))
148 # define FOLLY_POP_WARNING __pragma(warning(pop))
149 // Disable the GCC warnings.
150 # define FOLLY_GCC_DISABLE_WARNING(warningName)
151 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
152 #elif defined(__clang__) || defined(__GNUC__)
153 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
154 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
155 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName) #warningName
156 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) \
157   FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName)
158 #define FOLLY_GCC_DISABLE_WARNING(warningName)                       \
159   _Pragma(FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored \
160           FOLLY_GCC_DISABLE_WARNING_INTERNAL3(-W##warningName)))
161 // Disable the MSVC warnings.
162 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
163 #else
164 # define FOLLY_PUSH_WARNING
165 # define FOLLY_POP_WARNING
166 # define FOLLY_GCC_DISABLE_WARNING(warningName)
167 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
168 #endif
169
170 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
171 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
172 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
173 // Alias it into std (where it's found in 4.9 and later)
174 namespace std { typedef ::max_align_t max_align_t; }
175 #endif
176
177 // portable version check for clang
178 #ifndef __CLANG_PREREQ
179 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
180 /* nolint */
181 #  define __CLANG_PREREQ(maj, min) \
182     ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
183 # else
184 /* nolint */
185 #  define __CLANG_PREREQ(maj, min) 0
186 # endif
187 #endif
188
189 /* Platform specific TLS support
190  * gcc implements __thread
191  * msvc implements __declspec(thread)
192  * the semantics are the same
193  * (but remember __thread has different semantics when using emutls (ex. apple))
194  */
195 #if defined(_MSC_VER)
196 # define FOLLY_TLS __declspec(thread)
197 #elif defined(__GNUC__) || defined(__clang__)
198 # define FOLLY_TLS __thread
199 #else
200 # error cannot define platform specific thread local storage
201 #endif
202
203 #if FOLLY_MOBILE
204 #undef FOLLY_TLS
205 #endif
206
207 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
208 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
209 // up in a macro to make forward-declarations easier.
210 #if FOLLY_USE_LIBCPP
211 #include <__config>
212 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
213 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
214 #else
215 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
216 #define FOLLY_NAMESPACE_STD_END       }
217 #endif
218
219 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
220 // some types, e.g. std::list.
221 #if _GLIBCXX_USE_CXX11_ABI
222 #define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN \
223   inline _GLIBCXX_BEGIN_NAMESPACE_CXX11
224 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END   _GLIBCXX_END_NAMESPACE_CXX11
225 #else
226 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
227 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
228 #endif
229
230 // MSVC specific defines
231 // mainly for posix compat
232 #ifdef _MSC_VER
233 #include <folly/portability/SysTypes.h>
234
235 // compiler specific to compiler specific
236 // nolint
237 # define __PRETTY_FUNCTION__ __FUNCSIG__
238
239 // Hide a GCC specific thing that breaks MSVC if left alone.
240 # define __extension__
241
242 // We have compiler support for the newest of the new, but
243 // MSVC doesn't tell us that.
244 #define __SSE4_2__ 1
245
246 #endif
247
248 // Debug
249 namespace folly {
250 #ifdef NDEBUG
251 constexpr auto kIsDebug = false;
252 #else
253 constexpr auto kIsDebug = true;
254 #endif
255 }
256
257 // Endianness
258 namespace folly {
259 #ifdef _MSC_VER
260 // It's MSVC, so we just have to guess ... and allow an override
261 #ifdef FOLLY_ENDIAN_BE
262 constexpr auto kIsLittleEndian = false;
263 #else
264 constexpr auto kIsLittleEndian = true;
265 #endif
266 #else
267 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
268 #endif
269 constexpr auto kIsBigEndian = !kIsLittleEndian;
270 }
271
272 #ifndef FOLLY_SSE
273 # if defined(__SSE4_2__)
274 #  define FOLLY_SSE 4
275 #  define FOLLY_SSE_MINOR 2
276 # elif defined(__SSE4_1__)
277 #  define FOLLY_SSE 4
278 #  define FOLLY_SSE_MINOR 1
279 # elif defined(__SSE4__)
280 #  define FOLLY_SSE 4
281 #  define FOLLY_SSE_MINOR 0
282 # elif defined(__SSE3__)
283 #  define FOLLY_SSE 3
284 #  define FOLLY_SSE_MINOR 0
285 # elif defined(__SSE2__)
286 #  define FOLLY_SSE 2
287 #  define FOLLY_SSE_MINOR 0
288 # elif defined(__SSE__)
289 #  define FOLLY_SSE 1
290 #  define FOLLY_SSE_MINOR 0
291 # else
292 #  define FOLLY_SSE 0
293 #  define FOLLY_SSE_MINOR 0
294 # endif
295 #endif
296
297 #define FOLLY_SSE_PREREQ(major, minor) \
298   (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
299
300 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
301 namespace FOLLY_GFLAGS_NAMESPACE { }
302 namespace gflags {
303 using namespace FOLLY_GFLAGS_NAMESPACE;
304 }  // namespace gflags
305 #endif
306
307 // for TARGET_OS_IPHONE
308 #ifdef __APPLE__
309 #include <TargetConditionals.h>
310 #endif
311
312 // RTTI may not be enabled for this compilation unit.
313 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
314     (defined(_MSC_VER) && defined(_CPPRTTI))
315 # define FOLLY_HAS_RTTI 1
316 #endif
317
318 #if defined(__APPLE__) || defined(_MSC_VER)
319 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
320 #else
321 // 101 is the highest priority allowed by the init_priority attribute.
322 // This priority is already used by JEMalloc and other memory allocators so
323 // we will take the next one.
324 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
325 #endif
326
327 namespace folly {
328
329 #if defined(__linux__) && !FOLLY_MOBILE
330 constexpr auto kIsLinux = true;
331 #else
332 constexpr auto kIsLinux = false;
333 #endif
334 }