Create a malloc.h portability header
[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 #ifndef FOLLY_PORTABILITY_H_
18 #define FOLLY_PORTABILITY_H_
19
20 #include <string.h>
21
22 #include <cstddef>
23
24 #include <folly/portability/Config.h>
25
26 #include <folly/CPortability.h>
27
28 #if FOLLY_HAVE_SCHED_H
29  #include <sched.h>
30 #endif
31
32 // Unaligned loads and stores
33 namespace folly {
34 #if FOLLY_HAVE_UNALIGNED_ACCESS
35 constexpr bool kHasUnalignedAccess = true;
36 #else
37 constexpr bool kHasUnalignedAccess = false;
38 #endif
39 }
40
41 // compiler specific attribute translation
42 // msvc should come first, so if clang is in msvc mode it gets the right defines
43
44 #if defined(__clang__) || defined(__GNUC__)
45 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
46 #elif defined(_MSC_VER)
47 # define FOLLY_ALIGNED(size) __declspec(align(size))
48 #else
49 # error Cannot define FOLLY_ALIGNED on this platform
50 #endif
51 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
52
53 // NOTE: this will only do checking in msvc with versions that support /analyze
54 #if _MSC_VER
55 # ifdef _USE_ATTRIBUTES_FOR_SAL
56 #    undef _USE_ATTRIBUTES_FOR_SAL
57 # endif
58 /* nolint */
59 # define _USE_ATTRIBUTES_FOR_SAL 1
60 # include <sal.h>
61 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
62 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
63 #else
64 # define FOLLY_PRINTF_FORMAT /**/
65 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
66   __attribute__((__format__(__printf__, format_param, dots_param)))
67 #endif
68
69 // deprecated
70 #if defined(__clang__) || defined(__GNUC__)
71 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
72 #elif defined(_MSC_VER)
73 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
74 #else
75 # define FOLLY_DEPRECATED(msg)
76 #endif
77
78 // noinline
79 #ifdef _MSC_VER
80 # define FOLLY_NOINLINE __declspec(noinline)
81 #elif defined(__clang__) || defined(__GNUC__)
82 # define FOLLY_NOINLINE __attribute__((__noinline__))
83 #else
84 # define FOLLY_NOINLINE
85 #endif
86
87 // always inline
88 #ifdef _MSC_VER
89 # define FOLLY_ALWAYS_INLINE __forceinline
90 #elif defined(__clang__) || defined(__GNUC__)
91 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
92 #else
93 # define FOLLY_ALWAYS_INLINE inline
94 #endif
95
96 // detection for 64 bit
97 #if defined(__x86_64__) || defined(_M_X64)
98 # define FOLLY_X64 1
99 #else
100 # define FOLLY_X64 0
101 #endif
102
103 #if defined(__aarch64__)
104 # define FOLLY_A64 1
105 #else
106 # define FOLLY_A64 0
107 #endif
108
109 #if defined (__powerpc64__)
110 # define FOLLY_PPC64 1
111 #else
112 # define FOLLY_PPC64 0
113 #endif
114
115 // packing is very ugly in msvc
116 #ifdef _MSC_VER
117 # define FOLLY_PACK_ATTR /**/
118 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
119 # define FOLLY_PACK_POP __pragma(pack(pop))
120 #elif defined(__clang__) || defined(__GNUC__)
121 # define FOLLY_PACK_ATTR __attribute__((__packed__))
122 # define FOLLY_PACK_PUSH /**/
123 # define FOLLY_PACK_POP /**/
124 #else
125 # define FOLLY_PACK_ATTR /**/
126 # define FOLLY_PACK_PUSH /**/
127 # define FOLLY_PACK_POP /**/
128 #endif
129
130 // Generalize warning push/pop.
131 #if defined(_MSC_VER)
132 # define FOLLY_PUSH_WARNING __pragma(warning(push))
133 # define FOLLY_POP_WARNING __pragma(warning(pop))
134 // Disable the GCC warnings.
135 # define FOLLY_GCC_DISABLE_WARNING(warningName)
136 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
137 #elif defined(__clang__) || defined(__GNUC__)
138 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
139 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
140 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName) #warningName
141 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) \
142   FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName)
143 #define FOLLY_GCC_DISABLE_WARNING(warningName)                       \
144   _Pragma(FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored \
145           FOLLY_GCC_DISABLE_WARNING_INTERNAL3(-W##warningName)))
146 // Disable the MSVC warnings.
147 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
148 #else
149 # define FOLLY_PUSH_WARNING
150 # define FOLLY_POP_WARNING
151 # define FOLLY_GCC_DISABLE_WARNING(warningName)
152 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
153 #endif
154
155 // portable version check
156 #ifndef __GNUC_PREREQ
157 # if defined __GNUC__ && defined __GNUC_MINOR__
158 /* nolint */
159 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
160                                    ((maj) << 16) + (min))
161 # else
162 /* nolint */
163 #  define __GNUC_PREREQ(maj, min) 0
164 # endif
165 #endif
166
167 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
168 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
169 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
170 // Alias it into std (where it's found in 4.9 and later)
171 namespace std { typedef ::max_align_t max_align_t; }
172 #endif
173
174 // portable version check for clang
175 #ifndef __CLANG_PREREQ
176 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
177 /* nolint */
178 #  define __CLANG_PREREQ(maj, min) \
179     ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
180 # else
181 /* nolint */
182 #  define __CLANG_PREREQ(maj, min) 0
183 # endif
184 #endif
185
186 /* Platform specific TLS support
187  * gcc implements __thread
188  * msvc implements __declspec(thread)
189  * the semantics are the same
190  * (but remember __thread has different semantics when using emutls (ex. apple))
191  */
192 #if defined(_MSC_VER)
193 # define FOLLY_TLS __declspec(thread)
194 #elif defined(__GNUC__) || defined(__clang__)
195 # define FOLLY_TLS __thread
196 #else
197 # error cannot define platform specific thread local storage
198 #endif
199
200 #if FOLLY_MOBILE
201 #undef FOLLY_TLS
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 namespaces. 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 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
227 // some types, e.g. std::list.
228 #if _GLIBCXX_USE_CXX11_ABI
229 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
230 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END   _GLIBCXX_END_NAMESPACE_CXX11
231 #else
232 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
233 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
234 #endif
235
236 // Provide our own std::__throw_* wrappers for platforms that don't have them
237 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
238 #include <bits/functexcept.h>
239 #else
240 #include <folly/detail/FunctionalExcept.h>
241 #endif
242
243 #if defined(__cplusplus)
244 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
245 // usage of __has_trivial_copy(), so we can't use it as a
246 // least-common-denominator for C++11 implementations that don't support
247 // std::is_trivially_copyable<T>.
248 //
249 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
250 //
251 // As a result, use std::is_trivially_copyable() where it exists, and fall back
252 // to Boost otherwise.
253 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
254 #include <type_traits>
255 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
256   (std::is_trivially_copyable<T>::value)
257 #else
258 #include <boost/type_traits.hpp>
259 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
260   (boost::has_trivial_copy<T>::value &&                  \
261    boost::has_trivial_destructor<T>::value)
262 #endif
263 #endif // __cplusplus
264
265 // MSVC specific defines
266 // mainly for posix compat
267 #ifdef _MSC_VER
268 #include <folly/portability/SysTypes.h>
269
270 // sprintf semantics are not exactly identical
271 // but current usage is not a problem
272 # define snprintf _snprintf
273
274 // semantics here are identical
275 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
276
277 // compiler specific to compiler specific
278 // nolint
279 # define __PRETTY_FUNCTION__ __FUNCSIG__
280
281 // Hide a GCC specific thing that breaks MSVC if left alone.
282 # define __extension__
283
284 // We have compiler support for the newest of the new, but
285 // MSVC doesn't tell us that.
286 #define __SSE4_2__ 1
287
288 #endif
289
290 // Debug
291 namespace folly {
292 #ifdef NDEBUG
293 constexpr auto kIsDebug = false;
294 #else
295 constexpr auto kIsDebug = true;
296 #endif
297 }
298
299 // Endianness
300 namespace folly {
301 #ifdef _MSC_VER
302 // It's MSVC, so we just have to guess ... and allow an override
303 #ifdef FOLLY_ENDIAN_BE
304 constexpr auto kIsLittleEndian = false;
305 #else
306 constexpr auto kIsLittleEndian = true;
307 #endif
308 #else
309 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
310 #endif
311 constexpr auto kIsBigEndian = !kIsLittleEndian;
312 }
313
314 #ifndef FOLLY_SSE
315 # if defined(__SSE4_2__)
316 #  define FOLLY_SSE 4
317 #  define FOLLY_SSE_MINOR 2
318 # elif defined(__SSE4_1__)
319 #  define FOLLY_SSE 4
320 #  define FOLLY_SSE_MINOR 1
321 # elif defined(__SSE4__)
322 #  define FOLLY_SSE 4
323 #  define FOLLY_SSE_MINOR 0
324 # elif defined(__SSE3__)
325 #  define FOLLY_SSE 3
326 #  define FOLLY_SSE_MINOR 0
327 # elif defined(__SSE2__)
328 #  define FOLLY_SSE 2
329 #  define FOLLY_SSE_MINOR 0
330 # elif defined(__SSE__)
331 #  define FOLLY_SSE 1
332 #  define FOLLY_SSE_MINOR 0
333 # else
334 #  define FOLLY_SSE 0
335 #  define FOLLY_SSE_MINOR 0
336 # endif
337 #endif
338
339 #define FOLLY_SSE_PREREQ(major, minor) \
340   (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
341
342 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
343 namespace FOLLY_GFLAGS_NAMESPACE { }
344 namespace gflags {
345 using namespace FOLLY_GFLAGS_NAMESPACE;
346 }  // namespace gflags
347 #endif
348
349 // for TARGET_OS_IPHONE
350 #ifdef __APPLE__
351 #include <TargetConditionals.h>
352 #endif
353
354 // RTTI may not be enabled for this compilation unit.
355 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
356     (defined(_MSC_VER) && defined(_CPPRTTI))
357 # define FOLLY_HAS_RTTI 1
358 #endif
359
360 #if defined(__APPLE__) || defined(_MSC_VER)
361 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
362 #else
363 // 101 is the highest priority allowed by the init_priority attribute.
364 // This priority is already used by JEMalloc and other memory allocators so
365 // we will take the next one.
366 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
367 #endif
368
369 #endif // FOLLY_PORTABILITY_H_