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