Port Folly to PPC64
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2015 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 #include <cstddef>
21
22 #ifndef FOLLY_NO_CONFIG
23 #include <folly/folly-config.h>
24 #endif
25
26 #ifdef FOLLY_PLATFORM_CONFIG
27 #include FOLLY_PLATFORM_CONFIG
28 #endif
29
30 #if FOLLY_HAVE_FEATURES_H
31 #include <features.h>
32 #endif
33
34 #include <folly/CPortability.h>
35
36 #ifdef __APPLE__
37 # include <malloc/malloc.h>
38 #endif
39
40 #if FOLLY_HAVE_SCHED_H
41  #include <sched.h>
42  #ifndef FOLLY_HAVE_PTHREAD_YIELD
43   #define pthread_yield sched_yield
44  #endif
45 #endif
46
47 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
48 // MAP_ANON on OSX/BSD.
49 #if defined(__APPLE__) || defined(__FreeBSD__)
50   #include <sys/mman.h>
51   #ifndef MAP_ANONYMOUS
52     #ifdef MAP_ANON
53       #define MAP_ANONYMOUS MAP_ANON
54     #endif
55   #endif
56 #endif
57
58 // compiler specific attribute translation
59 // msvc should come first, so if clang is in msvc mode it gets the right defines
60
61 #if defined(__clang__) || defined(__GNUC__)
62 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
63 #elif defined(_MSC_VER)
64 # define FOLLY_ALIGNED(size) __declspec(align(size))
65 #else
66 # error Cannot define FOLLY_ALIGNED on this platform
67 #endif
68 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
69
70 // NOTE: this will only do checking in msvc with versions that support /analyze
71 #if _MSC_VER
72 # ifdef _USE_ATTRIBUTES_FOR_SAL
73 #    undef _USE_ATTRIBUTES_FOR_SAL
74 # endif
75 /* nolint */
76 # define _USE_ATTRIBUTES_FOR_SAL 1
77 # include <sal.h>
78 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
79 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
80 #else
81 # define FOLLY_PRINTF_FORMAT /**/
82 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
83   __attribute__((__format__(__printf__, format_param, dots_param)))
84 #endif
85
86 // deprecated
87 #if defined(__clang__) || defined(__GNUC__)
88 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
89 #elif defined(_MSC_VER)
90 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
91 #else
92 # define FOLLY_DEPRECATED(msg)
93 #endif
94
95 // noreturn
96 #if defined(_MSC_VER)
97 # define FOLLY_NORETURN __declspec(noreturn)
98 #elif defined(__clang__) || defined(__GNUC__)
99 # define FOLLY_NORETURN __attribute__((__noreturn__))
100 #else
101 # define FOLLY_NORETURN
102 #endif
103
104 // noinline
105 #ifdef _MSC_VER
106 # define FOLLY_NOINLINE __declspec(noinline)
107 #elif defined(__clang__) || defined(__GNUC__)
108 # define FOLLY_NOINLINE __attribute__((__noinline__))
109 #else
110 # define FOLLY_NOINLINE
111 #endif
112
113 // always inline
114 #ifdef _MSC_VER
115 # define FOLLY_ALWAYS_INLINE __forceinline
116 #elif defined(__clang__) || defined(__GNUC__)
117 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
118 #else
119 # define FOLLY_ALWAYS_INLINE inline
120 #endif
121
122 // detection for 64 bit
123 #if defined(__x86_64__) || defined(_M_X64)
124 # define FOLLY_X64 1
125 #else
126 # define FOLLY_X64 0
127 #endif
128
129 #if defined(__aarch64__)
130 # define FOLLY_A64 1
131 #else
132 # define FOLLY_A64 0
133 #endif
134
135 #if defined (__powerpc64__)
136 # define FOLLY_PPC64 1
137 #else
138 # define FOLLY_PPC64 0
139 #endif
140
141 // packing is very ugly in msvc
142 #ifdef _MSC_VER
143 # define FOLLY_PACK_ATTR /**/
144 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
145 # define FOLLY_PACK_POP __pragma(pack(pop))
146 #elif defined(__clang__) || defined(__GNUC__)
147 # define FOLLY_PACK_ATTR __attribute__((__packed__))
148 # define FOLLY_PACK_PUSH /**/
149 # define FOLLY_PACK_POP /**/
150 #else
151 # define FOLLY_PACK_ATTR /**/
152 # define FOLLY_PACK_PUSH /**/
153 # define FOLLY_PACK_POP /**/
154 #endif
155
156 // portable version check
157 #ifndef __GNUC_PREREQ
158 # if defined __GNUC__ && defined __GNUC_MINOR__
159 /* nolint */
160 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
161                                    ((maj) << 16) + (min))
162 # else
163 /* nolint */
164 #  define __GNUC_PREREQ(maj, min) 0
165 # endif
166 #endif
167
168 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
169 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
170 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
171 // Alias it into std (where it's found in 4.9 and later)
172 namespace std { typedef ::max_align_t max_align_t; }
173 #endif
174
175 /* Define macro wrappers for C++11's "final" and "override" keywords, which
176  * are supported in gcc 4.7 but not gcc 4.6. */
177 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
178 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
179 #  define FOLLY_FINAL final
180 #  define FOLLY_OVERRIDE override
181 # elif defined(_MSC_VER) && _MSC_VER >= 1600
182 #  define FOLLY_FINAL final
183 #  define FOLLY_OVERRIDE override
184 # else
185 #  define FOLLY_FINAL /**/
186 #  define FOLLY_OVERRIDE /**/
187 # endif
188 #endif
189
190 /* Platform specific TLS support
191  * gcc implements __thread
192  * msvc implements __declspec(thread)
193  * the semantics are the same
194  * (but remember __thread has different semantics when using emutls (ex. apple))
195  */
196 #if defined(_MSC_VER)
197 # define FOLLY_TLS __declspec(thread)
198 #elif defined(__GNUC__) || defined(__clang__)
199 # define FOLLY_TLS __thread
200 #else
201 # error cannot define platform specific thread local storage
202 #endif
203
204 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
205 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
206 # if defined(__GLIBC_PREREQ)
207 #  if __GLIBC_PREREQ(2, 10)
208 #   define FOLLY_HAVE_PREADV 1
209 #   define FOLLY_HAVE_PWRITEV 1
210 #  endif
211 # endif
212 #endif
213
214 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
215 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
216 // up in a macro to make forward-declarations easier.
217 #if FOLLY_USE_LIBCPP
218 #include <__config>
219 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
220 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
221 #else
222 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
223 #define FOLLY_NAMESPACE_STD_END       }
224 #endif
225
226 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
227 // versions of these into the global namespace.
228 #if FOLLY_HAVE_CLOCK_GETTIME
229 #include <time.h>
230 #else
231 #include <folly/detail/Clock.h>
232 #endif
233
234 // Provide our own std::__throw_* wrappers for platforms that don't have them
235 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
236 #include <bits/functexcept.h>
237 #else
238 #include <folly/detail/FunctionalExcept.h>
239 #endif
240
241 #if defined(__cplusplus)
242 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
243 // usage of __has_trivial_copy(), so we can't use it as a
244 // least-common-denominator for C++11 implementations that don't support
245 // std::is_trivially_copyable<T>.
246 //
247 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
248 //
249 // As a result, use std::is_trivially_copyable() where it exists, and fall back
250 // to Boost otherwise.
251 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
252 #include <type_traits>
253 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
254   (std::is_trivially_copyable<T>::value)
255 #else
256 #include <boost/type_traits.hpp>
257 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
258   (boost::has_trivial_copy<T>::value &&                  \
259    boost::has_trivial_destructor<T>::value)
260 #endif
261 #endif // __cplusplus
262
263 // MSVC specific defines
264 // mainly for posix compat
265 #ifdef _MSC_VER
266
267 // this definition is in a really silly place with a silly name
268 // and ifdefing it every time we want it is painful
269 #include <basetsd.h>
270 typedef SSIZE_T ssize_t;
271
272 // sprintf semantics are not exactly identical
273 // but current usage is not a problem
274 # define snprintf _snprintf
275
276 // semantics here are identical
277 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
278
279 // compiler specific to compiler specific
280 // nolint
281 # define __PRETTY_FUNCTION__ __FUNCSIG__
282
283 // Hide a GCC specific thing that breaks MSVC if left alone.
284 # define __extension__
285
286 #ifdef _M_IX86_FP
287 # define FOLLY_SSE _M_IX86_FP
288 # define FOLLY_SSE_MINOR 0
289 #endif
290
291 #endif
292
293 #ifndef FOLLY_SSE
294 # if defined(__SSE4_2__)
295 #  define FOLLY_SSE 4
296 #  define FOLLY_SSE_MINOR 2
297 # elif defined(__SSE4_1__)
298 #  define FOLLY_SSE 4
299 #  define FOLLY_SSE_MINOR 1
300 # elif defined(__SSE4__)
301 #  define FOLLY_SSE 4
302 #  define FOLLY_SSE_MINOR 0
303 # elif defined(__SSE3__)
304 #  define FOLLY_SSE 3
305 #  define FOLLY_SSE_MINOR 0
306 # elif defined(__SSE2__)
307 #  define FOLLY_SSE 2
308 #  define FOLLY_SSE_MINOR 0
309 # elif defined(__SSE__)
310 #  define FOLLY_SSE 1
311 #  define FOLLY_SSE_MINOR 0
312 # else
313 #  define FOLLY_SSE 0
314 #  define FOLLY_SSE_MINOR 0
315 # endif
316 #endif
317
318 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
319 namespace FOLLY_GFLAGS_NAMESPACE { }
320 namespace gflags {
321 using namespace FOLLY_GFLAGS_NAMESPACE;
322 }  // namespace gflags
323 #endif
324
325 // for TARGET_OS_IPHONE
326 #ifdef __APPLE__
327 #include <TargetConditionals.h>
328 #endif
329
330 // MacOS doesn't have malloc_usable_size()
331 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
332 inline size_t malloc_usable_size(void* ptr) {
333   return malloc_size(ptr);
334 }
335 #endif
336
337 // RTTI may not be enabled for this compilation unit.
338 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
339     (defined(_MSC_VER) && defined(_CPPRTTI))
340 # define FOLLY_HAS_RTTI 1
341 #endif
342
343 #ifdef _MSC_VER
344 # include <intrin.h>
345 #endif
346
347 namespace folly {
348
349 inline void asm_volatile_memory() {
350 #if defined(__clang__) || defined(__GNUC__)
351   asm volatile("" : : : "memory");
352 #elif defined(_MSC_VER)
353   ::_ReadWriteBarrier();
354 #endif
355 }
356
357 inline void asm_volatile_pause() {
358 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
359   ::_mm_pause();
360 #elif defined(__i386__) || FOLLY_X64
361   asm volatile ("pause");
362 #elif FOLLY_A64
363   asm volatile ("wfe");
364 #elif FOLLY_PPC64
365   asm volatile("or 27,27,27");
366 #endif
367 }
368 inline void asm_pause() {
369 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
370   ::_mm_pause();
371 #elif defined(__i386__) || FOLLY_X64
372   asm ("pause");
373 #elif FOLLY_A64
374   asm ("wfe");
375 #elif FOLLY_PPC64
376   asm ("or 31,31,31");
377 #endif
378 }
379
380 }
381
382 #endif // FOLLY_PORTABILITY_H_