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