MacOS doesn't have malloc_usable_size()
[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 #ifndef FOLLY_NO_CONFIG
21 #include <folly/folly-config.h>
22 #endif
23
24 #ifdef FOLLY_PLATFORM_CONFIG
25 #include FOLLY_PLATFORM_CONFIG
26 #endif
27
28 #if FOLLY_HAVE_FEATURES_H
29 #include <features.h>
30 #endif
31
32 #include <folly/CPortability.h>
33
34 #ifdef __APPLE__
35 # include <malloc/malloc.h>
36 #endif
37
38 #if FOLLY_HAVE_SCHED_H
39  #include <sched.h>
40  #ifndef FOLLY_HAVE_PTHREAD_YIELD
41   #define pthread_yield sched_yield
42  #endif
43 #endif
44
45 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
46 // MAP_ANON on OSX/BSD.
47 #if defined(__APPLE__) || defined(__FreeBSD__)
48   #include <sys/mman.h>
49   #ifndef MAP_ANONYMOUS
50     #ifdef MAP_ANON
51       #define MAP_ANONYMOUS MAP_ANON
52     #endif
53   #endif
54 #endif
55
56 // MaxAlign: max_align_t isn't supported by gcc
57 #ifdef __GNUC__
58 struct MaxAlign { char c; } __attribute__((__aligned__));
59 #else /* !__GNUC__ */
60 # error Cannot define MaxAlign on this platform
61 #endif
62
63 // compiler specific attribute translation
64 // msvc should come first, so if clang is in msvc mode it gets the right defines
65
66 // NOTE: this will only do checking in msvc with versions that support /analyze
67 #if _MSC_VER
68 # ifdef _USE_ATTRIBUTES_FOR_SAL
69 #    undef _USE_ATTRIBUTES_FOR_SAL
70 # endif
71 /* nolint */
72 # define _USE_ATTRIBUTES_FOR_SAL 1
73 # include <sal.h>
74 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
75 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
76 #else
77 # define FOLLY_PRINTF_FORMAT /**/
78 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
79   __attribute__((__format__(__printf__, format_param, dots_param)))
80 #endif
81
82 // noreturn
83 #if defined(_MSC_VER)
84 # define FOLLY_NORETURN __declspec(noreturn)
85 #elif defined(__clang__) || defined(__GNUC__)
86 # define FOLLY_NORETURN __attribute__((__noreturn__))
87 #else
88 # define FOLLY_NORETURN
89 #endif
90
91 // noinline
92 #ifdef _MSC_VER
93 # define FOLLY_NOINLINE __declspec(noinline)
94 #elif defined(__clang__) || defined(__GNUC__)
95 # define FOLLY_NOINLINE __attribute__((__noinline__))
96 #else
97 # define FOLLY_NOINLINE
98 #endif
99
100 // always inline
101 #ifdef _MSC_VER
102 # define FOLLY_ALWAYS_INLINE __forceinline
103 #elif defined(__clang__) || defined(__GNUC__)
104 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
105 #else
106 # define FOLLY_ALWAYS_INLINE
107 #endif
108
109 // detection for 64 bit
110 #if defined(__x86_64__) || defined(_M_X64)
111 # define FOLLY_X64 1
112 #else
113 # define FOLLY_X64 0
114 #endif
115
116 // packing is very ugly in msvc
117 #ifdef _MSC_VER
118 # define FOLLY_PACK_ATTR /**/
119 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
120 # define FOLLY_PACK_POP __pragma(pack(pop))
121 #elif defined(__clang__) || defined(__GNUC__)
122 # define FOLLY_PACK_ATTR __attribute__((__packed__))
123 # define FOLLY_PACK_PUSH /**/
124 # define FOLLY_PACK_POP /**/
125 #else
126 # define FOLLY_PACK_ATTR /**/
127 # define FOLLY_PACK_PUSH /**/
128 # define FOLLY_PACK_POP /**/
129 #endif
130
131 // portable version check
132 #ifndef __GNUC_PREREQ
133 # if defined __GNUC__ && defined __GNUC_MINOR__
134 /* nolint */
135 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
136                                    ((maj) << 16) + (min))
137 # else
138 /* nolint */
139 #  define __GNUC_PREREQ(maj, min) 0
140 # endif
141 #endif
142
143
144 /* Define macro wrappers for C++11's "final" and "override" keywords, which
145  * are supported in gcc 4.7 but not gcc 4.6. */
146 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
147 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
148 #  define FOLLY_FINAL final
149 #  define FOLLY_OVERRIDE override
150 # else
151 #  define FOLLY_FINAL /**/
152 #  define FOLLY_OVERRIDE /**/
153 # endif
154 #endif
155
156 /* Platform specific TLS support
157  * gcc implements __thread
158  * msvc implements __declspec(thread)
159  * the semantics are the same
160  * (but remember __thread has different semantics when using emutls (ex. apple))
161  */
162 #if defined(_MSC_VER)
163 # define FOLLY_TLS __declspec(thread)
164 #elif defined(__GNUC__) || defined(__clang__)
165 # define FOLLY_TLS __thread
166 #else
167 # error cannot define platform specific thread local storage
168 #endif
169
170 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
171 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
172 # if defined(__GLIBC_PREREQ)
173 #  if __GLIBC_PREREQ(2, 10)
174 #   define FOLLY_HAVE_PREADV 1
175 #   define FOLLY_HAVE_PWRITEV 1
176 #  endif
177 # endif
178 #endif
179
180 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
181 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
182 // up in a macro to make forward-declarations easier.
183 #if FOLLY_USE_LIBCPP
184 #include <__config>
185 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
186 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
187 #else
188 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
189 #define FOLLY_NAMESPACE_STD_END       }
190 #endif
191
192 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
193 // versions of these into the global namespace.
194 #if FOLLY_HAVE_CLOCK_GETTIME
195 #include <time.h>
196 #else
197 #include <folly/detail/Clock.h>
198 #endif
199
200 // Provide our own std::__throw_* wrappers for platforms that don't have them
201 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
202 #include <bits/functexcept.h>
203 #else
204 #include <folly/detail/FunctionalExcept.h>
205 #endif
206
207 #if defined(__cplusplus)
208 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
209 // usage of __has_trivial_copy(), so we can't use it as a
210 // least-common-denominator for C++11 implementations that don't support
211 // std::is_trivially_copyable<T>.
212 //
213 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
214 //
215 // As a result, use std::is_trivially_copyable() where it exists, and fall back
216 // to Boost otherwise.
217 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
218 #include <type_traits>
219 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
220   (std::is_trivially_copyable<T>::value)
221 #else
222 #include <boost/type_traits.hpp>
223 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
224   (boost::has_trivial_copy<T>::value &&                  \
225    boost::has_trivial_destructor<T>::value)
226 #endif
227 #endif // __cplusplus
228
229 // MSVC specific defines
230 // mainly for posix compat
231 #ifdef _MSC_VER
232
233 // this definition is in a really silly place with a silly name
234 // and ifdefing it every time we want it is painful
235 #include <basetsd.h>
236 typedef SSIZE_T ssize_t;
237
238 // sprintf semantics are not exactly identical
239 // but current usage is not a problem
240 # define snprintf _snprintf
241
242 // semantics here are identical
243 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
244
245 // compiler specific to compiler specific
246 // nolint
247 # define __PRETTY_FUNCTION__ __FUNCSIG__
248 #endif
249
250 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
251 namespace FOLLY_GFLAGS_NAMESPACE { }
252 namespace gflags {
253 using namespace FOLLY_GFLAGS_NAMESPACE;
254 }  // namespace gflags
255 #endif
256
257 // for TARGET_OS_IPHONE
258 #ifdef __APPLE__
259 #include <TargetConditionals.h>
260 #endif
261
262 // MacOS doesn't have malloc_usable_size()
263 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
264 inline size_t malloc_usable_size(void* ptr) {
265   return malloc_size(ptr);
266 }
267 #endif
268
269 #endif // FOLLY_PORTABILITY_H_