MSVC always_inline and noinline __attributes__ translation
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2014 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-config.h"
22 #endif
23
24 #if FOLLY_HAVE_FEATURES_H
25 #include <features.h>
26 #endif
27
28 #include "CPortability.h"
29
30 #if FOLLY_HAVE_SCHED_H
31  #include <sched.h>
32  #ifndef FOLLY_HAVE_PTHREAD_YIELD
33   #define pthread_yield sched_yield
34  #endif
35 #endif
36
37
38 // MaxAlign: max_align_t isn't supported by gcc
39 #ifdef __GNUC__
40 struct MaxAlign { char c; } __attribute__((aligned));
41 #else /* !__GNUC__ */
42 # error Cannot define MaxAlign on this platform
43 #endif
44
45
46 // noreturn
47 #if defined(__clang__) || defined(__GNUC__)
48 # define FOLLY_NORETURN __attribute__((noreturn))
49 #else
50 # define FOLLY_NORETURN
51 #endif
52
53 // noinline
54 #ifdef _MSC_VER
55 # define FOLLY_NOINLINE __declspec(noinline)
56 #elif defined(__clang__) || defined(__GNUC__)
57 # define FOLLY_NOINLINE __attribute__((noinline))
58 #else
59 # define FOLLY_NOINLINE
60 #endif
61
62 // always inline
63 #ifdef _MSC_VER
64 # define FOLLY_ALWAYS_INLINE __forceinline
65 #elif defined(__clang__) || defined(__GNUC__)
66 # define FOLLY_ALWAYS_INLINE inline __attribute__((always_inline))
67 #else
68 # define FOLLY_ALWAYS_INLINE
69 #endif
70
71 // portable version check
72 #ifndef __GNUC_PREREQ
73 # if defined __GNUC__ && defined __GNUC_MINOR__
74 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
75                                    ((maj) << 16) + (min))
76 # else
77 #  define __GNUC_PREREQ(maj, min) 0
78 # endif
79 #endif
80
81
82 /* Define macro wrappers for C++11's "final" and "override" keywords, which
83  * are supported in gcc 4.7 but not gcc 4.6. */
84 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
85 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
86 #  define FOLLY_FINAL final
87 #  define FOLLY_OVERRIDE override
88 # else
89 #  define FOLLY_FINAL /**/
90 #  define FOLLY_OVERRIDE /**/
91 # endif
92 #endif
93
94
95 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
96 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
97 # if defined(__GLIBC_PREREQ)
98 #  if __GLIBC_PREREQ(2, 10)
99 #   define FOLLY_HAVE_PREADV 1
100 #   define FOLLY_HAVE_PWRITEV 1
101 #  endif
102 # endif
103 #endif
104
105 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
106 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
107 // up in a macro to make forward-declarations easier.
108 #if FOLLY_USE_LIBCPP
109 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
110 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
111 #else
112 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
113 #define FOLLY_NAMESPACE_STD_END       }
114 #endif
115
116 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
117 // versions of these into the global namespace.
118 #if FOLLY_HAVE_CLOCK_GETTIME
119 #include <time.h>
120 #else
121 #include "folly/detail/Clock.h"
122 #endif
123
124 // Provide our own std::__throw_* wrappers for platforms that don't have them
125 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
126 #include <bits/functexcept.h>
127 #else
128 #include "folly/detail/FunctionalExcept.h"
129 #endif
130
131 #if defined(__cplusplus)
132 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
133 // usage of __has_trivial_copy(), so we can't use it as a
134 // least-common-denominator for C++11 implementations that don't support
135 // std::is_trivially_copyable<T>.
136 //
137 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
138 //
139 // As a result, use std::is_trivially_copyable() where it exists, and fall back
140 // to Boost otherwise.
141 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
142 #include <type_traits>
143 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
144   (std::is_trivially_copyable<T>::value)
145 #else
146 #include <boost/type_traits.hpp>
147 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
148   (boost::has_trivial_copy<T>::value &&                  \
149    boost::has_trivial_destructor<T>::value)
150 #endif
151 #endif // __cplusplus
152
153 #endif // FOLLY_PORTABILITY_H_