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