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 #ifndef _BSD_SOURCE
178 #define _BSD_SOURCE
179 #endif
180 #if TARGET_OS_IPHONE
181 #define GPR_PLATFORM_STRING "ios"
182 #define GPR_CPU_IPHONE 1
183 #define GPR_PTHREAD_TLS 1
184 #else /* TARGET_OS_IPHONE */
185 #define GPR_PLATFORM_STRING "osx"
186 #define GPR_CPU_POSIX 1
187 #define GPR_GCC_TLS 1
188 #endif
189 #define GPR_GCC_ATOMIC 1
190 #define GPR_POSIX_LOG 1
191 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
192 #define GPR_POSIX_WAKEUP_FD 1
193 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
194 #define GPR_POSIX_SOCKET 1
195 #define GPR_POSIX_SOCKETADDR 1
196 #define GPR_POSIX_SOCKETUTILS 1
197 #define GPR_POSIX_ENV 1
198 #define GPR_POSIX_FILE 1
199 #define GPR_POSIX_STRING 1
200 #define GPR_POSIX_SUBPROCESS 1
201 #define GPR_POSIX_SYNC 1
202 #define GPR_POSIX_TIME 1
203 #define GPR_GETPID_IN_UNISTD_H 1
204 #define GPR_HAVE_SO_NOSIGPIPE 1
205 #ifdef _LP64
206 #define GPR_ARCH_64 1
207 #else /* _LP64 */
208 #define GPR_ARCH_32 1
209 #endif /* _LP64 */
210 #elif defined(__FreeBSD__)
211 #define GPR_PLATFORM_STRING "freebsd"
212 #ifndef _BSD_SOURCE
213 #define _BSD_SOURCE
214 #endif
215 #define GPR_CPU_POSIX 1
216 #define GPR_GCC_ATOMIC 1
217 #define GPR_GCC_TLS 1
218 #define GPR_POSIX_LOG 1
219 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
220 #define GPR_POSIX_WAKEUP_FD 1
221 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
222 #define GPR_POSIX_SOCKET 1
223 #define GPR_POSIX_SOCKETADDR 1
224 #define GPR_POSIX_SOCKETUTILS 1
225 #define GPR_POSIX_ENV 1
226 #define GPR_POSIX_FILE 1
227 #define GPR_POSIX_STRING 1
228 #define GPR_POSIX_SUBPROCESS 1
229 #define GPR_POSIX_SYNC 1
230 #define GPR_POSIX_TIME 1
231 #define GPR_GETPID_IN_UNISTD_H 1
232 #define GPR_HAVE_SO_NOSIGPIPE 1
233 #ifdef _LP64
234 #define GPR_ARCH_64 1
235 #else /* _LP64 */
236 #define GPR_ARCH_32 1
237 #endif /* _LP64 */
238 #else
239 #error Could not auto-detect platform
240 #endif
241 #endif /* GPR_NO_AUTODETECT_PLATFORM */
242 
243 #ifndef GPR_PLATFORM_STRING
244 #warning "GPR_PLATFORM_STRING not auto-detected"
245 #define GPR_PLATFORM_STRING "unknown"
246 #endif
247 
248 /* For a common case, assume that the platform has a C99-like stdint.h */
249 
250 #include <stdint.h>
251 
252 /* Cache line alignment */
253 #ifndef GPR_CACHELINE_SIZE_LOG
254 #if defined(__i386__) || defined(__x86_64__)
255 #define GPR_CACHELINE_SIZE_LOG 6
256 #endif
257 #ifndef GPR_CACHELINE_SIZE_LOG
258 /* A reasonable default guess. Note that overestimates tend to waste more
259  space, while underestimates tend to waste more time. */
260 #define GPR_CACHELINE_SIZE_LOG 6
261 #endif /* GPR_CACHELINE_SIZE_LOG */
262 #endif /* GPR_CACHELINE_SIZE_LOG */
263 
264 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
265 
266 /* scrub GCC_ATOMIC if it's not available on this compiler */
267 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
268 #undef GPR_GCC_ATOMIC
269 #define GPR_GCC_SYNC 1
270 #endif
271 
272 /* Validate platform combinations */
273 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
274  defined(GPR_WIN32_ATOMIC) != \
275  1
276 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WIN32_ATOMIC
277 #endif
278 
279 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
280 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
281 #endif
282 
283 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WIN32) + \
284  defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
285  1
286 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WIN32, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
287 #endif
288 
289 #if defined(GPR_POSIX_MULTIPOLL_WITH_POLL) && !defined(GPR_POSIX_SOCKET)
290 #error Must define GPR_POSIX_SOCKET to use GPR_POSIX_MULTIPOLL_WITH_POLL
291 #endif
292 
293 #if defined(GPR_POSIX_SOCKET) + defined(GPR_WINSOCK_SOCKET) + \
294  defined(GPR_CUSTOM_SOCKET) != \
295  1
296 #error Must define exactly one of GPR_POSIX_SOCKET, GPR_WINSOCK_SOCKET, GPR_CUSTOM_SOCKET
297 #endif
298 
299 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + \
300  defined(GPR_CUSTOM_TLS) != \
301  1
302 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_CUSTOM_TLS
303 #endif
304 
305 typedef int16_t gpr_int16;
306 typedef int32_t gpr_int32;
307 typedef int64_t gpr_int64;
308 typedef uint8_t gpr_uint8;
309 typedef uint16_t gpr_uint16;
310 typedef uint32_t gpr_uint32;
311 typedef uint64_t gpr_uint64;
312 typedef intmax_t gpr_intmax;
313 typedef intptr_t gpr_intptr;
314 typedef uintmax_t gpr_uintmax;
315 typedef uintptr_t gpr_uintptr;
316 
317 /* INT64_MAX is unavailable on some platforms. */
318 #define GPR_INT64_MAX (gpr_int64)(~(gpr_uint64)0 >> 1)
319 
320 /* maximum alignment needed for any type on this platform, rounded up to a
321  power of two */
322 #define GPR_MAX_ALIGNMENT 16
323 
324 #ifndef GRPC_MUST_USE_RESULT
325 #ifdef __GNUC__
326 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
327 #else
328 #define GRPC_MUST_USE_RESULT
329 #endif
330 #endif
331 
332 #endif /* GRPC_SUPPORT_PORT_PLATFORM_H */
intptr_t gpr_intptr
Definition: port_platform.h:313
uint8_t gpr_uint8
Definition: port_platform.h:308
uintptr_t gpr_uintptr
Definition: port_platform.h:315
uint32_t gpr_uint32
Definition: port_platform.h:310
uintmax_t gpr_uintmax
Definition: port_platform.h:314
int64_t gpr_int64
Definition: port_platform.h:307
uint16_t gpr_uint16
Definition: port_platform.h:309
int32_t gpr_int32
Definition: port_platform.h:306
uint64_t gpr_uint64
Definition: port_platform.h:311
intmax_t gpr_intmax
Definition: port_platform.h:312
int16_t gpr_int16
Definition: port_platform.h:305