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