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