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