GRPC C++  1.73.0
port_platform.h
Go to the documentation of this file.
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_SUPPORT_PORT_PLATFORM_H
20 #define GRPC_SUPPORT_PORT_PLATFORM_H
21 
22 // [[deprecated]] attribute is only available since C++14
23 #if __cplusplus >= 201402L
24 #define GRPC_DEPRECATED(reason) [[deprecated(reason)]]
25 #else
26 #define GRPC_DEPRECATED(reason)
27 #endif // __cplusplus >= 201402L
28 
29 #ifndef GPR_DISABLE_ABSEIL_SYNC
30 /*
31  * Defines GPR_ABSEIL_SYNC to use synchronization features from Abseil
32  *
33  * You can opt for gRPC's native synchronization by enabling
34  * GPR_DISABLE_ABSEIL_SYNC. However, this flag is temporary and will be
35  * removed once the Abseil synchronization is stabilized.
36  * If you encounter any issues with this feature, please report them
37  * by filing a bug at https://github.com/grpc/grpc.
38  */
39 #ifndef GPR_ABSEIL_SYNC
40 #define GPR_ABSEIL_SYNC 1
41 #endif // GPR_ABSEIL_SYNC
42 #endif // GPR_DISABLE_ABSEIL_SYNC
43 
44 /* Get windows.h included everywhere (we need it) */
45 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
46 #ifndef WIN32_LEAN_AND_MEAN
47 #define GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
48 #define WIN32_LEAN_AND_MEAN
49 #endif /* WIN32_LEAN_AND_MEAN */
50 
51 // GPRC_DLL
52 // inspired by
53 // https://github.com/abseil/abseil-cpp/blob/20220623.1/absl/base/config.h#L730-L747
54 //
55 // When building gRPC as a DLL, this macro expands to `__declspec(dllexport)`
56 // so we can annotate symbols appropriately as being exported. When used in
57 // headers consuming a DLL, this macro expands to `__declspec(dllimport)` so
58 // that consumers know the symbol is defined inside the DLL. In all other cases,
59 // the macro expands to nothing.
60 //
61 // Warning: shared library support for Windows (i.e. producing DLL plus import
62 // library instead of a static library) is experimental. Some symbols that can
63 // be linked using the static library may not be available when using the
64 // dynamically linked library.
65 //
66 // Note: GRPC_DLL_EXPORTS is set in CMakeLists.txt when building shared
67 // grpc{,_unsecure}
68 // GRPC_DLL_IMPORTS is set by us as part of the interface for consumers of
69 // the DLL
70 #if !defined(GRPC_DLL)
71 #if defined(GRPC_DLL_EXPORTS)
72 #define GRPC_DLL __declspec(dllexport)
73 #elif defined(GRPC_DLL_IMPORTS)
74 #define GRPC_DLL __declspec(dllimport)
75 #else
76 #define GRPC_DLL
77 #endif // defined(GRPC_DLL_EXPORTS)
78 #endif
79 
80 // same for gRPC++
81 #if !defined(GRPCXX_DLL)
82 #if defined(GRPCXX_DLL_EXPORTS)
83 #define GRPCXX_DLL __declspec(dllexport)
84 #elif defined(GRPCXX_DLL_IMPORTS)
85 #define GRPCXX_DLL __declspec(dllimport)
86 #else
87 #define GRPCXX_DLL
88 #endif // defined(GRPCXX_DLL_EXPORTS)
89 #endif
90 
91 // same for GPR
92 #if !defined(GPR_DLL)
93 #if defined(GPR_DLL_EXPORTS)
94 #define GPR_DLL __declspec(dllexport)
95 #elif defined(GPR_DLL_IMPORTS)
96 #define GPR_DLL __declspec(dllimport)
97 #else
98 #define GPR_DLL
99 #endif // defined(GPR_DLL_EXPORTS)
100 #endif
101 
102 #ifndef NOMINMAX
103 #define GRPC_NOMINMX_WAS_NOT_DEFINED
104 #define NOMINMAX
105 #endif /* NOMINMAX */
106 
107 #include <windows.h>
108 
109 #ifndef _WIN32_WINNT
110 #error \
111  "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
112 #else /* !defined(_WIN32_WINNT) */
113 #if (_WIN32_WINNT < 0x0600)
114 #error \
115  "Please compile grpc with _WIN32_WINNT of at least 0x600 (aka Windows Vista)"
116 #endif /* _WIN32_WINNT < 0x0600 */
117 
118 #ifdef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
119 #undef GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED
120 #undef WIN32_LEAN_AND_MEAN
121 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
122 
123 #ifdef GRPC_NOMINMAX_WAS_NOT_DEFINED
124 #undef GRPC_NOMINMAX_WAS_NOT_DEFINED
125 #undef NOMINMAX
126 #endif /* GRPC_WIN32_LEAN_AND_MEAN_WAS_NOT_DEFINED */
127 #endif /* defined(_WIN64) || defined(WIN64) || defined(_WIN32) || \
128  defined(WIN32) */
129 #else
130 #define GRPC_DLL
131 #define GRPCXX_DLL
132 #define GPR_DLL
133 #endif /* defined(_WIN32_WINNT) */
134 
135 /* Override this file with one for your platform if you need to redefine
136  things. */
137 
138 #if !defined(GPR_NO_AUTODETECT_PLATFORM)
139 #if defined(_WIN64) || defined(WIN64) || defined(_WIN32) || defined(WIN32)
140 #if defined(_WIN64) || defined(WIN64)
141 #define GPR_ARCH_64 1
142 #else
143 #define GPR_ARCH_32 1
144 #endif
145 #define GPR_PLATFORM_STRING "windows"
146 #define GPR_WINDOWS 1
147 #define GPR_WINDOWS_SUBPROCESS 1
148 #define GPR_WINDOWS_ENV
149 #ifdef __MSYS__
150 #define GPR_GETPID_IN_UNISTD_H 1
151 #define GPR_MSYS_TMPFILE
152 #define GPR_POSIX_LOG
153 #define GPR_POSIX_STRING
154 #define GPR_POSIX_TIME
155 #else
156 #define GPR_GETPID_IN_PROCESS_H 1
157 #define GPR_WINDOWS_TMPFILE
158 #define GPR_WINDOWS_LOG
159 #define GPR_WINDOWS_CRASH_HANDLER 1
160 #define GPR_WINDOWS_STAT
161 #define GPR_WINDOWS_STRING
162 #define GPR_WINDOWS_TIME
163 #endif
164 #ifdef __GNUC__
165 #define GPR_GCC_ATOMIC 1
166 #else
167 #define GPR_WINDOWS_ATOMIC 1
168 #endif
169 #elif defined(ANDROID) || defined(__ANDROID__)
170 #define GPR_PLATFORM_STRING "android"
171 #define GPR_ANDROID 1
172 #ifndef __ANDROID_API__
173 #error "__ANDROID_API__ must be defined for Android builds."
174 #endif
175 #if __ANDROID_API__ < 21
176 #error "Requires Android API v21 and above"
177 #endif
178 // TODO(apolcyn): re-evaluate support for c-ares
179 // on android after upgrading our c-ares dependency.
180 // See https://github.com/grpc/grpc/issues/18038.
181 #define GRPC_ARES 0
182 #ifdef _LP64
183 #define GPR_ARCH_64 1
184 #else /* _LP64 */
185 #define GPR_ARCH_32 1
186 #endif /* _LP64 */
187 #define GPR_CPU_POSIX 1
188 #define GPR_GCC_SYNC 1
189 #define GPR_POSIX_ENV 1
190 #define GPR_POSIX_TMPFILE 1
191 #define GPR_POSIX_STAT 1
192 #define GPR_ANDROID_LOG 1
193 #define GPR_POSIX_STRING 1
194 #define GPR_POSIX_SUBPROCESS 1
195 #define GPR_POSIX_SYNC 1
196 #define GPR_POSIX_TIME 1
197 #define GPR_HAS_PTHREAD_H 1
198 #define GPR_GETPID_IN_UNISTD_H 1
199 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
200 #if defined(__has_include)
201 #if __has_include(<android/ndk-version.h>)
202 #include <android/ndk-version.h>
203 #endif /* __has_include(<android/ndk-version.h>) */
204 #endif /* defined(__has_include) */
205 #include <linux/version.h>
206 #elif defined(__linux__)
207 #define GPR_PLATFORM_STRING "linux"
208 #ifndef _BSD_SOURCE
209 #define _BSD_SOURCE
210 #endif
211 #ifndef _DEFAULT_SOURCE
212 #define _DEFAULT_SOURCE
213 #endif
214 #ifndef _GNU_SOURCE
215 #define _GNU_SOURCE
216 #endif
217 #include <features.h>
218 #define GPR_CPU_LINUX 1
219 #define GPR_GCC_ATOMIC 1
220 #define GPR_LINUX 1
221 #define GPR_LINUX_LOG
222 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
223 #define GPR_LINUX_ENV 1
224 #define GPR_POSIX_TMPFILE 1
225 #define GPR_POSIX_STAT 1
226 #define GPR_POSIX_STRING 1
227 #define GPR_POSIX_SUBPROCESS 1
228 #define GPR_POSIX_SYNC 1
229 #define GPR_POSIX_TIME 1
230 #define GPR_HAS_PTHREAD_H 1
231 #define GPR_GETPID_IN_UNISTD_H 1
232 #ifdef _LP64
233 #define GPR_ARCH_64 1
234 #else /* _LP64 */
235 #define GPR_ARCH_32 1
236 #endif /* _LP64 */
237 #ifdef __GLIBC__
238 #define GPR_POSIX_CRASH_HANDLER 1
239 #ifdef __GLIBC_PREREQ
240 #if __GLIBC_PREREQ(2, 12)
241 #define GPR_LINUX_PTHREAD_NAME 1
242 #endif
243 #else
244 // musl libc & others
245 #define GPR_LINUX_PTHREAD_NAME 1
246 #endif
247 #include <linux/version.h>
248 #else /* musl libc */
249 #define GPR_MUSL_LIBC_COMPAT 1
250 #endif
251 #elif defined(__ASYLO__)
252 #define GPR_ARCH_64 1
253 #define GPR_CPU_POSIX 1
254 #define GPR_PLATFORM_STRING "asylo"
255 #define GPR_GCC_SYNC 1
256 #define GPR_POSIX_STAT 1
257 #define GPR_POSIX_SYNC 1
258 #define GPR_POSIX_STRING 1
259 #define GPR_POSIX_LOG 1
260 #define GPR_POSIX_TIME 1
261 #define GPR_POSIX_ENV 1
262 #define GPR_ASYLO 1
263 #define GRPC_POSIX_SOCKET 1
264 #define GRPC_POSIX_SOCKETADDR
265 #define GRPC_POSIX_SOCKETUTILS 1
266 #define GRPC_TIMER_USE_GENERIC 1
267 #define GRPC_POSIX_NO_SPECIAL_WAKEUP_FD 1
268 #define GRPC_POSIX_WAKEUP_FD 1
269 #define GRPC_HAVE_MSG_NOSIGNAL 1
270 #define GRPC_HAVE_UNIX_SOCKET 1
271 #define GRPC_ARES 0
272 #define GPR_NO_AUTODETECT_PLATFORM 1
273 #elif defined(__APPLE__)
274 #include <Availability.h>
275 #include <TargetConditionals.h>
276 #ifndef _BSD_SOURCE
277 #define _BSD_SOURCE
278 #endif
279 #if TARGET_OS_IPHONE
280 #define GPR_PLATFORM_STRING "ios"
281 #define GPR_CPU_IPHONE 1
282 #define GRPC_CFSTREAM 1
283 #ifndef GRPC_IOS_EVENT_ENGINE_CLIENT
284 #define GRPC_IOS_EVENT_ENGINE_CLIENT 1
285 #endif /* GRPC_IOS_EVENT_ENGINE_CLIENT */
286 /* the c-ares resolver isn't safe to enable on iOS */
287 #define GRPC_ARES 0
288 #else /* TARGET_OS_IPHONE */
289 #define GPR_PLATFORM_STRING "osx"
290 #define GPR_CPU_POSIX 1
291 #define GPR_POSIX_CRASH_HANDLER 1
292 #endif
293 #define GPR_APPLE 1
294 #define GPR_GCC_ATOMIC 1
295 #define GPR_POSIX_LOG 1
296 #define GPR_POSIX_ENV 1
297 #define GPR_POSIX_TMPFILE 1
298 #define GPR_POSIX_STAT 1
299 #define GPR_POSIX_STRING 1
300 #define GPR_POSIX_SUBPROCESS 1
301 #define GPR_POSIX_SYNC 1
302 #define GPR_POSIX_TIME 1
303 #define GPR_HAS_PTHREAD_H 1
304 #define GPR_GETPID_IN_UNISTD_H 1
305 #ifndef GRPC_CFSTREAM
306 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
307 #endif
308 #ifdef _LP64
309 #define GPR_ARCH_64 1
310 #else /* _LP64 */
311 #define GPR_ARCH_32 1
312 #endif /* _LP64 */
313 #elif defined(__FreeBSD__)
314 #define GPR_PLATFORM_STRING "freebsd"
315 #ifndef _BSD_SOURCE
316 #define _BSD_SOURCE
317 #endif
318 #define GPR_FREEBSD 1
319 #define GPR_CPU_POSIX 1
320 #define GPR_GCC_ATOMIC 1
321 #define GPR_POSIX_LOG 1
322 #define GPR_POSIX_ENV 1
323 #define GPR_POSIX_TMPFILE 1
324 #define GPR_POSIX_STAT 1
325 #define GPR_POSIX_STRING 1
326 #define GPR_POSIX_SUBPROCESS 1
327 #define GPR_POSIX_SYNC 1
328 #define GPR_POSIX_TIME 1
329 #define GPR_HAS_PTHREAD_H 1
330 #define GPR_GETPID_IN_UNISTD_H 1
331 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
332 #ifdef _LP64
333 #define GPR_ARCH_64 1
334 #else /* _LP64 */
335 #define GPR_ARCH_32 1
336 #endif /* _LP64 */
337 #elif defined(__OpenBSD__)
338 #define GPR_PLATFORM_STRING "openbsd"
339 #ifndef _BSD_SOURCE
340 #define _BSD_SOURCE
341 #endif
342 #define GPR_OPENBSD 1
343 #define GPR_CPU_POSIX 1
344 #define GPR_GCC_ATOMIC 1
345 #define GPR_POSIX_LOG 1
346 #define GPR_POSIX_ENV 1
347 #define GPR_POSIX_TMPFILE 1
348 #define GPR_POSIX_STAT 1
349 #define GPR_POSIX_STRING 1
350 #define GPR_POSIX_SUBPROCESS 1
351 #define GPR_POSIX_SYNC 1
352 #define GPR_POSIX_TIME 1
353 #define GPR_HAS_PTHREAD_H 1
354 #define GPR_GETPID_IN_UNISTD_H 1
355 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
356 #ifdef _LP64
357 #define GPR_ARCH_64 1
358 #else /* _LP64 */
359 #define GPR_ARCH_32 1
360 #endif /* _LP64 */
361 #elif defined(__sun) && defined(__SVR4)
362 #define GPR_PLATFORM_STRING "solaris"
363 #define GPR_SOLARIS 1
364 #define GPR_CPU_POSIX 1
365 #define GPR_GCC_ATOMIC 1
366 #define GPR_POSIX_LOG 1
367 #define GPR_POSIX_ENV 1
368 #define GPR_POSIX_TMPFILE 1
369 #define GPR_POSIX_STAT 1
370 #define GPR_POSIX_STRING 1
371 #define GPR_POSIX_SUBPROCESS 1
372 #define GPR_POSIX_SYNC 1
373 #define GPR_POSIX_TIME 1
374 #define GPR_HAS_PTHREAD_H 1
375 #define GPR_GETPID_IN_UNISTD_H 1
376 #ifdef _LP64
377 #define GPR_ARCH_64 1
378 #else /* _LP64 */
379 #define GPR_ARCH_32 1
380 #endif /* _LP64 */
381 #elif defined(_AIX)
382 #define GPR_PLATFORM_STRING "aix"
383 #ifndef _ALL_SOURCE
384 #define _ALL_SOURCE
385 #endif
386 #define GPR_AIX 1
387 #define GPR_CPU_POSIX 1
388 #define GPR_GCC_ATOMIC 1
389 #define GPR_POSIX_LOG 1
390 #define GPR_POSIX_ENV 1
391 #define GPR_POSIX_TMPFILE 1
392 #define GPR_POSIX_STAT 1
393 #define GPR_POSIX_STRING 1
394 #define GPR_POSIX_SUBPROCESS 1
395 #define GPR_POSIX_SYNC 1
396 #define GPR_POSIX_TIME 1
397 #define GPR_HAS_PTHREAD_H 1
398 #define GPR_GETPID_IN_UNISTD_H 1
399 #ifdef _LP64
400 #define GPR_ARCH_64 1
401 #else /* _LP64 */
402 #define GPR_ARCH_32 1
403 #endif /* _LP64 */
404 #elif defined(__NetBSD__)
405 // NetBSD is a community-supported platform.
406 // Please contact Thomas Klausner <wiz@NetBSD.org> for support.
407 #define GPR_PLATFORM_STRING "netbsd"
408 #ifndef _BSD_SOURCE
409 #define _BSD_SOURCE
410 #endif
411 #define GPR_NETBSD 1
412 #define GPR_CPU_POSIX 1
413 #define GPR_GCC_ATOMIC 1
414 #define GPR_GCC_TLS 1
415 #define GPR_POSIX_LOG 1
416 #define GPR_POSIX_ENV 1
417 #define GPR_POSIX_TMPFILE 1
418 #define GPR_POSIX_STAT 1
419 #define GPR_POSIX_STRING 1
420 #define GPR_POSIX_SUBPROCESS 1
421 #define GPR_POSIX_SYNC 1
422 #define GPR_POSIX_TIME 1
423 #define GPR_GETPID_IN_UNISTD_H 1
424 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
425 #ifdef _LP64
426 #define GPR_ARCH_64 1
427 #else /* _LP64 */
428 #define GPR_ARCH_32 1
429 #endif /* _LP64 */
430 #elif defined(__native_client__)
431 #define GPR_PLATFORM_STRING "nacl"
432 #ifndef _BSD_SOURCE
433 #define _BSD_SOURCE
434 #endif
435 #ifndef _DEFAULT_SOURCE
436 #define _DEFAULT_SOURCE
437 #endif
438 #ifndef _GNU_SOURCE
439 #define _GNU_SOURCE
440 #endif
441 #define GPR_NACL 1
442 #define GPR_CPU_POSIX 1
443 #define GPR_GCC_ATOMIC 1
444 #define GPR_POSIX_LOG 1
445 #define GPR_POSIX_ENV 1
446 #define GPR_POSIX_TMPFILE 1
447 #define GPR_POSIX_STAT 1
448 #define GPR_POSIX_STRING 1
449 #define GPR_POSIX_SUBPROCESS 1
450 #define GPR_POSIX_SYNC 1
451 #define GPR_POSIX_TIME 1
452 #define GPR_HAS_PTHREAD_H 1
453 #define GPR_GETPID_IN_UNISTD_H 1
454 #ifdef _LP64
455 #define GPR_ARCH_64 1
456 #else /* _LP64 */
457 #define GPR_ARCH_32 1
458 #endif /* _LP64 */
459 #elif defined(__Fuchsia__)
460 #define GRPC_ARES 0
461 #define GPR_FUCHSIA 1
462 #define GPR_ARCH_64 1
463 #define GPR_PLATFORM_STRING "fuchsia"
464 #include <features.h>
465 // Specifying musl libc affects wrap_memcpy.c. It causes memmove() to be
466 // invoked.
467 #define GPR_MUSL_LIBC_COMPAT 1
468 #define GPR_CPU_POSIX 1
469 #define GPR_GCC_ATOMIC 1
470 #define GPR_POSIX_LOG 1
471 #define GPR_POSIX_SYNC 1
472 #define GPR_POSIX_ENV 1
473 #define GPR_POSIX_TMPFILE 1
474 #define GPR_POSIX_STAT 1
475 #define GPR_POSIX_SUBPROCESS 1
476 #define GPR_POSIX_SYNC 1
477 #define GPR_POSIX_STRING 1
478 #define GPR_POSIX_TIME 1
479 #define GPR_HAS_PTHREAD_H 1
480 #define GPR_GETPID_IN_UNISTD_H 1
481 #define GRPC_ROOT_PEM_PATH "/config/ssl/cert.pem"
482 #elif defined(__HAIKU__)
483 #define GPR_PLATFORM_STRING "haiku"
484 // Haiku is a community-supported platform.
485 // Please contact Jerome Duval <jerome.duval@gmail.com> for support.
486 #ifndef _BSD_SOURCE
487 #define _BSD_SOURCE
488 #endif
489 #define GPR_HAIKU 1
490 #define GPR_CPU_POSIX 1
491 #define GPR_GCC_ATOMIC 1
492 #define GPR_POSIX_LOG 1
493 #define GPR_POSIX_ENV 1
494 #define GPR_POSIX_TMPFILE 1
495 #define GPR_POSIX_STAT 1
496 #define GPR_POSIX_STRING 1
497 #define GPR_POSIX_SUBPROCESS 1
498 #define GPR_POSIX_SYNC 1
499 #define GPR_POSIX_TIME 1
500 #define GPR_HAS_PTHREAD_H 1
501 #define GPR_GETPID_IN_UNISTD_H 1
502 #define GPR_SUPPORT_CHANNELS_FROM_FD 1
503 #ifdef _LP64
504 #define GPR_ARCH_64 1
505 #else /* _LP64 */
506 #define GPR_ARCH_32 1
507 #endif /* _LP64 */
508 #elif defined(__QNX__) || defined(__QNXNTO__)
509 #define GPR_PLATFORM_STRING "qnx"
510 #define GPR_CPU_POSIX 1
511 #define GPR_GCC_ATOMIC 1
512 #define GPR_POSIX_LOG 1
513 #define GPR_POSIX_ENV 1
514 #define GPR_POSIX_TMPFILE 1
515 #define GPR_POSIX_STAT 1
516 #define GPR_POSIX_STRING 1
517 #define GPR_POSIX_SYNC 1
518 #define GPR_POSIX_TIME 1
519 #define GPR_HAS_PTHREAD_H 1
520 #define GPR_GETPID_IN_UNISTD_H 1
521 #ifdef _LP64
522 #define GPR_ARCH_64 1
523 #else /* _LP64 */
524 #define GPR_ARCH_32 1
525 #endif /* _LP64 */
526 #else
527 #error "Could not auto-detect platform"
528 #endif
529 #endif /* GPR_NO_AUTODETECT_PLATFORM */
530 
531 #if defined(__has_include)
532 #if __has_include(<atomic>)
533 #define GRPC_HAS_CXX11_ATOMIC
534 #endif /* __has_include(<atomic>) */
535 #endif /* defined(__has_include) */
536 
537 #ifndef GPR_PLATFORM_STRING
538 #warning "GPR_PLATFORM_STRING not auto-detected"
539 #define GPR_PLATFORM_STRING "unknown"
540 #endif
541 
542 #ifdef GPR_GCOV
543 #undef GPR_FORBID_UNREACHABLE_CODE
544 #define GPR_FORBID_UNREACHABLE_CODE 1
545 #endif
546 
547 #ifdef _MSC_VER
548 #if _MSC_VER < 1700
549 typedef __int8 int8_t;
550 typedef __int16 int16_t;
551 typedef __int32 int32_t;
552 typedef __int64 int64_t;
553 typedef unsigned __int8 uint8_t;
554 typedef unsigned __int16 uint16_t;
555 typedef unsigned __int32 uint32_t;
556 typedef unsigned __int64 uint64_t;
557 #else
558 #include <stdint.h>
559 #endif /* _MSC_VER < 1700 */
560 #else
561 #include <stdint.h>
562 #endif /* _MSC_VER */
563 
564 /* Type of cycle clock implementation */
565 #ifdef GPR_LINUX
566 /* Disable cycle clock by default.
567  TODO(soheil): enable when we support fallback for unstable cycle clocks.
568 #if defined(__i386__)
569 #define GPR_CYCLE_COUNTER_RDTSC_32 1
570 #elif defined(__x86_64__) || defined(__amd64__)
571 #define GPR_CYCLE_COUNTER_RDTSC_64 1
572 #else
573 #define GPR_CYCLE_COUNTER_FALLBACK 1
574 #endif
575 */
576 #define GPR_CYCLE_COUNTER_FALLBACK 1
577 #else
578 #define GPR_CYCLE_COUNTER_FALLBACK 1
579 #endif /* GPR_LINUX */
580 
581 /* Cache line alignment */
582 #ifndef GPR_CACHELINE_SIZE_LOG
583 #if defined(__i386__) || defined(__x86_64__)
584 #define GPR_CACHELINE_SIZE_LOG 6
585 #endif
586 #ifndef GPR_CACHELINE_SIZE_LOG
587 /* A reasonable default guess. Note that overestimates tend to waste more
588  space, while underestimates tend to waste more time. */
589 #define GPR_CACHELINE_SIZE_LOG 6
590 #endif /* GPR_CACHELINE_SIZE_LOG */
591 #endif /* GPR_CACHELINE_SIZE_LOG */
592 
593 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
594 
595 /* scrub GCC_ATOMIC if it's not available on this compiler */
596 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
597 #undef GPR_GCC_ATOMIC
598 #define GPR_GCC_SYNC 1
599 #endif
600 
601 /* Validate platform combinations */
602 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + \
603  defined(GPR_WINDOWS_ATOMIC) != \
604  1
605 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WINDOWS_ATOMIC
606 #endif
607 
608 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
609 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
610 #endif
611 
612 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WINDOWS) + \
613  defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != \
614  1
615 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WINDOWS, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
616 #endif
617 
618 /* maximum alignment needed for any type on this platform, rounded up to a
619  power of two */
620 #define GPR_MAX_ALIGNMENT 16
621 
622 #ifndef GRPC_ARES
623 #define GRPC_ARES 1
624 #endif
625 
626 #ifndef GRPC_IF_NAMETOINDEX
627 #define GRPC_IF_NAMETOINDEX 1
628 #endif
629 
630 #ifndef GRPC_UNUSED
631 #if defined(__GNUC__) && !defined(__MINGW32__)
632 #define GRPC_UNUSED __attribute__((unused))
633 #else
634 #define GRPC_UNUSED
635 #endif
636 #endif
637 
638 #ifndef GPR_PRINT_FORMAT_CHECK
639 #ifdef __GNUC__
640 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS) \
641  __attribute__((format(printf, FORMAT_STR, ARGS)))
642 #else
643 #define GPR_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS)
644 #endif
645 #endif /* GPR_PRINT_FORMAT_CHECK */
646 
647 #ifndef GPR_HAS_CPP_ATTRIBUTE
648 #ifdef __has_cpp_attribute
649 #define GPR_HAS_CPP_ATTRIBUTE(a) __has_cpp_attribute(a)
650 #else
651 #define GPR_HAS_CPP_ATTRIBUTE(a) 0
652 #endif
653 #endif /* GPR_HAS_CPP_ATTRIBUTE */
654 
655 #if defined(__GNUC__) && !defined(__MINGW32__)
656 #define GPR_ALIGN_STRUCT(n) __attribute__((aligned(n)))
657 #else
658 #define GPR_ALIGN_STRUCT(n)
659 #endif
660 
661 #ifndef GRPC_MUST_USE_RESULT
662 #if GPR_HAS_CPP_ATTRIBUTE(nodiscard)
663 #define GRPC_MUST_USE_RESULT [[nodiscard]]
664 #elif defined(__GNUC__) && !defined(__MINGW32__)
665 #define GRPC_MUST_USE_RESULT __attribute__((warn_unused_result))
666 #else
667 #define GRPC_MUST_USE_RESULT
668 #endif
669 #ifdef USE_STRICT_WARNING
670 /* When building with USE_STRICT_WARNING (which -Werror), types with this
671  attribute will be treated as annotated with warn_unused_result, enforcing
672  returned values of this type should be used.
673  This is added in grpc::Status in mind to address the issue where it always
674  has this annotation internally but OSS doesn't, sometimes causing internal
675  build failure. To prevent this, this is added while not introducing
676  a breaking change to existing user code which may not use returned values
677  of grpc::Status. */
678 #define GRPC_MUST_USE_RESULT_WHEN_USE_STRICT_WARNING GRPC_MUST_USE_RESULT
679 #else
680 #define GRPC_MUST_USE_RESULT_WHEN_USE_STRICT_WARNING
681 #endif
682 #endif
683 
684 #ifndef GRPC_REINITIALIZES
685 #if defined(__clang__)
686 #if GPR_HAS_CPP_ATTRIBUTE(clang::reinitializes)
687 #define GRPC_REINITIALIZES [[clang::reinitializes]]
688 #else
689 #define GRPC_REINITIALIZES
690 #endif
691 #else
692 #define GRPC_REINITIALIZES
693 #endif
694 #endif
695 
696 #ifndef GPR_HAS_ATTRIBUTE
697 #ifdef __has_attribute
698 #define GPR_HAS_ATTRIBUTE(a) __has_attribute(a)
699 #else
700 #define GPR_HAS_ATTRIBUTE(a) 0
701 #endif
702 #endif /* GPR_HAS_ATTRIBUTE */
703 
704 #if GPR_HAS_ATTRIBUTE(noreturn)
705 #define GPR_ATTRIBUTE_NORETURN __attribute__((noreturn))
706 #else
707 #define GPR_ATTRIBUTE_NORETURN
708 #endif
709 
710 #if defined(GPR_FORBID_UNREACHABLE_CODE) && GPR_FORBID_UNREACHABLE_CODE
711 #define GPR_UNREACHABLE_CODE(STATEMENT)
712 #else
713 #ifdef __cplusplus
714 extern "C" {
715 #endif
716 extern void gpr_unreachable_code(const char* reason, const char* file,
717  int line) GPR_ATTRIBUTE_NORETURN;
718 #ifdef __cplusplus
719 }
720 #endif
721 #define GPR_UNREACHABLE_CODE(STATEMENT) \
722  do { \
723  gpr_unreachable_code(#STATEMENT, __FILE__, __LINE__); \
724  STATEMENT; \
725  } while (0)
726 #endif /* GPR_FORBID_UNREACHABLE_CODE */
727 
728 #ifndef GPRAPI
729 #define GPRAPI
730 #endif
731 
732 #ifndef GRPCAPI
733 #define GRPCAPI GPRAPI
734 #endif
735 
736 #ifndef CENSUSAPI
737 #define CENSUSAPI GRPCAPI
738 #endif
739 
740 #ifndef GPR_HAS_FEATURE
741 #ifdef __has_feature
742 #define GPR_HAS_FEATURE(a) __has_feature(a)
743 #else
744 #define GPR_HAS_FEATURE(a) 0
745 #endif
746 #endif /* GPR_HAS_FEATURE */
747 
748 #ifndef GPR_ATTRIBUTE_NOINLINE
749 #if GPR_HAS_ATTRIBUTE(noinline) || (defined(__GNUC__) && !defined(__clang__))
750 #define GPR_ATTRIBUTE_NOINLINE __attribute__((noinline))
751 #define GPR_HAS_ATTRIBUTE_NOINLINE 1
752 #else
753 #define GPR_ATTRIBUTE_NOINLINE
754 #endif
755 #endif /* GPR_ATTRIBUTE_NOINLINE */
756 
757 #ifndef GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION
758 #ifdef __cplusplus
759 #if GPR_HAS_CPP_ATTRIBUTE(clang::always_inline)
760 #define GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION [[clang::always_inline]]
761 #elif GPR_HAS_ATTRIBUTE(always_inline)
762 #define GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION __attribute__((always_inline))
763 #else
764 // TODO(ctiller): add __forceinline for MSVC
765 #define GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION
766 #endif
767 #else
768 // Disable for C code
769 #define GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION
770 #endif
771 #endif /* GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION */
772 
773 #ifndef GPR_NO_UNIQUE_ADDRESS
774 #if GPR_HAS_CPP_ATTRIBUTE(no_unique_address)
775 #define GPR_NO_UNIQUE_ADDRESS [[no_unique_address]]
776 #else
777 #define GPR_NO_UNIQUE_ADDRESS
778 #endif
779 #endif /* GPR_NO_UNIQUE_ADDRESS */
780 
781 #ifndef GRPC_DEPRECATED
782 #if GPR_HAS_CPP_ATTRIBUTE(deprecated)
783 #define GRPC_DEPRECATED(reason) [[deprecated(reason)]]
784 #else
785 #define GRPC_DEPRECATED(reason)
786 #endif
787 #endif /* GRPC_DEPRECATED */
788 
789 #ifndef GPR_ATTRIBUTE_WEAK
790 /* Attribute weak is broken on LLVM/windows:
791  * https://bugs.llvm.org/show_bug.cgi?id=37598 */
792 #if (GPR_HAS_ATTRIBUTE(weak) || (defined(__GNUC__) && !defined(__clang__))) && \
793  !(defined(__llvm__) && defined(_WIN32))
794 #define GPR_ATTRIBUTE_WEAK __attribute__((weak))
795 #define GPR_HAS_ATTRIBUTE_WEAK 1
796 #else
797 #define GPR_ATTRIBUTE_WEAK
798 #endif
799 #endif /* GPR_ATTRIBUTE_WEAK */
800 
801 #ifndef GPR_ATTRIBUTE_NO_TSAN /* (1) */
802 #if GPR_HAS_FEATURE(thread_sanitizer)
803 #define GPR_ATTRIBUTE_NO_TSAN __attribute__((no_sanitize("thread")))
804 #endif /* GPR_HAS_FEATURE */
805 #ifndef GPR_ATTRIBUTE_NO_TSAN /* (2) */
806 #define GPR_ATTRIBUTE_NO_TSAN
807 #endif /* GPR_ATTRIBUTE_NO_TSAN (2) */
808 #endif /* GPR_ATTRIBUTE_NO_TSAN (1) */
809 
810 /* GRPC_TSAN_ENABLED will be defined, when compiled with thread sanitizer. */
811 #ifndef GRPC_TSAN_SUPPRESSED
812 #if defined(__SANITIZE_THREAD__)
813 #define GRPC_TSAN_ENABLED
814 #elif GPR_HAS_FEATURE(thread_sanitizer)
815 #define GRPC_TSAN_ENABLED
816 #endif
817 #endif
818 
819 /* GRPC_ASAN_ENABLED will be defined, when compiled with address sanitizer. */
820 #ifndef GRPC_ASAN_SUPPRESSED
821 #if defined(__SANITIZE_ADDRESS__)
822 #define GRPC_ASAN_ENABLED
823 #elif GPR_HAS_FEATURE(address_sanitizer)
824 #define GRPC_ASAN_ENABLED
825 #endif
826 #endif
827 
828 /* GRPC_ALLOW_EXCEPTIONS should be 0 or 1 if exceptions are allowed or not */
829 #ifndef GRPC_ALLOW_EXCEPTIONS
830 #ifdef GPR_WINDOWS
831 #if defined(_MSC_VER) && defined(_CPPUNWIND)
832 #define GRPC_ALLOW_EXCEPTIONS 1
833 #elif defined(__EXCEPTIONS)
834 #define GRPC_ALLOW_EXCEPTIONS 1
835 #else
836 #define GRPC_ALLOW_EXCEPTIONS 0
837 #endif
838 #else /* GPR_WINDOWS */
839 #ifdef __EXCEPTIONS
840 #define GRPC_ALLOW_EXCEPTIONS 1
841 #else /* __EXCEPTIONS */
842 #define GRPC_ALLOW_EXCEPTIONS 0
843 #endif /* __EXCEPTIONS */
844 #endif /* __GPR_WINDOWS */
845 #endif /* GRPC_ALLOW_EXCEPTIONS */
846 
847 #ifdef __has_builtin
848 #define GRPC_HAS_BUILTIN(a) __has_builtin(a)
849 #else
850 #define GRPC_HAS_BUILTIN(a) 0
851 #endif
852 
853 /* Use GPR_LIKELY only in cases where you are sure that a certain outcome is the
854  * most likely. Ideally, also collect performance numbers to justify the claim.
855  */
856 #ifdef __GNUC__
857 #define GPR_LIKELY(x) __builtin_expect((x), 1)
858 #define GPR_UNLIKELY(x) __builtin_expect((x), 0)
859 #else /* __GNUC__ */
860 #define GPR_LIKELY(x) (x)
861 #define GPR_UNLIKELY(x) (x)
862 #endif /* __GNUC__ */
863 
864 #ifndef __STDC_FORMAT_MACROS
865 #define __STDC_FORMAT_MACROS
866 #endif
867 
868 /* MSVC doesn't do the empty base class optimization in debug builds by default,
869  * and because of ABI likely won't.
870  * This enables it for specific types, use as:
871  * class GPR_MSVC_EMPTY_BASE_CLASS_WORKAROUND Foo : public A, public B, public C
872  * {}; */
873 #ifndef GPR_MSVC_EMPTY_BASE_CLASS_WORKAROUND
874 #ifdef GPR_WINDOWS
875 #define GPR_MSVC_EMPTY_BASE_CLASS_WORKAROUND __declspec(empty_bases)
876 #else
877 #define GPR_MSVC_EMPTY_BASE_CLASS_WORKAROUND
878 #endif
879 #endif
880 
881 #define GRPC_CALLBACK_API_NONEXPERIMENTAL
882 
883 /* clang 12 and lower with msan miscompiles destruction of [[no_unique_address]]
884  * members of zero size - for a repro see:
885  * test/core/compiler_bugs/miscompile_with_no_unique_address_test.cc
886  */
887 #ifdef __clang__
888 #if __clang__ && __clang_major__ <= 12 && __has_feature(memory_sanitizer)
889 #undef GPR_NO_UNIQUE_ADDRESS
890 #define GPR_NO_UNIQUE_ADDRESS
891 #endif
892 #endif
893 
894 #endif /* GRPC_SUPPORT_PORT_PLATFORM_H */
GPR_ATTRIBUTE_NORETURN
#define GPR_ATTRIBUTE_NORETURN
Definition: port_platform.h:707
gpr_unreachable_code
void gpr_unreachable_code(const char *reason, const char *file, int line)