UCommon
|
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. 00002 // 00003 // This file is part of GNU uCommon C++. 00004 // 00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU Lesser General Public License as published 00007 // by the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // GNU uCommon C++ is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public License 00016 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. 00017 00027 #ifndef _UCOMMON_PLATFORM_H_ 00028 #define _UCOMMON_PLATFORM_H_ 00029 #define UCOMMON_ABI 4 00030 00042 #define UCOMMON_NAMESPACE ucc 00043 #define NAMESPACE_UCOMMON namespace ucc { 00044 #define END_NAMESPACE } 00045 00046 #ifndef _REENTRANT 00047 #define _REENTRANT 1 00048 #endif 00049 00050 #ifndef __PTH__ 00051 #ifndef _THREADSAFE 00052 #define _THREADSAFE 1 00053 #endif 00054 00055 #ifndef _POSIX_PTHREAD_SEMANTICS 00056 #define _POSIX_PTHREAD_SEMANTICS 00057 #endif 00058 #endif 00059 00060 #if defined(__GNUC__) && (__GNUC < 3) && !defined(_GNU_SOURCE) 00061 #define _GNU_SOURCE 00062 #endif 00063 00064 #if __GNUC__ > 3 || (__GNUC__ == 3 && (__GNU_MINOR__ > 3)) 00065 #define __PRINTF(x,y) __attribute__ ((format (printf, x, y))) 00066 #define __SCANF(x, y) __attribute__ ((format (scanf, x, y))) 00067 #define __MALLOC __attribute__ ((malloc)) 00068 #endif 00069 00070 #ifndef __MALLOC 00071 #define __PRINTF(x, y) 00072 #define __SCANF(x, y) 00073 #define __MALLOC 00074 #endif 00075 00076 #ifndef DEBUG 00077 #ifndef NDEBUG 00078 #define NDEBUG 00079 #endif 00080 #endif 00081 00082 #ifdef DEBUG 00083 #ifdef NDEBUG 00084 #undef NDEBUG 00085 #endif 00086 #endif 00087 00088 // see if we are building for or using extended stdc++ runtime library support 00089 00090 #if defined(NEW_STDCPP) || defined(OLD_STDCPP) 00091 #define _UCOMMON_EXTENDED_ 00092 #endif 00093 00094 // see if targeting legacy Microsoft windows platform 00095 00096 #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) 00097 #define _MSWINDOWS_ 00098 00099 //#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0501 00100 //#undef _WIN32_WINNT 00101 //#define _WIN32_WINNT 0x0501 00102 //#endif 00103 00104 //#ifndef _WIN32_WINNT 00105 //#define _WIN32_WINNT 0x0501 00106 //#endif 00107 00108 #pragma warning(disable: 4996) 00109 #pragma warning(disable: 4355) 00110 #pragma warning(disable: 4290) 00111 #pragma warning(disable: 4291) 00112 00113 #if defined(__BORLANDC__) && !defined(__MT__) 00114 #error Please enable multithreading 00115 #endif 00116 00117 #if defined(_MSC_VER) && !defined(_MT) 00118 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library) 00119 #endif 00120 00121 // Require for compiling with critical sections. 00122 #ifndef _WIN32_WINNT 00123 #define _WIN32_WINNT 0x0501 00124 #endif 00125 00126 // Make sure we're consistent with _WIN32_WINNT 00127 #ifndef WINVER 00128 #define WINVER _WIN32_WINNT 00129 #endif 00130 00131 #ifndef WIN32_LEAN_AND_MEAN 00132 #define WIN32_LEAN_AND_MEAN 00133 #endif 00134 00135 #include <winsock2.h> 00136 #include <ws2tcpip.h> 00137 00138 #if defined(_MSC_VER) 00139 typedef signed long ssize_t; 00140 typedef int pid_t; 00141 #endif 00142 00143 #include <process.h> 00144 #ifndef __EXPORT 00145 #ifdef UCOMMON_STATIC 00146 #define __EXPORT 00147 #else 00148 #define __EXPORT __declspec(dllimport) 00149 #endif 00150 #endif 00151 #define __LOCAL 00152 #elif UCOMMON_VISIBILITY > 0 00153 #define __EXPORT __attribute__ ((visibility("default"))) 00154 #define __LOCAL __attribute__ ((visibility("hidden"))) 00155 #else 00156 #define __EXPORT 00157 #define __LOCAL 00158 #endif 00159 00160 #ifdef _MSWINDOWS_ 00161 00162 #define _UWIN 00163 00164 #include <sys/stat.h> 00165 #include <io.h> 00166 00167 typedef DWORD pthread_t; 00168 typedef CRITICAL_SECTION pthread_mutex_t; 00169 typedef char *caddr_t; 00170 typedef HANDLE fd_t; 00171 typedef SOCKET socket_t; 00172 00173 typedef struct timespec { 00174 time_t tv_sec; 00175 long tv_nsec; 00176 } timespec_t; 00177 00178 extern "C" { 00179 00180 int cpr_setenv(const char *s, const char *v, int p); 00181 00182 inline int setenv(const char *s, const char *v, int overwrite) 00183 {return cpr_setenv(s, v, overwrite);}; 00184 00185 inline void sleep(int seconds) 00186 {::Sleep((seconds * 1000l));}; 00187 00188 inline void pthread_exit(void *p) 00189 {_endthreadex((DWORD)p);}; 00190 00191 inline pthread_t pthread_self(void) 00192 {return (pthread_t)GetCurrentThreadId();}; 00193 00194 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x) 00195 {InitializeCriticalSection(mutex); return 0;}; 00196 00197 inline void pthread_mutex_destroy(pthread_mutex_t *mutex) 00198 {DeleteCriticalSection(mutex);}; 00199 00200 inline void pthread_mutex_lock(pthread_mutex_t *mutex) 00201 {EnterCriticalSection(mutex);}; 00202 00203 inline void pthread_mutex_unlock(pthread_mutex_t *mutex) 00204 {LeaveCriticalSection(mutex);}; 00205 00206 inline char *strdup(const char *s) 00207 {return _strdup(s);}; 00208 00209 inline int stricmp(const char *s1, const char *s2) 00210 {return _stricmp(s1, s2);}; 00211 00212 inline int strnicmp(const char *s1, const char *s2, size_t l) 00213 {return _strnicmp(s1, s2, l);}; 00214 }; 00215 00216 #elif defined(__PTH__) 00217 00218 #include <pth.h> 00219 #include <sys/wait.h> 00220 00221 typedef int socket_t; 00222 typedef int fd_t; 00223 #define INVALID_SOCKET -1 00224 #define INVALID_HANDLE_VALUE -1 00225 #include <signal.h> 00226 00227 #define pthread_mutex_t pth_mutex_t 00228 #define pthread_cond_t pth_cond_t 00229 #define pthread_t pth_t 00230 00231 inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) 00232 {return pth_sigmask(how, set, oset);}; 00233 00234 inline void pthread_exit(void *p) 00235 {pth_exit(p);}; 00236 00237 inline void pthread_kill(pthread_t tid, int sig) 00238 {pth_raise(tid, sig);}; 00239 00240 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x) 00241 {return pth_mutex_init(mutex) != 0;}; 00242 00243 inline void pthread_mutex_destroy(pthread_mutex_t *mutex) 00244 {}; 00245 00246 inline void pthread_mutex_lock(pthread_mutex_t *mutex) 00247 {pth_mutex_acquire(mutex, 0, NULL);}; 00248 00249 inline void pthread_mutex_unlock(pthread_mutex_t *mutex) 00250 {pth_mutex_release(mutex);}; 00251 00252 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) 00253 {pth_cond_await(cond, mutex, NULL);}; 00254 00255 inline void pthread_cond_signal(pthread_cond_t *cond) 00256 {pth_cond_notify(cond, FALSE);}; 00257 00258 inline void pthread_cond_broadcast(pthread_cond_t *cond) 00259 {pth_cond_notify(cond, TRUE);}; 00260 00261 #else 00262 00263 #include <pthread.h> 00264 00265 typedef int socket_t; 00266 typedef int fd_t; 00267 #define INVALID_SOCKET -1 00268 #define INVALID_HANDLE_VALUE -1 00269 #include <signal.h> 00270 00271 #endif 00272 00273 #ifdef _MSC_VER 00274 typedef signed __int8 int8_t; 00275 typedef unsigned __int8 uint8_t; 00276 typedef signed __int16 int16_t; 00277 typedef unsigned __int16 uint16_t; 00278 typedef signed __int32 int32_t; 00279 typedef unsigned __int32 uint32_t; 00280 typedef signed __int64 int64_t; 00281 typedef unsigned __int64 uint64_t; 00282 typedef char *caddr_t; 00283 00284 #if defined(_MSC_VER) 00285 #include <stdio.h> 00286 #define snprintf _snprintf 00287 #define vsnprintf _vsnprintf 00288 #endif 00289 00290 #else 00291 00292 #include <sys/stat.h> 00293 #include <sys/types.h> 00294 #include <stdint.h> 00295 #include <unistd.h> 00296 00297 #endif 00298 00299 typedef void (*sighandler_t)(int); 00300 typedef unsigned long timeout_t; 00302 #include <stdlib.h> 00303 #include <errno.h> 00304 00305 #ifdef _MSWINDOWS_ 00306 #ifndef ENETDOWN 00307 #define ENETDOWN ((int)(WSAENETDOWN)) 00308 #endif 00309 #ifndef EINPROGRESS 00310 #define EINPROGRESS ((int)(WSAEINPROGRESS)) 00311 #endif 00312 #ifndef ENOPROTOOPT 00313 #define ENOPROTOOPT ((int)(WSAENOPROTOOPT)) 00314 #endif 00315 #ifndef EADDRINUSE 00316 #define EADDRINUSE ((int)(WSAEADDRINUSE)) 00317 #endif 00318 #ifndef EADDRNOTAVAIL 00319 #define EADDRNOTAVAIL ((int)(WSAEADDRNOTAVAIL)) 00320 #endif 00321 #ifndef ENETUNREACH 00322 #define ENETUNREACH ((int)(WSAENETUNREACH)) 00323 #endif 00324 #ifndef EHOSTUNREACH 00325 #define EHOSTUNREACH ((int)(WSAEHOSTUNREACH)) 00326 #endif 00327 #ifndef EHOSTDOWN 00328 #define EHOSTDOWN ((int)(WSAEHOSTDOWN)) 00329 #endif 00330 #ifndef ENETRESET 00331 #define ENETRESET ((int)(WSAENETRESET)) 00332 #endif 00333 #ifndef ECONNABORTED 00334 #define ECONNABORTED ((int)(WSAECONNABORTED)) 00335 #endif 00336 #ifndef ECONNRESET 00337 #define ECONNRESET ((int)(WSAECONNRESET)) 00338 #endif 00339 #ifndef EISCONN 00340 #define EISCONN ((int)(WSAEISCONN)) 00341 #endif 00342 #ifndef ENOTCONN 00343 #define ENOTCONN ((int)(WSAENOTCONN)) 00344 #endif 00345 #ifndef ESHUTDOWN 00346 #define ESHUTDOWN ((int)(WSAESHUTDOWN)) 00347 #endif 00348 #ifndef ETIMEDOUT 00349 #define ETIMEDOUT ((int)(WSAETIMEDOUT)) 00350 #endif 00351 #ifndef ECONNREFUSED 00352 #define ECONNREFUSED ((int)(WSAECONNREFUSED)) 00353 #endif 00354 #endif 00355 00362 __EXPORT void cpr_runtime_error(const char *text); 00363 00370 extern "C" __EXPORT void *cpr_memalloc(size_t size) __MALLOC; 00371 00381 extern "C" __EXPORT void *cpr_memassign(size_t size, caddr_t address, size_t known) __MALLOC; 00382 00389 extern "C" __EXPORT void cpr_memswap(void *mem1, void *mem2, size_t size); 00390 00391 #ifndef _UCOMMON_EXTENDED_ 00392 00397 inline void *operator new(size_t size) 00398 {return cpr_memalloc(size);}; 00399 00405 inline void *operator new[](size_t size) 00406 {return cpr_memalloc(size);}; 00407 #endif 00408 00409 #ifndef _UCOMMON_EXTENDED_ 00410 00418 inline void *operator new[](size_t size, caddr_t address) 00419 {return cpr_memassign(size, address, size);}; 00420 00430 inline void *operator new[](size_t size, caddr_t address, size_t known) 00431 {return cpr_memassign(size, address, known);}; 00432 #endif 00433 00443 inline void *operator new(size_t size, size_t extra) 00444 {return cpr_memalloc(size + extra);} 00445 00454 inline void *operator new(size_t size, caddr_t address) 00455 {return cpr_memassign(size, address, size);} 00456 00467 inline void *operator new(size_t size, caddr_t address, size_t known) 00468 {return cpr_memassign(size, address, known);} 00469 00470 #ifndef _UCOMMON_EXTENDED_ 00471 00475 inline void operator delete(void *object) 00476 {free(object);} 00477 00482 inline void operator delete[](void *array) 00483 {free(array);} 00484 00485 #ifdef __GNUC__ 00486 extern "C" __EXPORT void __cxa_pure_virtual(void); 00487 #endif 00488 #endif 00489 00490 #ifndef DEBUG 00491 #ifndef NDEBUG 00492 #define NDEBUG 00493 #endif 00494 #endif 00495 00496 #ifdef DEBUG 00497 #ifdef NDEBUG 00498 #undef NDEBUG 00499 #endif 00500 #endif 00501 00502 #include <assert.h> 00503 #ifdef DEBUG 00504 #define crit(x, text) assert(x) 00505 #else 00506 #define crit(x, text) if(!(x)) cpr_runtime_error(text) 00507 #endif 00508 00515 template<class T> 00516 inline T *init(T *memory) 00517 {return ((memory) ? new(((caddr_t)memory)) T : NULL);} 00518 00519 extern "C" { 00520 00521 __EXPORT uint16_t lsb_getshort(uint8_t *b); 00522 __EXPORT uint32_t lsb_getlong(uint8_t *b); 00523 __EXPORT uint16_t msb_getshort(uint8_t *b); 00524 __EXPORT uint32_t msb_getlong(uint8_t *b); 00525 00526 __EXPORT void lsb_setshort(uint8_t *b, uint16_t v); 00527 __EXPORT void lsb_setlong(uint8_t *b, uint32_t v); 00528 __EXPORT void msb_setshort(uint8_t *b, uint16_t v); 00529 __EXPORT void msb_setlong(uint8_t *b, uint32_t v); 00530 00531 } 00532 00533 typedef long Integer; 00534 typedef unsigned long Unsigned; 00535 typedef double Real; 00536 00537 #endif