msvc packed attribute 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 // compiler specific attribute translation
46 // msvc should come first, so if clang is in msvc mode it gets the right defines
47
48 // noreturn
49 #if defined(_MSC_VER)
50 # define FOLLY_NORETURN __declspec(noreturn)
51 #elif defined(__clang__) || defined(__GNUC__)
52 # define FOLLY_NORETURN __attribute__((noreturn))
53 #else
54 # define FOLLY_NORETURN
55 #endif
56
57 // noinline
58 #ifdef _MSC_VER
59 # define FOLLY_NOINLINE __declspec(noinline)
60 #elif defined(__clang__) || defined(__GNUC__)
61 # define FOLLY_NOINLINE __attribute__((noinline))
62 #else
63 # define FOLLY_NOINLINE
64 #endif
65
66 // always inline
67 #ifdef _MSC_VER
68 # define FOLLY_ALWAYS_INLINE __forceinline
69 #elif defined(__clang__) || defined(__GNUC__)
70 # define FOLLY_ALWAYS_INLINE inline __attribute__((always_inline))
71 #else
72 # define FOLLY_ALWAYS_INLINE
73 #endif
74
75 // detection for 64 bit
76 #if defined(__x86_64__) || defined(_M_X64)
77 # define FOLLY_X64 1
78 #else
79 # define FOLLY_X64 0
80 #endif
81
82 // packing is very ugly in msvc
83 #ifdef _MSC_VER
84 # define FOLLY_PACK_ATTR /**/
85 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
86 # define FOLLY_PACK_POP __pragma(pack(pop))
87 #elif defined(__clang__) || defined(__GNUC__)
88 # define FOLLY_PACK_ATTR __attribute__((packed))
89 # define FOLLY_PACK_PUSH /**/
90 # define FOLLY_PACK_POP /**/
91 #else
92 # define FOLLY_PACK_ATTR /**/
93 # define FOLLY_PACK_PUSH /**/
94 # define FOLLY_PACK_POP /**/
95 #endif
96
97 // portable version check
98 #ifndef __GNUC_PREREQ
99 # if defined __GNUC__ && defined __GNUC_MINOR__
100 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
101                                    ((maj) << 16) + (min))
102 # else
103 #  define __GNUC_PREREQ(maj, min) 0
104 # endif
105 #endif
106
107
108 /* Define macro wrappers for C++11's "final" and "override" keywords, which
109  * are supported in gcc 4.7 but not gcc 4.6. */
110 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
111 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
112 #  define FOLLY_FINAL final
113 #  define FOLLY_OVERRIDE override
114 # else
115 #  define FOLLY_FINAL /**/
116 #  define FOLLY_OVERRIDE /**/
117 # endif
118 #endif
119
120 /* Platform specific TLS support
121  * gcc implements __thread
122  * msvc implements __declspec(thread)
123  * the semantics are the same (but remember __thread is broken on apple)
124  */
125 #if defined(_MSC_VER)
126 # define FOLLY_TLS __declspec(thread)
127 #elif defined(__GNUC__) || defined(__clang__)
128 # define FOLLY_TLS __thread
129 #else
130 # error cannot define platform specific thread local storage
131 #endif
132
133 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
134 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
135 # if defined(__GLIBC_PREREQ)
136 #  if __GLIBC_PREREQ(2, 10)
137 #   define FOLLY_HAVE_PREADV 1
138 #   define FOLLY_HAVE_PWRITEV 1
139 #  endif
140 # endif
141 #endif
142
143 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
144 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
145 // up in a macro to make forward-declarations easier.
146 #if FOLLY_USE_LIBCPP
147 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
148 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
149 #else
150 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
151 #define FOLLY_NAMESPACE_STD_END       }
152 #endif
153
154 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
155 // versions of these into the global namespace.
156 #if FOLLY_HAVE_CLOCK_GETTIME
157 #include <time.h>
158 #else
159 #include "folly/detail/Clock.h"
160 #endif
161
162 // Provide our own std::__throw_* wrappers for platforms that don't have them
163 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
164 #include <bits/functexcept.h>
165 #else
166 #include "folly/detail/FunctionalExcept.h"
167 #endif
168
169 #if defined(__cplusplus)
170 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
171 // usage of __has_trivial_copy(), so we can't use it as a
172 // least-common-denominator for C++11 implementations that don't support
173 // std::is_trivially_copyable<T>.
174 //
175 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
176 //
177 // As a result, use std::is_trivially_copyable() where it exists, and fall back
178 // to Boost otherwise.
179 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
180 #include <type_traits>
181 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
182   (std::is_trivially_copyable<T>::value)
183 #else
184 #include <boost/type_traits.hpp>
185 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
186   (boost::has_trivial_copy<T>::value &&                  \
187    boost::has_trivial_destructor<T>::value)
188 #endif
189 #endif // __cplusplus
190
191 #endif // FOLLY_PORTABILITY_H_