GRPC Core  0.12.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
port_platform.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_SUPPORT_PORT_PLATFORM_H
35 #define GRPC_SUPPORT_PORT_PLATFORM_H
36 
37 /* Get windows.h included everywhere (we need it) */
38 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
39 #ifndef WIN32_LEAN_AND_MEAN
40 #define GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
41 #define WIN32_LEAN_AND_MEAN
42 #endif /* WIN32_LEAN_AND_MEAN */
43 
44 #ifndef NOMINMAX
45 #define GRPC_NOMINMX_WAS_NOT_DEFINED
46 #define NOMINMAX
47 #endif /* NOMINMAX */
48 
49 #ifndef _WIN32_WINNT
50 #error \
51  "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
52 #else /* !defined(_WIN32_WINNT) */
53 #if (_WIN32_WINNT < 0x0600)
54 #error \
55  "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
56 #endif /* _WIN32_WINNT < 0x0600 */
57 #endif /* defined(_WIN32_WINNT) */
58 
59 #include <windows.h>
60 
61 #ifdef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
62 #undef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
63 #undef WIN32_LEAN_AND_MEAN
64 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
65 
66 #ifdef GRPC_NOMINMAX_WAS_NOT_DEFINED
67 #undef GRPC_NOMINMAX_WAS_NOT_DEFINED
68 #undef NOMINMAX
69 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
70 #endif /* defined(_WIN64) || defined(WIN64) || defined(_WIN32) || \
71  defined(WIN32) */
72 
73 /* Override this file with one for your platform if you need to redefine
74  things. */
75 
76 #if !defined(GPR_NO_AUTODETECT_PLATFORM)
77 #if defined(_WIN64) || defined(WIN64)
78 #define GPR_PLATFORM_STRING "windows"
79 #define GPR_WIN32 1
80 #define GPR_ARCH_64 1
81 #define GPR_GETPID_IN_PROCESS_H 1
82 #define GPR_WINSOCK_SOCKET 1
83 #ifdef __GNUC__
84 #define GPR_GCC_ATOMIC 1
85 #define GPR_GCC_TLS 1
86 #else
87 #define GPR_WIN32_ATOMIC 1
88 #define GPR_MSVC_TLS 1
89 #endif
90 #define GPR_WINDOWS_CRASH_HANDLER 1
91 #elif defined(_WIN32) || defined(WIN32)
92 #define GPR_PLATFORM_STRING "windows"
93 #define GPR_ARCH_32 1
94 #define GPR_WIN32 1
95 #define GPR_GETPID_IN_PROCESS_H 1
96 #define GPR_WINSOCK_SOCKET 1
97 #ifdef __GNUC__
98 #define GPR_GCC_ATOMIC 1
99 #define GPR_GCC_TLS 1
100 #else
101 #define GPR_WIN32_ATOMIC 1
102 #define GPR_MSVC_TLS 1
103 #endif
104 #define GPR_WINDOWS_CRASH_HANDLER 1
105 #elif defined(ANDROID) || defined(__ANDROID__)
106 #define GPR_PLATFORM_STRING "android"
107 #define GPR_ANDROID 1
108 #define GPR_ARCH_32 1
109 #define GPR_CPU_LINUX 1
110 #define GPR_GCC_SYNC 1
111 #define GPR_GCC_TLS 1
112 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
113 #define GPR_POSIX_WAKEUP_FD 1
114 #define GPR_LINUX_EVENTFD 1
115 #define GPR_POSIX_SOCKET 1
116 #define GPR_POSIX_SOCKETADDR 1
117 #define GPR_POSIX_SOCKETUTILS 1
118 #define GPR_POSIX_ENV 1
119 #define GPR_POSIX_FILE 1
120 #define GPR_POSIX_STRING 1
121 #define GPR_POSIX_SUBPROCESS 1
122 #define GPR_POSIX_SYNC 1
123 #define GPR_POSIX_TIME 1
124 #define GPR_GETPID_IN_UNISTD_H 1
125 #define GPR_HAVE_MSG_NOSIGNAL 1
126 #elif defined(__linux__)
127 #define GPR_POSIX_CRASH_HANDLER 1
128 #define GPR_PLATFORM_STRING "linux"
129 #ifndef _BSD_SOURCE
130 #define _BSD_SOURCE
131 #endif
132 #ifndef _DEFAULT_SOURCE
133 #define _DEFAULT_SOURCE
134 #endif
135 #ifndef _GNU_SOURCE
136 #define _GNU_SOURCE
137 #endif
138 #include <features.h>
139 #define GPR_CPU_LINUX 1
140 #define GPR_GCC_ATOMIC 1
141 #define GPR_GCC_TLS 1
142 #define GPR_LINUX 1
143 #define GPR_LINUX_MULTIPOLL_WITH_EPOLL 1
144 #define GPR_POSIX_WAKEUP_FD 1
145 #define GPR_POSIX_SOCKET 1
146 #define GPR_POSIX_SOCKETADDR 1
147 #ifdef __GLIBC_PREREQ
148 #if __GLIBC_PREREQ(2, 9)
149 #define GPR_LINUX_EVENTFD 1
150 #endif
151 #if __GLIBC_PREREQ(2, 10)
152 #define GPR_LINUX_SOCKETUTILS 1
153 #endif
154 #if __GLIBC_PREREQ(2, 17)
155 #define GPR_LINUX_ENV 1
156 #endif
157 #endif
158 #ifndef GPR_LINUX_EVENTFD
159 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
160 #endif
161 #ifndef GPR_LINUX_SOCKETUTILS
162 #define GPR_POSIX_SOCKETUTILS
163 #endif
164 #ifndef GPR_LINUX_ENV
165 #define GPR_POSIX_ENV 1
166 #endif
167 #define GPR_POSIX_FILE 1
168 #define GPR_POSIX_STRING 1
169 #define GPR_POSIX_SUBPROCESS 1
170 #define GPR_POSIX_SYNC 1
171 #define GPR_POSIX_TIME 1
172 #define GPR_GETPID_IN_UNISTD_H 1
173 #define GPR_HAVE_MSG_NOSIGNAL 1
174 #ifdef _LP64
175 #define GPR_ARCH_64 1
176 #else /* _LP64 */
177 #define GPR_ARCH_32 1
178 #endif /* _LP64 */
179 #elif defined(__APPLE__)
180 #include <TargetConditionals.h>
181 #ifndef _BSD_SOURCE
182 #define _BSD_SOURCE
183 #endif
184 #define GPR_MSG_IOVLEN_TYPE int
185 #if TARGET_OS_IPHONE
186 #define GPR_FORBID_UNREACHABLE_CODE 1
187 #define GPR_PLATFORM_STRING "ios"
188 #define GPR_CPU_IPHONE 1
189 #define GPR_PTHREAD_TLS 1
190 #else /* TARGET_OS_IPHONE */
191 #define GPR_PLATFORM_STRING "osx"
192 #define GPR_CPU_POSIX 1
193 #define GPR_GCC_TLS 1
194 #define GPR_POSIX_CRASH_HANDLER 1
195 #endif
196 #define GPR_GCC_ATOMIC 1
197 #define GPR_POSIX_LOG 1
198 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
199 #define GPR_POSIX_WAKEUP_FD 1
200 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
201 #define GPR_POSIX_SOCKET 1
202 #define GPR_POSIX_SOCKETADDR 1
203 #define GPR_POSIX_SOCKETUTILS 1
204 #define GPR_POSIX_ENV 1
205 #define GPR_POSIX_FILE 1
206 #define GPR_POSIX_STRING 1
207 #define GPR_POSIX_SUBPROCESS 1
208 #define GPR_POSIX_SYNC 1
209 #define GPR_POSIX_TIME 1
210 #define GPR_GETPID_IN_UNISTD_H 1
211 #define GPR_HAVE_SO_NOSIGPIPE 1
212 #ifdef _LP64
213 #define GPR_ARCH_64 1
214 #else /* _LP64 */
215 #define GPR_ARCH_32 1
216 #endif /* _LP64 */
217 #elif defined(__FreeBSD__)
218 #define GPR_PLATFORM_STRING "freebsd"
219 #ifndef _BSD_SOURCE
220 #define _BSD_SOURCE
221 #endif
222 #define GPR_CPU_POSIX 1
223 #define GPR_GCC_ATOMIC 1
224 #define GPR_GCC_TLS 1
225 #define GPR_POSIX_LOG 1
226 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
227 #define GPR_POSIX_WAKEUP_FD 1
228 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
229 #define GPR_POSIX_SOCKET 1
230 #define GPR_POSIX_SOCKETADDR 1
231 #define GPR_POSIX_SOCKETUTILS 1
232 #define GPR_POSIX_ENV 1
233 #define GPR_POSIX_FILE 1
234 #define GPR_POSIX_STRING 1
235 #define GPR_POSIX_SUBPROCESS 1
236 #define GPR_POSIX_SYNC 1
237 #define GPR_POSIX_TIME 1
238 #define GPR_GETPID_IN_UNISTD_H 1
239 #define GPR_HAVE_SO_NOSIGPIPE 1
240 #ifdef _LP64
241 #define GPR_ARCH_64 1
242 #else /* _LP64 */
243 #define GPR_ARCH_32 1
244 #endif /* _LP64 */
245 #else
246 #error Could not auto-detect platform
247 #endif
248 #endif /* GPR_NO_AUTODETECT_PLATFORM */
249 
250 #ifndef GPR_PLATFORM_STRING
251 #warning "GPR_PLATFORM_STRING not auto-detected"
252 #define GPR_PLATFORM_STRING "unknown"
253 #endif
254 
255 #ifdef GPR_GCOV
256 #undef GPR_FORBID_UNREACHABLE_CODE
257 #define GPR_FORBID_UNREACHABLE_CODE 1
258 #endif
259 
260 /* For a common case, assume that the platform has a C99-like stdint.h */
261 
262 #include <stdint.h>
263 
264 /* Cache line alignment */
265 #ifndef GPR_CACHELINE_SIZE_LOG
266 #if defined(__i386__) || defined(__x86_64__)
267 #define GPR_CACHELINE_SIZE_LOG 6
268 #endif
269 #ifndef GPR_CACHELINE_SIZE_LOG
270 /* A reasonable default guess. Note that overestimates tend to waste more
271  space, while underestimates tend to waste more time. */
272 #define GPR_CACHELINE_SIZE_LOG 6
273 #endif /* GPR_CACHELINE_SIZE_LOG */
274 #endif /* GPR_CACHELINE_SIZE_LOG */
275 
276 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
277 
278 /* scrub GCC_ATOMIC if it's not available on this compiler */
279 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
280 #undef GPR_GCC_ATOMIC
281 #define GPR_GCC_SYNC 1
282 #endif
283 
284 /* Validate platform combinations */
285 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
286  defined(GPR_WIN32_ATOMIC) != \
287  1
288 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WIN32_ATOMIC
289 #endif
290 
291 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
292 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
293 #endif
294 
295 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WIN32) + \
296  defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
297  1
298 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WIN32, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
299 #endif
300 
301 #if defined(GPR_POSIX_MULTIPOLL_WITH_POLL) && !defined(GPR_POSIX_SOCKET)
302 #error Must define GPR_POSIX_SOCKET to use GPR_POSIX_MULTIPOLL_WITH_POLL
303 #endif
304 
305 #if defined(GPR_POSIX_SOCKET) + defined(GPR_WINSOCK_SOCKET) + \
306  defined(GPR_CUSTOM_SOCKET) != \
307  1
308 #error Must define exactly one of GPR_POSIX_SOCKET, GPR_WINSOCK_SOCKET, GPR_CUSTOM_SOCKET
309 #endif
310 
311 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + \
312  defined(GPR_CUSTOM_TLS) != \
313  1
314 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_CUSTOM_TLS
315 #endif
316 
317 typedef int16_t gpr_int16;
318 typedef int32_t gpr_int32;
319 typedef int64_t gpr_int64;
320 typedef uint8_t gpr_uint8;
321 typedef uint16_t gpr_uint16;
322 typedef uint32_t gpr_uint32;
323 typedef uint64_t gpr_uint64;
324 typedef intmax_t gpr_intmax;
325 typedef intptr_t gpr_intptr;
326 typedef uintmax_t gpr_uintmax;
327 typedef uintptr_t gpr_uintptr;
328 
329 /* INT64_MAX is unavailable on some platforms. */
330 #define GPR_INT64_MAX (gpr_int64)(~(gpr_uint64)0 >> 1)
331 #define GPR_UINT32_MAX (~(gpr_uint32)0)
332 
333 /* maximum alignment needed for any type on this platform, rounded up to a
334  power of two */
335 #define GPR_MAX_ALIGNMENT 16
336 
337 #ifndef GRPC_MUST_USE_RESULT
338 #ifdef __GNUC__
339 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
340 #else
341 #define GRPC_MUST_USE_RESULT
342 #endif
343 #endif
344 
345 #if GPR_FORBID_UNREACHABLE_CODE
346 #define GPR_UNREACHABLE_CODE(STATEMENT)
347 #else
348 #define GPR_UNREACHABLE_CODE(STATEMENT) \
349  do { \
350  gpr_log(GPR_ERROR, "Should never reach here."); \
351  abort(); \
352  STATEMENT; \
353  } while (0)
354 #endif /* GPR_FORBID_UNREACHABLE_CODE */
355 
356 #endif /* GRPC_SUPPORT_PORT_PLATFORM_H */
intptr_t gpr_intptr
Definition: port_platform.h:325
uint8_t gpr_uint8
Definition: port_platform.h:320
uintptr_t gpr_uintptr
Definition: port_platform.h:327
uint32_t gpr_uint32
Definition: port_platform.h:322
uintmax_t gpr_uintmax
Definition: port_platform.h:326
int64_t gpr_int64
Definition: port_platform.h:319
uint16_t gpr_uint16
Definition: port_platform.h:321
int32_t gpr_int32
Definition: port_platform.h:318
uint64_t gpr_uint64
Definition: port_platform.h:323
intmax_t gpr_intmax
Definition: port_platform.h:324
int16_t gpr_int16
Definition: port_platform.h:317