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 6
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
103 #define _WIN32_WINNT 0x0600
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 #ifdef _MSC_VER
117 #pragma warning(disable: 4251)
118 #pragma warning(disable: 4996)
119 #pragma warning(disable: 4355)
120 #pragma warning(disable: 4290)
121 #pragma warning(disable: 4291)
122 #endif
123 
124 #if defined(__BORLANDC__) && !defined(__MT__)
125 #error Please enable multithreading
126 #endif
127 
128 #if defined(_MSC_VER) && !defined(_MT)
129 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
130 #endif
131 
132 // Require for compiling with critical sections.
133 #ifndef _WIN32_WINNT
134 #define _WIN32_WINNT 0x0501
135 #endif
136 
137 // Make sure we're consistent with _WIN32_WINNT
138 #ifndef WINVER
139 #define WINVER _WIN32_WINNT
140 #endif
141 
142 #ifndef WIN32_LEAN_AND_MEAN
143 #define WIN32_LEAN_AND_MEAN
144 #endif
145 
146 #include <winsock2.h>
147 #include <ws2tcpip.h>
148 
149 #if defined(_MSC_VER)
150 typedef signed long ssize_t;
151 typedef int pid_t;
152 #endif
153 
154 #include <process.h>
155 #ifndef __EXPORT
156 #ifdef UCOMMON_STATIC
157 #define __EXPORT
158 #else
159 #define __EXPORT __declspec(dllimport)
160 #endif
161 #endif
162 #define __LOCAL
163 
164 // if runtime mode then non-runtime libraries are static on windows...
165 #if defined(UCOMMON_RUNTIME) || defined(UCOMMON_STATIC)
166 #define __SHARED
167 #else
168 #define __SHARED __EXPORT
169 #endif
170 
171 #elif UCOMMON_VISIBILITY > 0
172 #define __EXPORT __attribute__ ((visibility("default")))
173 #define __LOCAL __attribute__ ((visibility("hidden")))
174 #define __SHARED __attribute__ ((visibility("default")))
175 #else
176 #define __EXPORT
177 #define __LOCAL
178 #define __SHARED
179 #endif
180 
181 #ifdef _MSWINDOWS_
182 
183 #define _UWIN
184 
185 #include <sys/stat.h>
186 #include <io.h>
187 
188 typedef DWORD pthread_t;
189 typedef CRITICAL_SECTION pthread_mutex_t;
190 typedef char *caddr_t;
191 typedef HANDLE fd_t;
192 typedef SOCKET socket_t;
193 
194 #ifdef _MSC_VER
195 typedef struct timespec {
196  time_t tv_sec;
197  long tv_nsec;
198 } timespec_t;
199 #endif
200 
201 extern "C" {
202 
203  #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
204 
205  typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
206 
207  inline void sleep(int seconds)
208  {::Sleep((seconds * 1000l));}
209 
210  inline void pthread_exit(void *p)
211  {_endthreadex((DWORD)0);}
212 
213  inline pthread_t pthread_self(void)
214  {return (pthread_t)GetCurrentThreadId();}
215 
216  inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
217  {InitializeCriticalSection(mutex); return 0;}
218 
219  inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
220  {DeleteCriticalSection(mutex);}
221 
222  inline void pthread_mutex_lock(pthread_mutex_t *mutex)
223  {EnterCriticalSection(mutex);}
224 
225  inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
226  {LeaveCriticalSection(mutex);}
227 
228  inline char *strdup(const char *s)
229  {return _strdup(s);}
230 
231  inline int stricmp(const char *s1, const char *s2)
232  {return _stricmp(s1, s2);}
233 
234  inline int strnicmp(const char *s1, const char *s2, size_t l)
235  {return _strnicmp(s1, s2, l);}
236 }
237 
238 #elif defined(__PTH__)
239 
240 #include <pth.h>
241 #include <sys/wait.h>
242 
243 typedef int socket_t;
244 typedef int fd_t;
245 #define INVALID_SOCKET -1
246 #define INVALID_HANDLE_VALUE -1
247 #include <signal.h>
248 
249 #define pthread_mutex_t pth_mutex_t
250 #define pthread_cond_t pth_cond_t
251 #define pthread_t pth_t
252 
253 inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
254  {return pth_sigmask(how, set, oset);};
255 
256 inline void pthread_exit(void *p)
257  {pth_exit(p);};
258 
259 inline void pthread_kill(pthread_t tid, int sig)
260  {pth_raise(tid, sig);};
261 
262 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
263  {return pth_mutex_init(mutex) != 0;};
264 
265 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
266  {};
267 
268 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
269  {pth_mutex_acquire(mutex, 0, NULL);};
270 
271 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
272  {pth_mutex_release(mutex);};
273 
274 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
275  {pth_cond_await(cond, mutex, NULL);};
276 
277 inline void pthread_cond_signal(pthread_cond_t *cond)
278  {pth_cond_notify(cond, FALSE);};
279 
280 inline void pthread_cond_broadcast(pthread_cond_t *cond)
281  {pth_cond_notify(cond, TRUE);};
282 
283 #else
284 
285 #include <pthread.h>
286 
287 typedef int socket_t;
288 typedef int fd_t;
289 #define INVALID_SOCKET -1
290 #define INVALID_HANDLE_VALUE -1
291 #include <signal.h>
292 
293 #endif
294 
295 #ifdef _MSC_VER
296 typedef signed __int8 int8_t;
297 typedef unsigned __int8 uint8_t;
298 typedef signed __int16 int16_t;
299 typedef unsigned __int16 uint16_t;
300 typedef signed __int32 int32_t;
301 typedef unsigned __int32 uint32_t;
302 typedef signed __int64 int64_t;
303 typedef unsigned __int64 uint64_t;
304 typedef char *caddr_t;
305 
306 #include <stdio.h>
307 #define snprintf _snprintf
308 #define vsnprintf _vsnprintf
309 
310 #else
311 
312 #include <sys/stat.h>
313 #include <sys/types.h>
314 #include <stdint.h>
315 #include <unistd.h>
316 #include <stdio.h>
317 
318 #endif
319 
320 #undef getchar
321 #undef putchar
322 
323 #ifndef _GNU_SOURCE
324 typedef void (*sighandler_t)(int);
325 #endif
326 typedef unsigned long timeout_t;
328 #include <stdlib.h>
329 #include <ctype.h>
330 #include <limits.h>
331 #include <errno.h>
332 
333 #ifdef _MSWINDOWS_
334 #ifndef ENETDOWN
335 #define ENETDOWN ((int)(WSAENETDOWN))
336 #endif
337 #ifndef EINPROGRESS
338 #define EINPROGRESS ((int)(WSAEINPROGRESS))
339 #endif
340 #ifndef ENOPROTOOPT
341 #define ENOPROTOOPT ((int)(WSAENOPROTOOPT))
342 #endif
343 #ifndef EADDRINUSE
344 #define EADDRINUSE ((int)(WSAEADDRINUSE))
345 #endif
346 #ifndef EADDRNOTAVAIL
347 #define EADDRNOTAVAIL ((int)(WSAEADDRNOTAVAIL))
348 #endif
349 #ifndef ENETUNREACH
350 #define ENETUNREACH ((int)(WSAENETUNREACH))
351 #endif
352 #ifndef EHOSTUNREACH
353 #define EHOSTUNREACH ((int)(WSAEHOSTUNREACH))
354 #endif
355 #ifndef EHOSTDOWN
356 #define EHOSTDOWN ((int)(WSAEHOSTDOWN))
357 #endif
358 #ifndef ENETRESET
359 #define ENETRESET ((int)(WSAENETRESET))
360 #endif
361 #ifndef ECONNABORTED
362 #define ECONNABORTED ((int)(WSAECONNABORTED))
363 #endif
364 #ifndef ECONNRESET
365 #define ECONNRESET ((int)(WSAECONNRESET))
366 #endif
367 #ifndef EISCONN
368 #define EISCONN ((int)(WSAEISCONN))
369 #endif
370 #ifndef ENOTCONN
371 #define ENOTCONN ((int)(WSAENOTCONN))
372 #endif
373 #ifndef ESHUTDOWN
374 #define ESHUTDOWN ((int)(WSAESHUTDOWN))
375 #endif
376 #ifndef ETIMEDOUT
377 #define ETIMEDOUT ((int)(WSAETIMEDOUT))
378 #endif
379 #ifndef ECONNREFUSED
380 #define ECONNREFUSED ((int)(WSAECONNREFUSED))
381 #endif
382 #endif
383 
384 #ifndef DEBUG
385 #ifndef NDEBUG
386 #define NDEBUG
387 #endif
388 #endif
389 
390 #ifdef DEBUG
391 #ifdef NDEBUG
392 #undef NDEBUG
393 #endif
394 #endif
395 
396 #ifndef PROGRAM_MAIN
397 #define PROGRAM_MAIN(argc, argv) extern "C" int main(int argc, char **argv)
398 #define PROGRAM_EXIT(code) return code
399 #endif
400 
401 #ifndef SERVICE_MAIN
402 #define SERVICE_MAIN(id, argc, argv) void service_##id(int argc, char **argv)
403 typedef void (*cpr_service_t)(int argc, char **argv);
404 #endif
405 
406 #include <assert.h>
407 #ifdef DEBUG
408 #define crit(x, text) assert(x)
409 #else
410 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
411 #endif
412 
419 template<class T>
420 inline T *init(T *memory)
421  {return ((memory) ? new(((caddr_t)memory)) T : NULL);}
422 
423 typedef long Integer;
424 typedef unsigned long Unsigned;
425 typedef double Real;
426 
431 inline void strfree(char *str)
432  {::free(str);}
433 
434 #endif
int strnicmp(const char *string1, const char *string2, size_t max)
Convenience function for case insensitive null terminated string compare.
Definition: string.h:1640
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Definition: platform.h:420
int stricmp(const char *string1, const char *string2)
Convenience function for case insensitive null terminated string compare.
Definition: string.h:1630
void(* sighandler_t)(int)
Convenient typedef for signal handlers.
Definition: platform.h:324
void strfree(char *str)
Matching function for strdup().
Definition: platform.h:431
unsigned long timeout_t
Typedef for millisecond timer values.
Definition: platform.h:326
Process services.