ucommon
platform.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
27 #ifndef _UCOMMON_PLATFORM_H_
28 #define _UCOMMON_PLATFORM_H_
29 #define UCOMMON_ABI 5
30 
41 #define UCOMMON_NAMESPACE ucommon
42 #define NAMESPACE_UCOMMON namespace ucommon {
43 #define NAMESPACE_EXTERN_C extern "C" {
44 #define END_NAMESPACE }
45 #define EXTERN_C extern "C"
46 
47 #ifndef _REENTRANT
48 #define _REENTRANT 1
49 #endif
50 
51 #ifndef __PTH__
52 #ifndef _THREADSAFE
53 #define _THREADSAFE 1
54 #endif
55 
56 #ifndef _POSIX_PTHREAD_SEMANTICS
57 #define _POSIX_PTHREAD_SEMANTICS
58 #endif
59 #endif
60 
61 #if defined(__GNUC__) && (__GNUC < 3) && !defined(_GNU_SOURCE)
62 #define _GNU_SOURCE
63 #endif
64 
65 #if __GNUC__ > 3 || (__GNUC__ == 3 && (__GNU_MINOR__ > 3))
66 #define __PRINTF(x,y) __attribute__ ((format (printf, x, y)))
67 #define __SCANF(x, y) __attribute__ ((format (scanf, x, y)))
68 #define __MALLOC __attribute__ ((malloc))
69 #endif
70 
71 #ifndef __MALLOC
72 #define __PRINTF(x, y)
73 #define __SCANF(x, y)
74 #define __MALLOC
75 #endif
76 
77 #ifndef DEBUG
78 #ifndef NDEBUG
79 #define NDEBUG
80 #endif
81 #endif
82 
83 #ifdef DEBUG
84 #ifdef NDEBUG
85 #undef NDEBUG
86 #endif
87 #endif
88 
89 // see if we are building for or using extended stdc++ runtime library support
90 
91 #if defined(NEW_STDCPP) || defined(OLD_STDCPP)
92 #define _UCOMMON_EXTENDED_
93 #endif
94 
95 // see if targeting legacy Microsoft windows platform
96 
97 #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
98 #define _MSWINDOWS_
99 
100 #if defined(_M_X64) || defined(_M_ARM)
101 #define _MSCONDITIONALS_
102 #ifndef _WIN32_WINNT 0x0600
103 #define _WIN32_WINNT
104 #endif
105 #endif
106 
107 //#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0501
108 //#undef _WIN32_WINNT
109 //#define _WIN32_WINNT 0x0501
110 //#endif
111 
112 //#ifndef _WIN32_WINNT
113 //#define _WIN32_WINNT 0x0501
114 //#endif
115 
116 #pragma warning(disable: 4251)
117 #pragma warning(disable: 4996)
118 #pragma warning(disable: 4355)
119 #pragma warning(disable: 4290)
120 #pragma warning(disable: 4291)
121 
122 #if defined(__BORLANDC__) && !defined(__MT__)
123 #error Please enable multithreading
124 #endif
125 
126 #if defined(_MSC_VER) && !defined(_MT)
127 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
128 #endif
129 
130 // Require for compiling with critical sections.
131 #ifndef _WIN32_WINNT
132 #define _WIN32_WINNT 0x0501
133 #endif
134 
135 // Make sure we're consistent with _WIN32_WINNT
136 #ifndef WINVER
137 #define WINVER _WIN32_WINNT
138 #endif
139 
140 #ifndef WIN32_LEAN_AND_MEAN
141 #define WIN32_LEAN_AND_MEAN
142 #endif
143 
144 #include <winsock2.h>
145 #include <ws2tcpip.h>
146 
147 #if defined(_MSC_VER)
148 typedef signed long ssize_t;
149 typedef int pid_t;
150 #endif
151 
152 #include <process.h>
153 #ifndef __EXPORT
154 #ifdef UCOMMON_STATIC
155 #define __EXPORT
156 #else
157 #define __EXPORT __declspec(dllimport)
158 #endif
159 #endif
160 #define __LOCAL
161 
162 // if runtime mode then non-runtime libraries are static on windows...
163 #if defined(UCOMMON_RUNTIME) || defined(UCOMMON_STATIC)
164 #define __SHARED
165 #else
166 #define __SHARED __EXPORT
167 #endif
168 
169 #elif UCOMMON_VISIBILITY > 0
170 #define __EXPORT __attribute__ ((visibility("default")))
171 #define __LOCAL __attribute__ ((visibility("hidden")))
172 #define __SHARED __attribute__ ((visibility("default")))
173 #else
174 #define __EXPORT
175 #define __LOCAL
176 #define __SHARED
177 #endif
178 
179 #ifdef _MSWINDOWS_
180 
181 #define _UWIN
182 
183 #include <sys/stat.h>
184 #include <io.h>
185 
186 typedef DWORD pthread_t;
187 typedef CRITICAL_SECTION pthread_mutex_t;
188 typedef char *caddr_t;
189 typedef HANDLE fd_t;
190 typedef SOCKET socket_t;
191 
192 typedef struct timespec {
193  time_t tv_sec;
194  long tv_nsec;
195 } timespec_t;
196 
197 extern "C" {
198 
199  #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
200 
201  typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
202 
203  inline void sleep(int seconds)
204  {::Sleep((seconds * 1000l));};
205 
206  inline void pthread_exit(void *p)
207  {_endthreadex((DWORD)p);};
208 
209  inline pthread_t pthread_self(void)
210  {return (pthread_t)GetCurrentThreadId();};
211 
212  inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
213  {InitializeCriticalSection(mutex); return 0;};
214 
215  inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
216  {DeleteCriticalSection(mutex);};
217 
218  inline void pthread_mutex_lock(pthread_mutex_t *mutex)
219  {EnterCriticalSection(mutex);};
220 
221  inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
222  {LeaveCriticalSection(mutex);};
223 
224  inline char *strdup(const char *s)
225  {return _strdup(s);};
226 
227  inline int stricmp(const char *s1, const char *s2)
228  {return _stricmp(s1, s2);};
229 
230  inline int strnicmp(const char *s1, const char *s2, size_t l)
231  {return _strnicmp(s1, s2, l);};
232 };
233 
234 #elif defined(__PTH__)
235 
236 #include <pth.h>
237 #include <sys/wait.h>
238 
239 typedef int socket_t;
240 typedef int fd_t;
241 #define INVALID_SOCKET -1
242 #define INVALID_HANDLE_VALUE -1
243 #include <signal.h>
244 
245 #define pthread_mutex_t pth_mutex_t
246 #define pthread_cond_t pth_cond_t
247 #define pthread_t pth_t
248 
249 inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
250  {return pth_sigmask(how, set, oset);};
251 
252 inline void pthread_exit(void *p)
253  {pth_exit(p);};
254 
255 inline void pthread_kill(pthread_t tid, int sig)
256  {pth_raise(tid, sig);};
257 
258 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
259  {return pth_mutex_init(mutex) != 0;};
260 
261 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
262  {};
263 
264 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
265  {pth_mutex_acquire(mutex, 0, NULL);};
266 
267 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
268  {pth_mutex_release(mutex);};
269 
270 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
271  {pth_cond_await(cond, mutex, NULL);};
272 
273 inline void pthread_cond_signal(pthread_cond_t *cond)
274  {pth_cond_notify(cond, FALSE);};
275 
276 inline void pthread_cond_broadcast(pthread_cond_t *cond)
277  {pth_cond_notify(cond, TRUE);};
278 
279 #else
280 
281 #include <pthread.h>
282 
283 typedef int socket_t;
284 typedef int fd_t;
285 #define INVALID_SOCKET -1
286 #define INVALID_HANDLE_VALUE -1
287 #include <signal.h>
288 
289 #endif
290 
291 #ifdef _MSC_VER
292 typedef signed __int8 int8_t;
293 typedef unsigned __int8 uint8_t;
294 typedef signed __int16 int16_t;
295 typedef unsigned __int16 uint16_t;
296 typedef signed __int32 int32_t;
297 typedef unsigned __int32 uint32_t;
298 typedef signed __int64 int64_t;
299 typedef unsigned __int64 uint64_t;
300 typedef char *caddr_t;
301 
302 #include <stdio.h>
303 #define snprintf _snprintf
304 #define vsnprintf _vsnprintf
305 
306 #else
307 
308 #include <sys/stat.h>
309 #include <sys/types.h>
310 #include <stdint.h>
311 #include <unistd.h>
312 
313 #endif
314 
315 #ifndef _GNU_SOURCE
316 typedef void (*sighandler_t)(int);
317 #endif
318 typedef unsigned long timeout_t;
320 #include <stdlib.h>
321 #include <errno.h>
322 
323 #ifdef _MSWINDOWS_
324 #ifndef ENETDOWN
325 #define ENETDOWN ((int)(WSAENETDOWN))
326 #endif
327 #ifndef EINPROGRESS
328 #define EINPROGRESS ((int)(WSAEINPROGRESS))
329 #endif
330 #ifndef ENOPROTOOPT
331 #define ENOPROTOOPT ((int)(WSAENOPROTOOPT))
332 #endif
333 #ifndef EADDRINUSE
334 #define EADDRINUSE ((int)(WSAEADDRINUSE))
335 #endif
336 #ifndef EADDRNOTAVAIL
337 #define EADDRNOTAVAIL ((int)(WSAEADDRNOTAVAIL))
338 #endif
339 #ifndef ENETUNREACH
340 #define ENETUNREACH ((int)(WSAENETUNREACH))
341 #endif
342 #ifndef EHOSTUNREACH
343 #define EHOSTUNREACH ((int)(WSAEHOSTUNREACH))
344 #endif
345 #ifndef EHOSTDOWN
346 #define EHOSTDOWN ((int)(WSAEHOSTDOWN))
347 #endif
348 #ifndef ENETRESET
349 #define ENETRESET ((int)(WSAENETRESET))
350 #endif
351 #ifndef ECONNABORTED
352 #define ECONNABORTED ((int)(WSAECONNABORTED))
353 #endif
354 #ifndef ECONNRESET
355 #define ECONNRESET ((int)(WSAECONNRESET))
356 #endif
357 #ifndef EISCONN
358 #define EISCONN ((int)(WSAEISCONN))
359 #endif
360 #ifndef ENOTCONN
361 #define ENOTCONN ((int)(WSAENOTCONN))
362 #endif
363 #ifndef ESHUTDOWN
364 #define ESHUTDOWN ((int)(WSAESHUTDOWN))
365 #endif
366 #ifndef ETIMEDOUT
367 #define ETIMEDOUT ((int)(WSAETIMEDOUT))
368 #endif
369 #ifndef ECONNREFUSED
370 #define ECONNREFUSED ((int)(WSAECONNREFUSED))
371 #endif
372 #endif
373 
374 #ifndef DEBUG
375 #ifndef NDEBUG
376 #define NDEBUG
377 #endif
378 #endif
379 
380 #ifdef DEBUG
381 #ifdef NDEBUG
382 #undef NDEBUG
383 #endif
384 #endif
385 
386 #ifndef PROGRAM_MAIN
387 #define PROGRAM_MAIN(argc, argv) extern "C" int main(int argc, char **argv)
388 #define PROGRAM_EXIT(code) return code
389 #endif
390 
391 #ifndef SERVICE_MAIN
392 #define SERVICE_MAIN(id, argc, argv) void service_##id(int argc, char **argv)
393 typedef void (*cpr_service_t)(int argc, char **argv);
394 #endif
395 
396 #include <assert.h>
397 #ifdef DEBUG
398 #define crit(x, text) assert(x)
399 #else
400 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
401 #endif
402 
409 template<class T>
410 inline T *init(T *memory)
411  {return ((memory) ? new(((caddr_t)memory)) T : NULL);}
412 
413 typedef long Integer;
414 typedef unsigned long Unsigned;
415 typedef double Real;
416 
417 #endif