2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_PORTABILITY_H_
18 #define FOLLY_PORTABILITY_H_
20 #ifndef FOLLY_NO_CONFIG
21 #include "folly-config.h"
24 #if FOLLY_HAVE_FEATURES_H
28 #include "CPortability.h"
30 #if FOLLY_HAVE_SCHED_H
32 #ifndef FOLLY_HAVE_PTHREAD_YIELD
33 #define pthread_yield sched_yield
37 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
38 // MAP_ANON on OSX/BSD.
39 #if defined(__APPLE__) || defined(__FreeBSD__)
43 #define MAP_ANONYMOUS MAP_ANON
48 // MaxAlign: max_align_t isn't supported by gcc
50 struct MaxAlign { char c; } __attribute__((aligned));
52 # error Cannot define MaxAlign on this platform
55 // compiler specific attribute translation
56 // msvc should come first, so if clang is in msvc mode it gets the right defines
58 // NOTE: this will only do checking in msvc with versions that support /analyze
60 # ifdef _USE_ATTRIBUTES_FOR_SAL
61 # undef _USE_ATTRIBUTES_FOR_SAL
63 # define _USE_ATTRIBUTES_FOR_SAL 1
65 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
66 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
68 # define FOLLY_PRINTF_FORMAT /**/
69 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
70 __attribute__((format(printf, format_param, dots_param)))
75 # define FOLLY_NORETURN __declspec(noreturn)
76 #elif defined(__clang__) || defined(__GNUC__)
77 # define FOLLY_NORETURN __attribute__((noreturn))
79 # define FOLLY_NORETURN
84 # define FOLLY_NOINLINE __declspec(noinline)
85 #elif defined(__clang__) || defined(__GNUC__)
86 # define FOLLY_NOINLINE __attribute__((noinline))
88 # define FOLLY_NOINLINE
93 # define FOLLY_ALWAYS_INLINE __forceinline
94 #elif defined(__clang__) || defined(__GNUC__)
95 # define FOLLY_ALWAYS_INLINE inline __attribute__((always_inline))
97 # define FOLLY_ALWAYS_INLINE
100 // detection for 64 bit
101 #if defined(__x86_64__) || defined(_M_X64)
107 // packing is very ugly in msvc
109 # define FOLLY_PACK_ATTR /**/
110 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
111 # define FOLLY_PACK_POP __pragma(pack(pop))
112 #elif defined(__clang__) || defined(__GNUC__)
113 # define FOLLY_PACK_ATTR __attribute__((packed))
114 # define FOLLY_PACK_PUSH /**/
115 # define FOLLY_PACK_POP /**/
117 # define FOLLY_PACK_ATTR /**/
118 # define FOLLY_PACK_PUSH /**/
119 # define FOLLY_PACK_POP /**/
122 // portable version check
123 #ifndef __GNUC_PREREQ
124 # if defined __GNUC__ && defined __GNUC_MINOR__
125 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
126 ((maj) << 16) + (min))
128 # define __GNUC_PREREQ(maj, min) 0
133 /* Define macro wrappers for C++11's "final" and "override" keywords, which
134 * are supported in gcc 4.7 but not gcc 4.6. */
135 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
136 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
137 # define FOLLY_FINAL final
138 # define FOLLY_OVERRIDE override
140 # define FOLLY_FINAL /**/
141 # define FOLLY_OVERRIDE /**/
145 /* Platform specific TLS support
146 * gcc implements __thread
147 * msvc implements __declspec(thread)
148 * the semantics are the same (but remember __thread is broken on apple)
150 #if defined(_MSC_VER)
151 # define FOLLY_TLS __declspec(thread)
152 #elif defined(__GNUC__) || defined(__clang__)
153 # define FOLLY_TLS __thread
155 # error cannot define platform specific thread local storage
158 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
159 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
160 # if defined(__GLIBC_PREREQ)
161 # if __GLIBC_PREREQ(2, 10)
162 # define FOLLY_HAVE_PREADV 1
163 # define FOLLY_HAVE_PWRITEV 1
168 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
169 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
170 // up in a macro to make forward-declarations easier.
172 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
173 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
175 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
176 #define FOLLY_NAMESPACE_STD_END }
179 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
180 // versions of these into the global namespace.
181 #if FOLLY_HAVE_CLOCK_GETTIME
184 #include <folly/detail/Clock.h>
187 // Provide our own std::__throw_* wrappers for platforms that don't have them
188 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
189 #include <bits/functexcept.h>
191 #include <folly/detail/FunctionalExcept.h>
194 #if defined(__cplusplus)
195 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
196 // usage of __has_trivial_copy(), so we can't use it as a
197 // least-common-denominator for C++11 implementations that don't support
198 // std::is_trivially_copyable<T>.
200 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
202 // As a result, use std::is_trivially_copyable() where it exists, and fall back
203 // to Boost otherwise.
204 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
205 #include <type_traits>
206 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
207 (std::is_trivially_copyable<T>::value)
209 #include <boost/type_traits.hpp>
210 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
211 (boost::has_trivial_copy<T>::value && \
212 boost::has_trivial_destructor<T>::value)
214 #endif // __cplusplus
216 // MSVC specific defines
217 // mainly for posix compat
220 // this definition is in a really silly place with a silly name
221 // and ifdefing it every time we want it is painful
223 typedef SSIZE_T ssize_t;
225 // sprintf semantics are not exactly identical
226 // but current usage is not a problem
227 # define snprintf _snprintf
229 // semantics here are identical
230 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
232 // compiler specific to compiler specific
233 # define __PRETTY_FUNCTION__ __FUNCSIG__
236 #endif // FOLLY_PORTABILITY_H_