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