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