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