gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
port_platform.h
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_SUPPORT_PORT_PLATFORM_H
35 #define GRPC_SUPPORT_PORT_PLATFORM_H
36 
37 /* Override this file with one for your platform if you need to redefine
38  things. */
39 
40 #if !defined(GPR_NO_AUTODETECT_PLATFORM)
41 #if defined(_WIN64) || defined(WIN64)
42 #define GPR_WIN32 1
43 #define GPR_ARCH_64 1
44 #define GPR_GETPID_IN_PROCESS_H 1
45 #define GPR_WINSOCK_SOCKET 1
46 #ifdef __GNUC__
47 #define GPR_GCC_ATOMIC 1
48 #define GPR_GCC_TLS 1
49 #else
50 #define GPR_WIN32_ATOMIC 1
51 #define GPR_MSVC_TLS 1
52 #endif
53 #elif defined(_WIN32) || defined(WIN32)
54 #define GPR_ARCH_32 1
55 #define GPR_WIN32 1
56 #define GPR_GETPID_IN_PROCESS_H 1
57 #define GPR_WINSOCK_SOCKET 1
58 #ifdef __GNUC__
59 #define GPR_GCC_ATOMIC 1
60 #define GPR_GCC_TLS 1
61 #else
62 #define GPR_WIN32_ATOMIC 1
63 #define GPR_MSVC_TLS 1
64 #endif
65 #elif defined(ANDROID) || defined(__ANDROID__)
66 #define GPR_ANDROID 1
67 #define GPR_ARCH_32 1
68 #define GPR_CPU_LINUX 1
69 #define GPR_GCC_SYNC 1
70 #define GPR_GCC_TLS 1
71 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
72 #define GPR_POSIX_WAKEUP_FD 1
73 #define GPR_LINUX_EVENTFD 1
74 #define GPR_POSIX_SOCKET 1
75 #define GPR_POSIX_SOCKETADDR 1
76 #define GPR_POSIX_SOCKETUTILS 1
77 #define GPR_POSIX_ENV 1
78 #define GPR_POSIX_FILE 1
79 #define GPR_POSIX_STRING 1
80 #define GPR_POSIX_SUBPROCESS 1
81 #define GPR_POSIX_SYNC 1
82 #define GPR_POSIX_TIME 1
83 #define GPR_GETPID_IN_UNISTD_H 1
84 #define GPR_HAVE_MSG_NOSIGNAL 1
85 #elif defined(__linux__)
86 #ifndef _BSD_SOURCE
87 #define _BSD_SOURCE
88 #endif
89 #ifndef _DEFAULT_SOURCE
90 #define _DEFAULT_SOURCE
91 #endif
92 #ifndef _GNU_SOURCE
93 #define _GNU_SOURCE
94 #endif
95 #include <features.h>
96 #define GPR_CPU_LINUX 1
97 #define GPR_GCC_ATOMIC 1
98 #define GPR_GCC_TLS 1
99 #define GPR_LINUX 1
100 #define GPR_LINUX_MULTIPOLL_WITH_EPOLL 1
101 #define GPR_POSIX_WAKEUP_FD 1
102 #define GPR_POSIX_SOCKET 1
103 #define GPR_POSIX_SOCKETADDR 1
104 #ifdef __GLIBC_PREREQ
105 #if __GLIBC_PREREQ(2, 9)
106 #define GPR_LINUX_EVENTFD 1
107 #endif
108 #if __GLIBC_PREREQ(2, 10)
109 #define GPR_LINUX_SOCKETUTILS 1
110 #endif
111 #if __GLIBC_PREREQ(2, 17)
112 #define GPR_LINUX_ENV 1
113 #endif
114 #endif
115 #ifndef GPR_LINUX_EVENTFD
116 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
117 #endif
118 #ifndef GPR_LINUX_SOCKETUTILS
119 #define GPR_POSIX_SOCKETUTILS
120 #endif
121 #ifndef GPR_LINUX_ENV
122 #define GPR_POSIX_ENV 1
123 #endif
124 #define GPR_POSIX_FILE 1
125 #define GPR_POSIX_STRING 1
126 #define GPR_POSIX_SUBPROCESS 1
127 #define GPR_POSIX_SYNC 1
128 #define GPR_POSIX_TIME 1
129 #define GPR_GETPID_IN_UNISTD_H 1
130 #define GPR_HAVE_MSG_NOSIGNAL 1
131 #ifdef _LP64
132 #define GPR_ARCH_64 1
133 #else /* _LP64 */
134 #define GPR_ARCH_32 1
135 #endif /* _LP64 */
136 #elif defined(__APPLE__)
137 #include <TargetConditionals.h>
138 #ifndef _BSD_SOURCE
139 #define _BSD_SOURCE
140 #endif
141 #if TARGET_OS_IPHONE
142 #define GPR_CPU_IPHONE 1
143 #define GPR_PTHREAD_TLS 1
144 #else /* TARGET_OS_IPHONE */
145 #define GPR_CPU_POSIX 1
146 #define GPR_GCC_TLS 1
147 #endif
148 #define GPR_GCC_ATOMIC 1
149 #define GPR_POSIX_LOG 1
150 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
151 #define GPR_POSIX_WAKEUP_FD 1
152 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
153 #define GPR_POSIX_SOCKET 1
154 #define GPR_POSIX_SOCKETADDR 1
155 #define GPR_POSIX_SOCKETUTILS 1
156 #define GPR_POSIX_ENV 1
157 #define GPR_POSIX_FILE 1
158 #define GPR_POSIX_STRING 1
159 #define GPR_POSIX_SUBPROCESS 1
160 #define GPR_POSIX_SYNC 1
161 #define GPR_POSIX_TIME 1
162 #define GPR_GETPID_IN_UNISTD_H 1
163 #define GPR_HAVE_SO_NOSIGPIPE 1
164 #ifdef _LP64
165 #define GPR_ARCH_64 1
166 #else /* _LP64 */
167 #define GPR_ARCH_32 1
168 #endif /* _LP64 */
169 #elif defined(__FreeBSD__)
170 #ifndef _BSD_SOURCE
171 #define _BSD_SOURCE
172 #endif
173 #define GPR_CPU_POSIX 1
174 #define GPR_GCC_ATOMIC 1
175 #define GPR_GCC_TLS 1
176 #define GPR_POSIX_LOG 1
177 #define GPR_POSIX_MULTIPOLL_WITH_POLL 1
178 #define GPR_POSIX_WAKEUP_FD 1
179 #define GPR_POSIX_NO_SPECIAL_WAKEUP_FD 1
180 #define GPR_POSIX_SOCKET 1
181 #define GPR_POSIX_SOCKETADDR 1
182 #define GPR_POSIX_SOCKETUTILS 1
183 #define GPR_POSIX_ENV 1
184 #define GPR_POSIX_FILE 1
185 #define GPR_POSIX_STRING 1
186 #define GPR_POSIX_SUBPROCESS 1
187 #define GPR_POSIX_SYNC 1
188 #define GPR_POSIX_TIME 1
189 #define GPR_GETPID_IN_UNISTD_H 1
190 #define GPR_HAVE_SO_NOSIGPIPE 1
191 #ifdef _LP64
192 #define GPR_ARCH_64 1
193 #else /* _LP64 */
194 #define GPR_ARCH_32 1
195 #endif /* _LP64 */
196 #else
197 #error Could not auto-detect platform
198 #endif
199 #endif /* GPR_NO_AUTODETECT_PLATFORM */
200 
201 /* For a common case, assume that the platform has a C99-like stdint.h */
202 
203 #include <stdint.h>
204 
205 /* Cache line alignment */
206 #ifndef GPR_CACHELINE_SIZE_LOG
207 #if defined(__i386__) || defined(__x86_64__)
208 #define GPR_CACHELINE_SIZE_LOG 6
209 #endif
210 #ifndef GPR_CACHELINE_SIZE_LOG
211 /* A reasonable default guess. Note that overestimates tend to waste more
212  space, while underestimates tend to waste more time. */
213 #define GPR_CACHELINE_SIZE_LOG 6
214 #endif /* GPR_CACHELINE_SIZE_LOG */
215 #endif /* GPR_CACHELINE_SIZE_LOG */
216 
217 #define GPR_CACHELINE_SIZE (1 << GPR_CACHELINE_SIZE_LOG)
218 
219 /* scrub GCC_ATOMIC if it's not available on this compiler */
220 #if defined(GPR_GCC_ATOMIC) && !defined(__ATOMIC_RELAXED)
221 #undef GPR_GCC_ATOMIC
222 #define GPR_GCC_SYNC 1
223 #endif
224 
225 /* Validate platform combinations */
226 #if defined(GPR_GCC_ATOMIC) + defined(GPR_GCC_SYNC) + defined(GPR_WIN32_ATOMIC) != 1
227 #error Must define exactly one of GPR_GCC_ATOMIC, GPR_GCC_SYNC, GPR_WIN32_ATOMIC
228 #endif
229 
230 #if defined(GPR_ARCH_32) + defined(GPR_ARCH_64) != 1
231 #error Must define exactly one of GPR_ARCH_32, GPR_ARCH_64
232 #endif
233 
234 #if defined(GPR_CPU_LINUX) + defined(GPR_CPU_POSIX) + defined(GPR_WIN32) + defined(GPR_CPU_IPHONE) + defined(GPR_CPU_CUSTOM) != 1
235 #error Must define exactly one of GPR_CPU_LINUX, GPR_CPU_POSIX, GPR_WIN32, GPR_CPU_IPHONE, GPR_CPU_CUSTOM
236 #endif
237 
238 #if defined(GPR_POSIX_MULTIPOLL_WITH_POLL) && !defined(GPR_POSIX_SOCKET)
239 #error Must define GPR_POSIX_SOCKET to use GPR_POSIX_MULTIPOLL_WITH_POLL
240 #endif
241 
242 #if defined(GPR_POSIX_SOCKET) + defined(GPR_WINSOCK_SOCKET) + defined(GPR_CUSTOM_SOCKET) != 1
243 #error Must define exactly one of GPR_POSIX_SOCKET, GPR_WINSOCK_SOCKET, GPR_CUSTOM_SOCKET
244 #endif
245 
246 #if defined(GPR_MSVC_TLS) + defined(GPR_GCC_TLS) + defined(GPR_PTHREAD_TLS) + defined(GPR_CUSTOM_TLS) != 1
247 #error Must define exactly one of GPR_MSVC_TLS, GPR_GCC_TLS, GPR_PTHREAD_TLS, GPR_CUSTOM_TLS
248 #endif
249 
250 typedef int16_t gpr_int16;
251 typedef int32_t gpr_int32;
252 typedef int64_t gpr_int64;
253 typedef uint8_t gpr_uint8;
254 typedef uint16_t gpr_uint16;
255 typedef uint32_t gpr_uint32;
256 typedef uint64_t gpr_uint64;
257 typedef intmax_t gpr_intmax;
258 typedef intptr_t gpr_intptr;
259 typedef uintmax_t gpr_uintmax;
260 typedef uintptr_t gpr_uintptr;
261 
262 /* INT64_MAX is unavailable on some platforms. */
263 #define GPR_INT64_MAX (gpr_int64)(~(gpr_uint64)0 >> 1)
264 
265 /* maximum alignment needed for any type on this platform, rounded up to a
266  power of two */
267 #define GPR_MAX_ALIGNMENT 16
268 
269 #endif /* GRPC_SUPPORT_PORT_PLATFORM_H */