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