GRPC Core  0.11.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 #if defined(_WIN32_WINNT)
50 #if _WIN32_WINNT < 0x0600
51 #undef _WIN32_WINNT
52 #define _WIN32_WINNT 0x0600
53 #endif /* _WIN32_WINNT < 0x0600 */
54 #endif /* defined(_WIN32_WINNT) */
55 
56 #include <windows.h>
57 
58 #ifdef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
59 #undef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
60 #undef WIN32_LEAN_AND_MEAN
61 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
62 
63 #ifdef GRPC_NOMINMAX_WAS_NOT_DEFINED
64 #undef GRPC_NOMINMAX_WAS_NOT_DEFINED
65 #undef NOMINMAX
66 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
67 #endif /* defined(_WIN64) || defined(WIN64) || defined(_WIN32) || \
68  defined(WIN32) */
69 
70 /* Override this file with one for your platform if you need to redefine
71  things. */
72 
73 #if !defined(GPR_NO_AUTODETECT_PLATFORM)
74 #if defined(_WIN64) || defined(WIN64)
75 #define GPR_PLATFORM_STRING "windows"
76 #define GPR_WIN32 1
77 #define GPR_ARCH_64 1
78 #define GPR_GETPID_IN_PROCESS_H 1
79 #define GPR_WINSOCK_SOCKET 1
80 #ifdef __GNUC__
81 #define GPR_GCC_ATOMIC 1
82 #define GPR_GCC_TLS 1
83 #else
84 #define GPR_WIN32_ATOMIC 1
85 #define GPR_MSVC_TLS 1
86 #endif
87 #define GPR_WINDOWS_CRASH_HANDLER 1
88 #elif defined(_WIN32) || defined(WIN32)
89 #define GPR_PLATFORM_STRING "windows"
90 #define GPR_ARCH_32 1
91 #define GPR_WIN32 1
92 #define GPR_GETPID_IN_PROCESS_H 1
93 #define GPR_WINSOCK_SOCKET 1
94 #ifdef __GNUC__
95 #define GPR_GCC_ATOMIC 1
96 #define GPR_GCC_TLS 1
97 #else
98 #define GPR_WIN32_ATOMIC 1
99 #define GPR_MSVC_TLS 1
100 #endif
101 #define GPR_WINDOWS_CRASH_HANDLER 1
102 #elif defined(ANDROID) || defined(__ANDROID__)
103 #define GPR_PLATFORM_STRING "android"
104 #define GPR_ANDROID 1
105 #define GPR_ARCH_32 1
106 #define GPR_CPU_LINUX 1
107 #define GPR_GCC_SYNC 1
108 #define GPR_GCC_TLS 1
109 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
110 #define GPR_POSIX_WAKEUP_FD 1
111 #define GPR_LINUX_EVENTFD 1
112 #define GPR_POSIX_SOCKET 1
113 #define GPR_POSIX_SOCKETADDR 1
114 #define GPR_POSIX_SOCKETUTILS 1
115 #define GPR_POSIX_ENV 1
116 #define GPR_POSIX_FILE 1
117 #define GPR_POSIX_STRING 1
118 #define GPR_POSIX_SUBPROCESS 1
119 #define GPR_POSIX_SYNC 1
120 #define GPR_POSIX_TIME 1
121 #define GPR_GETPID_IN_UNISTD_H 1
122 #define GPR_HAVE_MSG_NOSIGNAL 1
123 #elif defined(__linux__)
124 #define GPR_PLATFORM_STRING "linux"
125 #ifndef _BSD_SOURCE
126 #define _BSD_SOURCE
127 #endif
128 #ifndef _DEFAULT_SOURCE
129 #define _DEFAULT_SOURCE
130 #endif
131 #ifndef _GNU_SOURCE
132 #define _GNU_SOURCE
133 #endif
134 #include <features.h>
135 #define GPR_CPU_LINUX 1
136 #define GPR_GCC_ATOMIC 1
137 #define GPR_GCC_TLS 1
138 #define GPR_LINUX 1
139 #define GPR_LINUX_MULTIPOLL_WITH_EPOLL 1
140 #define GPR_POSIX_WAKEUP_FD 1
141 #define GPR_POSIX_SOCKET 1
142 #define GPR_POSIX_SOCKETADDR 1
143 #ifdef __GLIBC_PREREQ
144 #if __GLIBC_PREREQ(2, 9)
145 #define GPR_LINUX_EVENTFD 1
146 #endif
147 #if __GLIBC_PREREQ(2, 10)
148 #define GPR_LINUX_SOCKETUTILS 1
149 #endif
150 #if __GLIBC_PREREQ(2, 17)
151 #define GPR_LINUX_ENV 1
152 #endif
153 #endif
154 #ifndef GPR_LINUX_EVENTFD
155 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
156 #endif
157 #ifndef GPR_LINUX_SOCKETUTILS
158 #define GPR_POSIX_SOCKETUTILS
159 #endif
160 #ifndef GPR_LINUX_ENV
161 #define GPR_POSIX_ENV 1
162 #endif
163 #define GPR_POSIX_FILE 1
164 #define GPR_POSIX_STRING 1
165 #define GPR_POSIX_SUBPROCESS 1
166 #define GPR_POSIX_SYNC 1
167 #define GPR_POSIX_TIME 1
168 #define GPR_GETPID_IN_UNISTD_H 1
169 #define GPR_HAVE_MSG_NOSIGNAL 1
170 #ifdef _LP64
171 #define GPR_ARCH_64 1
172 #else /* _LP64 */
173 #define GPR_ARCH_32 1
174 #endif /* _LP64 */
175 #elif defined(__APPLE__)
176 #include <TargetConditionals.h>
177 /* Provides IPV6_RECVPKTINFO */
178 #define __APPLE_USE_RFC_3542
179 #ifndef _BSD_SOURCE
180 #define _BSD_SOURCE
181 #endif
182 #if TARGET_OS_IPHONE
183 #define GPR_PLATFORM_STRING "ios"
184 #define GPR_CPU_IPHONE 1
185 #define GPR_PTHREAD_TLS 1
186 #else /* TARGET_OS_IPHONE */
187 #define GPR_PLATFORM_STRING "osx"
188 #define GPR_CPU_POSIX 1
189 #define GPR_GCC_TLS 1
190 #endif
191 #define GPR_GCC_ATOMIC 1
192 #define GPR_POSIX_LOG 1
193 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
194 #define GPR_POSIX_WAKEUP_FD 1
195 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
196 #define GPR_POSIX_SOCKET 1
197 #define GPR_POSIX_SOCKETADDR 1
198 #define GPR_POSIX_SOCKETUTILS 1
199 #define GPR_POSIX_ENV 1
200 #define GPR_POSIX_FILE 1
201 #define GPR_POSIX_STRING 1
202 #define GPR_POSIX_SUBPROCESS 1
203 #define GPR_POSIX_SYNC 1
204 #define GPR_POSIX_TIME 1
205 #define GPR_GETPID_IN_UNISTD_H 1
206 #define GPR_HAVE_SO_NOSIGPIPE 1
207 #ifdef _LP64
208 #define GPR_ARCH_64 1
209 #else /* _LP64 */
210 #define GPR_ARCH_32 1
211 #endif /* _LP64 */
212 #elif defined(__FreeBSD__)
213 #define GPR_PLATFORM_STRING "freebsd"
214 #ifndef _BSD_SOURCE
215 #define _BSD_SOURCE
216 #endif
217 #define GPR_CPU_POSIX 1
218 #define GPR_GCC_ATOMIC 1
219 #define GPR_GCC_TLS 1
220 #define GPR_POSIX_LOG 1
221 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
222 #define GPR_POSIX_WAKEUP_FD 1
223 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
224 #define GPR_POSIX_SOCKET 1
225 #define GPR_POSIX_SOCKETADDR 1
226 #define GPR_POSIX_SOCKETUTILS 1
227 #define GPR_POSIX_ENV 1
228 #define GPR_POSIX_FILE 1
229 #define GPR_POSIX_STRING 1
230 #define GPR_POSIX_SUBPROCESS 1
231 #define GPR_POSIX_SYNC 1
232 #define GPR_POSIX_TIME 1
233 #define GPR_GETPID_IN_UNISTD_H 1
234 #define GPR_HAVE_SO_NOSIGPIPE 1
235 #ifdef _LP64
236 #define GPR_ARCH_64 1
237 #else /* _LP64 */
238 #define GPR_ARCH_32 1
239 #endif /* _LP64 */
240 #else
241 #error Could not auto-detect platform
242 #endif
243 #endif /* GPR_NO_AUTODETECT_PLATFORM */
244 
245 #ifndef GPR_PLATFORM_STRING
246 #warning "GPR_PLATFORM_STRING not auto-detected"
247 #define GPR_PLATFORM_STRING "unknown"
248 #endif
249 
250 /* For a common case, assume that the platform has a C99-like stdint.h */
251 
252 #include <stdint.h>
253 
254 /* Cache line alignment */
255 #ifndef GPR_CACHELINE_SIZE_LOG
256 #if defined(__i386__) || defined(__x86_64__)
257 #define GPR_CACHELINE_SIZE_LOG 6
258 #endif
259 #ifndef GPR_CACHELINE_SIZE_LOG
260 /* A reasonable default guess. Note that overestimates tend to waste more
261  space, while underestimates tend to waste more time. */
262 #define GPR_CACHELINE_SIZE_LOG 6
263 #endif /* GPR_CACHELINE_SIZE_LOG */
264 #endif /* GPR_CACHELINE_SIZE_LOG */
265 
266 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
267 
268 /* scrub GCC_ATOMIC if it's not available on this compiler */
269 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
270 #undef GPR_GCC_ATOMIC
271 #define GPR_GCC_SYNC 1
272 #endif
273 
274 /* Validate platform combinations */
275 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
276  defined(GPR_WIN32_ATOMIC) != \
277  1
278 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WIN32_ATOMIC
279 #endif
280 
281 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
282 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
283 #endif
284 
285 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WIN32) + \
286  defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
287  1
288 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WIN32, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
289 #endif
290 
291 #if defined(GPR_POSIX_MULTIPOLL_WITH_POLL) && !defined(GPR_POSIX_SOCKET)
292 #error Must define GPR_POSIX_SOCKET to use GPR_POSIX_MULTIPOLL_WITH_POLL
293 #endif
294 
295 #if defined(GPR_POSIX_SOCKET) + defined(GPR_WINSOCK_SOCKET) + \
296  defined(GPR_CUSTOM_SOCKET) != \
297  1
298 #error Must define exactly one of GPR_POSIX_SOCKET, GPR_WINSOCK_SOCKET, GPR_CUSTOM_SOCKET
299 #endif
300 
301 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + \
302  defined(GPR_CUSTOM_TLS) != \
303  1
304 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_CUSTOM_TLS
305 #endif
306 
307 typedef int16_t gpr_int16;
308 typedef int32_t gpr_int32;
309 typedef int64_t gpr_int64;
310 typedef uint8_t gpr_uint8;
311 typedef uint16_t gpr_uint16;
312 typedef uint32_t gpr_uint32;
313 typedef uint64_t gpr_uint64;
314 typedef intmax_t gpr_intmax;
315 typedef intptr_t gpr_intptr;
316 typedef uintmax_t gpr_uintmax;
317 typedef uintptr_t gpr_uintptr;
318 
319 /* INT64_MAX is unavailable on some platforms. */
320 #define GPR_INT64_MAX (gpr_int64)(~(gpr_uint64)0 >> 1)
321 
322 /* maximum alignment needed for any type on this platform, rounded up to a
323  power of two */
324 #define GPR_MAX_ALIGNMENT 16
325 
326 #ifndef GRPC_MUST_USE_RESULT
327 #ifdef __GNUC__
328 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
329 #else
330 #define GRPC_MUST_USE_RESULT
331 #endif
332 #endif
333 
334 #endif /* GRPC_SUPPORT_PORT_PLATFORM_H */
intptr_t gpr_intptr
Definition: port_platform.h:315
uint8_t gpr_uint8
Definition: port_platform.h:310
uintptr_t gpr_uintptr
Definition: port_platform.h:317
uint32_t gpr_uint32
Definition: port_platform.h:312
uintmax_t gpr_uintmax
Definition: port_platform.h:316
int64_t gpr_int64
Definition: port_platform.h:309
uint16_t gpr_uint16
Definition: port_platform.h:311
int32_t gpr_int32
Definition: port_platform.h:308
uint64_t gpr_uint64
Definition: port_platform.h:313
intmax_t gpr_intmax
Definition: port_platform.h:314
int16_t gpr_int16
Definition: port_platform.h:307