GRPC Core  10.0.0
port_platform.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
20 #define GRPC_IMPL_CODEGEN_PORT_PLATFORM_H
21 
22 /*
23  * Define GPR_BACKWARDS_COMPATIBILITY_MODE to try harder to be ABI
24  * compatible with older platforms (currently only on Linux)
25  * Causes:
26  * - some libc calls to be gotten via dlsym
27  * - some syscalls to be made directly
28  */
29 
30 /*
31  * Defines GRPC_USE_ABSL to use Abseil Common Libraries (C++)
32  */
33 #ifndef GRPC_USE_ABSL
34 #define GRPC_USE_ABSL 1
35 #endif
36 
37 /*
38  * Defines GPR_ABSEIL_SYNC to use synchronization features from Abseil
39  */
40 #ifndef GPR_ABSEIL_SYNC
41 /* #define GPR_ABSEIL_SYNC 1 */
42 #endif
43 
44 /* Get windows.h included everywhere (we need it) */
45 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
46 #ifndef WIN32_LEAN_AND_MEAN
47 #define GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
48 #define WIN32_LEAN_AND_MEAN
49 #endif /* WIN32_LEAN_AND_MEAN */
50 
51 #ifndef NOMINMAX
52 #define GRPC_NOMINMX_WAS_NOT_DEFINED
53 #define NOMINMAX
54 #endif /* NOMINMAX */
55 
56 #include <windows.h>
57 
58 #ifndef _WIN32_WINNT
59 #error \
60  "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
61 #else /* !defined(_WIN32_WINNT) */
62 #if (_WIN32_WINNT < 0x0600)
63 #error \
64  "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
65 #endif /* _WIN32_WINNT < 0x0600 */
66 #endif /* defined(_WIN32_WINNT) */
67 
68 #ifdef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
69 #undef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
70 #undef WIN32_LEAN_AND_MEAN
71 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
72 
73 #ifdef GRPC_NOMINMAX_WAS_NOT_DEFINED
74 #undef GRPC_NOMINMAX_WAS_NOT_DEFINED
75 #undef NOMINMAX
76 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
77 #endif /* defined(_WIN64) || defined(WIN64) || defined(_WIN32) || \
78  defined(WIN32) */
79 
80 /* Override this file with one for your platform if you need to redefine
81  things. */
82 
83 #if !defined(GPR_NO_AUTODETECT_PLATFORM)
84 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
85 #if defined(_WIN64) || defined(WIN64)
86 #define GPR_ARCH_64 1
87 #else
88 #define GPR_ARCH_32 1
89 #endif
90 #define GPR_PLATFORM_STRING "windows"
91 #define GPR_WINDOWS 1
92 #define GPR_WINDOWS_SUBPROCESS 1
93 #define GPR_WINDOWS_ENV
94 #ifdef __MSYS__
95 #define GPR_GETPID_IN_UNISTD_H 1
96 #define GPR_MSYS_TMPFILE
97 #define GPR_POSIX_LOG
98 #define GPR_POSIX_STRING
99 #define GPR_POSIX_TIME
100 #else
101 #define GPR_GETPID_IN_PROCESS_H 1
102 #define GPR_WINDOWS_TMPFILE
103 #define GPR_WINDOWS_LOG
104 #define GPR_WINDOWS_CRASH_HANDLER 1
105 #define GPR_WINDOWS_STRING
106 #define GPR_WINDOWS_TIME
107 #endif
108 #ifdef __GNUC__
109 #define GPR_GCC_ATOMIC 1
110 #define GPR_GCC_TLS 1
111 #else
112 #define GPR_WINDOWS_ATOMIC 1
113 #define GPR_MSVC_TLS 1
114 #endif
115 #elif defined(GPR_MANYLINUX1)
116 // TODO(atash): manylinux1 is just another __linux__ but with ancient
117 // libraries; it should be integrated with the `__linux__` definitions below.
118 #define GPR_PLATFORM_STRING "manylinux"
119 #define GPR_POSIX_CRASH_HANDLER 1
120 #define GPR_CPU_POSIX 1
121 #define GPR_GCC_ATOMIC 1
122 #define GPR_GCC_TLS 1
123 #define GPR_LINUX 1
124 #define GPR_LINUX_LOG 1
125 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
126 #define GPR_LINUX_ENV 1
127 #define GPR_POSIX_TMPFILE 1
128 #define GPR_POSIX_STRING 1
129 #define GPR_POSIX_SUBPROCESS 1
130 #define GPR_POSIX_SYNC 1
131 #define GPR_POSIX_TIME 1
132 #define GPR_HAS_PTHREAD_H 1
133 #define GPR_GETPID_IN_UNISTD_H 1
134 #ifdef _LP64
135 #define GPR_ARCH_64 1
136 #else /* _LP64 */
137 #define GPR_ARCH_32 1
138 #endif /* _LP64 */
139 #include <linux/version.h>
140 #elif defined(ANDROID) || defined(__ANDROID__)
141 #define GPR_PLATFORM_STRING "android"
142 #define GPR_ANDROID 1
143 // TODO(apolcyn): re-evaluate support for c-ares
144 // on android after upgrading our c-ares dependency.
145 // See https://github.com/grpc/grpc/issues/18038.
146 #define GRPC_ARES 0
147 #ifdef _LP64
148 #define GPR_ARCH_64 1
149 #else /* _LP64 */
150 #define GPR_ARCH_32 1
151 #endif /* _LP64 */
152 #define GPR_CPU_POSIX 1
153 #define GPR_GCC_SYNC 1
154 #define GPR_GCC_TLS 1
155 #define GPR_POSIX_ENV 1
156 #define GPR_POSIX_TMPFILE 1
157 #define GPR_ANDROID_LOG 1
158 #define GPR_POSIX_STRING 1
159 #define GPR_POSIX_SUBPROCESS 1
160 #define GPR_POSIX_SYNC 1
161 #define GPR_POSIX_TIME 1
162 #define GPR_HAS_PTHREAD_H 1
163 #define GPR_GETPID_IN_UNISTD_H 1
164 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
165 #elif defined(__linux__)
166 #define GPR_PLATFORM_STRING "linux"
167 #ifndef _BSD_SOURCE
168 #define _BSD_SOURCE
169 #endif
170 #ifndef _DEFAULT_SOURCE
171 #define _DEFAULT_SOURCE
172 #endif
173 #ifndef _GNU_SOURCE
174 #define _GNU_SOURCE
175 #endif
176 #include <features.h>
177 #define GPR_CPU_LINUX 1
178 #define GPR_GCC_ATOMIC 1
179 #define GPR_GCC_TLS 1
180 #define GPR_LINUX 1
181 #define GPR_LINUX_LOG
182 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
183 #define GPR_LINUX_ENV 1
184 #define GPR_POSIX_TMPFILE 1
185 #define GPR_POSIX_STRING 1
186 #define GPR_POSIX_SUBPROCESS 1
187 #define GPR_POSIX_SYNC 1
188 #define GPR_POSIX_TIME 1
189 #define GPR_HAS_PTHREAD_H 1
190 #define GPR_GETPID_IN_UNISTD_H 1
191 #ifdef _LP64
192 #define GPR_ARCH_64 1
193 #else /* _LP64 */
194 #define GPR_ARCH_32 1
195 #endif /* _LP64 */
196 #ifdef __GLIBC__
197 #define GPR_POSIX_CRASH_HANDLER 1
198 #define GPR_LINUX_PTHREAD_NAME 1
199 #include <linux/version.h>
200 #else /* musl libc */
201 #define GPR_MUSL_LIBC_COMPAT 1
202 #endif
203 #elif defined(__ASYLO__)
204 #define GPR_ARCH_64 1
205 #define GPR_CPU_POSIX 1
206 #define GPR_GCC_TLS 1
207 #define GPR_PLATFORM_STRING "asylo"
208 #define GPR_GCC_SYNC 1
209 #define GPR_POSIX_SYNC 1
210 #define GPR_POSIX_STRING 1
211 #define GPR_POSIX_LOG 1
212 #define GPR_POSIX_TIME 1
213 #define GPR_POSIX_ENV 1
214 #define GPR_ASYLO 1
215 #define GRPC_POSIX_SOCKET 1
216 #define GRPC_POSIX_SOCKETADDR
217 #define GRPC_POSIX_SOCKETUTILS 1
218 #define GRPC_TIMER_USE_GENERIC 1
219 #define GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 1
220 #define GRPC_POSIX_WAKEUP_FD 1
221 #define GRPC_ARES 0
222 #define GPR_NO_AUTODETECT_PLATFORM 1
223 #elif defined(__APPLE__)
224 #include <Availability.h>
225 #include <TargetConditionals.h>
226 #ifndef _BSD_SOURCE
227 #define _BSD_SOURCE
228 #endif
229 #if TARGET_OS_IPHONE
230 #define GPR_PLATFORM_STRING "ios"
231 #define GPR_CPU_IPHONE 1
232 #define GPR_PTHREAD_TLS 1
233 #define GRPC_CFSTREAM 1
234 /* the c-ares resolver isn't safe to enable on iOS */
235 #define GRPC_ARES 0
236 #else /* TARGET_OS_IPHONE */
237 #define GPR_PLATFORM_STRING "osx"
238 #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
239 #if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7
240 #define GPR_CPU_IPHONE 1
241 #define GPR_PTHREAD_TLS 1
242 #else /* __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_7 */
243 #define GPR_CPU_POSIX 1
244 /* TODO(vjpai): there is a reported issue in bazel build for Mac where __thread
245  in a header is currently not working (bazelbuild/bazel#4341). Remove
246  the following conditional and use GPR_GCC_TLS when that is fixed */
247 #ifndef GRPC_BAZEL_BUILD
248 #define GPR_GCC_TLS 1
249 #else /* GRPC_BAZEL_BUILD */
250 #define GPR_PTHREAD_TLS 1
251 #endif /* GRPC_BAZEL_BUILD */
252 #define GPR_APPLE_PTHREAD_NAME 1
253 #endif
254 #else /* __MAC_OS_X_VERSION_MIN_REQUIRED */
255 #define GPR_CPU_POSIX 1
256 /* TODO(vjpai): Remove the following conditional and use only GPR_GCC_TLS
257  when bazelbuild/bazel#4341 is fixed */
258 #ifndef GRPC_BAZEL_BUILD
259 #define GPR_GCC_TLS 1
260 #else /* GRPC_BAZEL_BUILD */
261 #define GPR_PTHREAD_TLS 1
262 #endif /* GRPC_BAZEL_BUILD */
263 #endif
264 #define GPR_POSIX_CRASH_HANDLER 1
265 #endif
266 #define GPR_APPLE 1
267 #define GPR_GCC_ATOMIC 1
268 #define GPR_POSIX_LOG 1
269 #define GPR_POSIX_ENV 1
270 #define GPR_POSIX_TMPFILE 1
271 #define GPR_POSIX_STRING 1
272 #define GPR_POSIX_SUBPROCESS 1
273 #define GPR_POSIX_SYNC 1
274 #define GPR_POSIX_TIME 1
275 #define GPR_HAS_PTHREAD_H 1
276 #define GPR_GETPID_IN_UNISTD_H 1
277 #ifndef GRPC_CFSTREAM
278 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
279 #endif
280 #ifdef _LP64
281 #define GPR_ARCH_64 1
282 #else /* _LP64 */
283 #define GPR_ARCH_32 1
284 #endif /* _LP64 */
285 #elif defined(__FreeBSD__)
286 #define GPR_PLATFORM_STRING "freebsd"
287 #ifndef _BSD_SOURCE
288 #define _BSD_SOURCE
289 #endif
290 #define GPR_FREEBSD 1
291 #define GPR_CPU_POSIX 1
292 #define GPR_GCC_ATOMIC 1
293 #define GPR_GCC_TLS 1
294 #define GPR_POSIX_LOG 1
295 #define GPR_POSIX_ENV 1
296 #define GPR_POSIX_TMPFILE 1
297 #define GPR_POSIX_STRING 1
298 #define GPR_POSIX_SUBPROCESS 1
299 #define GPR_POSIX_SYNC 1
300 #define GPR_POSIX_TIME 1
301 #define GPR_HAS_PTHREAD_H 1
302 #define GPR_GETPID_IN_UNISTD_H 1
303 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
304 #ifdef _LP64
305 #define GPR_ARCH_64 1
306 #else /* _LP64 */
307 #define GPR_ARCH_32 1
308 #endif /* _LP64 */
309 #elif defined(__OpenBSD__)
310 #define GPR_PLATFORM_STRING "openbsd"
311 #ifndef _BSD_SOURCE
312 #define _BSD_SOURCE
313 #endif
314 #define GPR_OPENBSD 1
315 #define GPR_CPU_POSIX 1
316 #define GPR_GCC_ATOMIC 1
317 #define GPR_GCC_TLS 1
318 #define GPR_POSIX_LOG 1
319 #define GPR_POSIX_ENV 1
320 #define GPR_POSIX_TMPFILE 1
321 #define GPR_POSIX_STRING 1
322 #define GPR_POSIX_SUBPROCESS 1
323 #define GPR_POSIX_SYNC 1
324 #define GPR_POSIX_TIME 1
325 #define GPR_HAS_PTHREAD_H 1
326 #define GPR_GETPID_IN_UNISTD_H 1
327 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
328 #ifdef _LP64
329 #define GPR_ARCH_64 1
330 #else /* _LP64 */
331 #define GPR_ARCH_32 1
332 #endif /* _LP64 */
333 #elif defined(__sun) && defined(__SVR4)
334 #define GPR_PLATFORM_STRING "solaris"
335 #define GPR_SOLARIS 1
336 #define GPR_CPU_POSIX 1
337 #define GPR_GCC_ATOMIC 1
338 #define GPR_GCC_TLS 1
339 #define GPR_POSIX_LOG 1
340 #define GPR_POSIX_ENV 1
341 #define GPR_POSIX_TMPFILE 1
342 #define GPR_POSIX_STRING 1
343 #define GPR_POSIX_SUBPROCESS 1
344 #define GPR_POSIX_SYNC 1
345 #define GPR_POSIX_TIME 1
346 #define GPR_HAS_PTHREAD_H 1
347 #define GPR_GETPID_IN_UNISTD_H 1
348 #ifdef _LP64
349 #define GPR_ARCH_64 1
350 #else /* _LP64 */
351 #define GPR_ARCH_32 1
352 #endif /* _LP64 */
353 #elif defined(_AIX)
354 #define GPR_PLATFORM_STRING "aix"
355 #ifndef _ALL_SOURCE
356 #define _ALL_SOURCE
357 #endif
358 #define GPR_AIX 1
359 #define GPR_CPU_POSIX 1
360 #define GPR_GCC_ATOMIC 1
361 #define GPR_GCC_TLS 1
362 #define GPR_POSIX_LOG 1
363 #define GPR_POSIX_ENV 1
364 #define GPR_POSIX_TMPFILE 1
365 #define GPR_POSIX_STRING 1
366 #define GPR_POSIX_SUBPROCESS 1
367 #define GPR_POSIX_SYNC 1
368 #define GPR_POSIX_TIME 1
369 #define GPR_HAS_PTHREAD_H 1
370 #define GPR_GETPID_IN_UNISTD_H 1
371 #ifdef _LP64
372 #define GPR_ARCH_64 1
373 #else /* _LP64 */
374 #define GPR_ARCH_32 1
375 #endif /* _LP64 */
376 #elif defined(__native_client__)
377 #define GPR_PLATFORM_STRING "nacl"
378 #ifndef _BSD_SOURCE
379 #define _BSD_SOURCE
380 #endif
381 #ifndef _DEFAULT_SOURCE
382 #define _DEFAULT_SOURCE
383 #endif
384 #ifndef _GNU_SOURCE
385 #define _GNU_SOURCE
386 #endif
387 #define GPR_NACL 1
388 #define GPR_CPU_POSIX 1
389 #define GPR_GCC_ATOMIC 1
390 #define GPR_GCC_TLS 1
391 #define GPR_POSIX_LOG 1
392 #define GPR_POSIX_ENV 1
393 #define GPR_POSIX_TMPFILE 1
394 #define GPR_POSIX_STRING 1
395 #define GPR_POSIX_SUBPROCESS 1
396 #define GPR_POSIX_SYNC 1
397 #define GPR_POSIX_TIME 1
398 #define GPR_HAS_PTHREAD_H 1
399 #define GPR_GETPID_IN_UNISTD_H 1
400 #ifdef _LP64
401 #define GPR_ARCH_64 1
402 #else /* _LP64 */
403 #define GPR_ARCH_32 1
404 #endif /* _LP64 */
405 #elif defined(__Fuchsia__)
406 #define GPR_FUCHSIA 1
407 #define GPR_ARCH_64 1
408 #define GPR_PLATFORM_STRING "fuchsia"
409 #include <features.h>
410 // Specifying musl libc affects wrap_memcpy.c. It causes memmove() to be
411 // invoked.
412 #define GPR_MUSL_LIBC_COMPAT 1
413 #define GPR_CPU_POSIX 1
414 #define GPR_GCC_ATOMIC 1
415 #define GPR_PTHREAD_TLS 1
416 #define GPR_POSIX_LOG 1
417 #define GPR_POSIX_SYNC 1
418 #define GPR_POSIX_ENV 1
419 #define GPR_POSIX_TMPFILE 1
420 #define GPR_POSIX_SUBPROCESS 1
421 #define GPR_POSIX_SYNC 1
422 #define GPR_POSIX_STRING 1
423 #define GPR_POSIX_TIME 1
424 #define GPR_HAS_PTHREAD_H 1
425 #define GPR_GETPID_IN_UNISTD_H 1
426 #else
427 #error "Could not auto-detect platform"
428 #endif
429 #endif /* GPR_NO_AUTODETECT_PLATFORM */
430 
431 #if defined(GPR_BACKWARDS_COMPATIBILITY_MODE)
432 /*
433  * For backward compatibility mode, reset _FORTIFY_SOURCE to prevent
434  * a library from having non-standard symbols such as __asprintf_chk.
435  * This helps non-glibc systems such as alpine using musl to find symbols.
436  */
437 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
438 #undef _FORTIFY_SOURCE
439 #define _FORTIFY_SOURCE 0
440 #endif
441 #endif
442 
443 /*
444  * There are platforms for which TLS should not be used even though the
445  * compiler makes it seem like it's supported (Android NDK < r12b for example).
446  * This is primarily because of linker problems and toolchain misconfiguration:
447  * TLS isn't supported until NDK r12b per
448  * https://developer.android.com/ndk/downloads/revision_history.html
449  * TLS also does not work with Android NDK if GCC is being used as the compiler
450  * instead of Clang.
451  * Since NDK r16, `__NDK_MAJOR__` and `__NDK_MINOR__` are defined in
452  * <android/ndk-version.h>. For NDK < r16, users should define these macros,
453  * e.g. `-D__NDK_MAJOR__=11 -D__NKD_MINOR__=0` for NDK r11. */
454 #if defined(__ANDROID__) && defined(GPR_GCC_TLS)
455 #if __has_include(<android/ndk-version.h>)
456 #include <android/ndk-version.h>
457 #endif /* __has_include(<android/ndk-version.h>) */
458 #if (defined(__clang__) && defined(__NDK_MAJOR__) && defined(__NDK_MINOR__) && \
459  ((__NDK_MAJOR__ < 12) || \
460  ((__NDK_MAJOR__ == 12) && (__NDK_MINOR__ < 1)))) || \
461  (defined(__GNUC__) && !defined(__clang__))
462 #undef GPR_GCC_TLS
463 #define GPR_PTHREAD_TLS 1
464 #endif
465 #endif /*defined(__ANDROID__) && defined(GPR_GCC_TLS) */
466 
467 #if defined(__has_include)
468 #if __has_include(<atomic>)
469 #define GRPC_HAS_CXX11_ATOMIC
470 #endif /* __has_include(<atomic>) */
471 #endif /* defined(__has_include) */
472 
473 #ifndef GPR_PLATFORM_STRING
474 #warning "GPR_PLATFORM_STRING not auto-detected"
475 #define GPR_PLATFORM_STRING "unknown"
476 #endif
477 
478 #ifdef GPR_GCOV
479 #undef GPR_FORBID_UNREACHABLE_CODE
480 #define GPR_FORBID_UNREACHABLE_CODE 1
481 #endif
482 
483 #ifdef _MSC_VER
484 #if _MSC_VER < 1700
485 typedef __int8 int8_t;
486 typedef __int16 int16_t;
487 typedef __int32 int32_t;
488 typedef __int64 int64_t;
489 typedef unsigned __int8 uint8_t;
490 typedef unsigned __int16 uint16_t;
491 typedef unsigned __int32 uint32_t;
492 typedef unsigned __int64 uint64_t;
493 #else
494 #include <stdint.h>
495 #endif /* _MSC_VER < 1700 */
496 #else
497 #include <stdint.h>
498 #endif /* _MSC_VER */
499 
500 /* Type of cycle clock implementation */
501 #ifdef GPR_LINUX
502 /* Disable cycle clock by default.
503  TODO(soheil): enable when we support fallback for unstable cycle clocks.
504 #if defined(__i386__)
505 #define GPR_CYCLE_COUNTER_RDTSC_32 1
506 #elif defined(__x86_64__) || defined(__amd64__)
507 #define GPR_CYCLE_COUNTER_RDTSC_64 1
508 #else
509 #define GPR_CYCLE_COUNTER_FALLBACK 1
510 #endif
511 */
512 #define GPR_CYCLE_COUNTER_FALLBACK 1
513 #else
514 #define GPR_CYCLE_COUNTER_FALLBACK 1
515 #endif /* GPR_LINUX */
516 
517 /* Cache line alignment */
518 #ifndef GPR_CACHELINE_SIZE_LOG
519 #if defined(__i386__) || defined(__x86_64__)
520 #define GPR_CACHELINE_SIZE_LOG 6
521 #endif
522 #ifndef GPR_CACHELINE_SIZE_LOG
523 /* A reasonable default guess. Note that overestimates tend to waste more
524  space, while underestimates tend to waste more time. */
525 #define GPR_CACHELINE_SIZE_LOG 6
526 #endif /* GPR_CACHELINE_SIZE_LOG */
527 #endif /* GPR_CACHELINE_SIZE_LOG */
528 
529 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
530 
531 /* scrub GCC_ATOMIC if it's not available on this compiler */
532 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
533 #undef GPR_GCC_ATOMIC
534 #define GPR_GCC_SYNC 1
535 #endif
536 
537 /* Validate platform combinations */
538 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
539  defined(GPR_WINDOWS_ATOMIC) != \
540  1
541 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WINDOWS_ATOMIC
542 #endif
543 
544 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
545 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
546 #endif
547 
548 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WINDOWS) + \
549  defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
550  1
551 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WINDOWS, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
552 #endif
553 
554 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + \
555  defined(GPR_CUSTOM_TLS) != \
556  1
557 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_CUSTOM_TLS
558 #endif
559 
560 /* maximum alignment needed for any type on this platform, rounded up to a
561  power of two */
562 #define GPR_MAX_ALIGNMENT 16
563 
564 #ifndef GRPC_ARES
565 #define GRPC_ARES 1
566 #endif
567 
568 #ifndef GRPC_IF_NAMETOINDEX
569 #define GRPC_IF_NAMETOINDEX 1
570 #endif
571 
572 #ifndef GRPC_MUST_USE_RESULT
573 #if defined(__GNUC__) && !defined(__MINGW32__)
574 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
575 #define GPR_ALIGN_STRUCT(n) __attribute__((aligned(n)))
576 #else
577 #define GRPC_MUST_USE_RESULT
578 #define GPR_ALIGN_STRUCT(n)
579 #endif
580 #endif
581 
582 #ifndef GRPC_UNUSED
583 #if defined(__GNUC__) && !defined(__MINGW32__)
584 #define GRPC_UNUSED __attribute__((unused))
585 #else
586 #define GRPC_UNUSED
587 #endif
588 #endif
589 
590 #ifndef GPR_PRINT_FORMAT_CHECK
591 #ifdef __GNUC__
592 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS) \
593  __attribute__((format(printf, FORMAT_STR, ARGS)))
594 #else
595 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS)
596 #endif
597 #endif /* GPR_PRINT_FORMAT_CHECK */
598 
599 #if GPR_FORBID_UNREACHABLE_CODE
600 #define GPR_UNREACHABLE_CODE(STATEMENT)
601 #else
602 #define GPR_UNREACHABLE_CODE(STATEMENT) \
603  do { \
604  gpr_log(GPR_ERROR, "Should never reach here."); \
605  abort(); \
606  STATEMENT; \
607  } while (0)
608 #endif /* GPR_FORBID_UNREACHABLE_CODE */
609 
610 #ifndef GPRAPI
611 #define GPRAPI
612 #endif
613 
614 #ifndef GRPCAPI
615 #define GRPCAPI GPRAPI
616 #endif
617 
618 #ifndef CENSUSAPI
619 #define CENSUSAPI GRPCAPI
620 #endif
621 
622 #ifndef GPR_HAS_ATTRIBUTE
623 #ifdef __has_attribute
624 #define GPR_HAS_ATTRIBUTE(a) __has_attribute(a)
625 #else
626 #define GPR_HAS_ATTRIBUTE(a) 0
627 #endif
628 #endif /* GPR_HAS_ATTRIBUTE */
629 
630 #ifndef GPR_HAS_FEATURE
631 #ifdef __has_feature
632 #define GPR_HAS_FEATURE(a) __has_feature(a)
633 #else
634 #define GPR_HAS_FEATURE(a) 0
635 #endif
636 #endif /* GPR_HAS_FEATURE */
637 
638 #ifndef GPR_ATTRIBUTE_NOINLINE
639 #if GPR_HAS_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
640 #define GPR_ATTRIBUTE_NOINLINE __attribute__((noinline))
641 #define GPR_HAS_ATTRIBUTE_NOINLINE 1
642 #else
643 #define GPR_ATTRIBUTE_NOINLINE
644 #endif
645 #endif /* GPR_ATTRIBUTE_NOINLINE */
646 
647 #ifndef GPR_ATTRIBUTE_WEAK
648 /* Attribute weak is broken on LLVM/windows:
649  * https://bugs.llvm.org/show_bug.cgi?id=37598 */
650 #if (GPR_HAS_ATTRIBUTE(weak) || (defined(__GNUC__) && !defined(__clang__))) && \
651  !(defined(__llvm__) && defined(_WIN32))
652 #define GPR_ATTRIBUTE_WEAK __attribute__((weak))
653 #define GPR_HAS_ATTRIBUTE_WEAK 1
654 #else
655 #define GPR_ATTRIBUTE_WEAK
656 #endif
657 #endif /* GPR_ATTRIBUTE_WEAK */
658 
659 #ifndef GPR_ATTRIBUTE_NO_TSAN /* (1) */
660 #if GPR_HAS_FEATURE(thread_sanitizer)
661 #define GPR_ATTRIBUTE_NO_TSAN __attribute__((no_sanitize("thread")))
662 #endif /* GPR_HAS_FEATURE */
663 #ifndef GPR_ATTRIBUTE_NO_TSAN /* (2) */
664 #define GPR_ATTRIBUTE_NO_TSAN
665 #endif /* GPR_ATTRIBUTE_NO_TSAN (2) */
666 #endif /* GPR_ATTRIBUTE_NO_TSAN (1) */
667 
668 /* GRPC_TSAN_ENABLED will be defined, when compiled with thread sanitizer. */
669 #ifndef GRPC_TSAN_SUPPRESSED
670 #if defined(__SANITIZE_THREAD__)
671 #define GRPC_TSAN_ENABLED
672 #elif GPR_HAS_FEATURE(thread_sanitizer)
673 #define GRPC_TSAN_ENABLED
674 #endif
675 #endif
676 
677 /* GRPC_ASAN_ENABLED will be defined, when compiled with address sanitizer. */
678 #ifndef GRPC_ASAN_SUPPRESSED
679 #if defined(__SANITIZE_ADDRESS__)
680 #define GRPC_ASAN_ENABLED
681 #elif GPR_HAS_FEATURE(address_sanitizer)
682 #define GRPC_ASAN_ENABLED
683 #endif
684 #endif
685 
686 /* GRPC_ALLOW_EXCEPTIONS should be 0 or 1 if exceptions are allowed or not */
687 #ifndef GRPC_ALLOW_EXCEPTIONS
688 #ifdef GPR_WINDOWS
689 #if defined(_MSC_VER) && defined(_CPPUNWIND)
690 #define GRPC_ALLOW_EXCEPTIONS 1
691 #elif defined(__EXCEPTIONS)
692 #define GRPC_ALLOW_EXCEPTIONS 1
693 #else
694 #define GRPC_ALLOW_EXCEPTIONS 0
695 #endif
696 #else /* GPR_WINDOWS */
697 #ifdef __EXCEPTIONS
698 #define GRPC_ALLOW_EXCEPTIONS 1
699 #else /* __EXCEPTIONS */
700 #define GRPC_ALLOW_EXCEPTIONS 0
701 #endif /* __EXCEPTIONS */
702 #endif /* __GPR_WINDOWS */
703 #endif /* GRPC_ALLOW_EXCEPTIONS */
704 
705 /* Use GPR_LIKELY only in cases where you are sure that a certain outcome is the
706  * most likely. Ideally, also collect performance numbers to justify the claim.
707  */
708 #ifdef __GNUC__
709 #define GPR_LIKELY(x) __builtin_expect((x), 1)
710 #define GPR_UNLIKELY(x) __builtin_expect((x), 0)
711 #else /* __GNUC__ */
712 #define GPR_LIKELY(x) (x)
713 #define GPR_UNLIKELY(x) (x)
714 #endif /* __GNUC__ */
715 
716 #ifndef __STDC_FORMAT_MACROS
717 #define __STDC_FORMAT_MACROS
718 #endif
719 
720 #endif /* GRPC_IMPL_CODEGEN_PORT_PLATFORM_H */