UCommon
ucommon/platform.h
Go to the documentation of this file.
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 5
00030 
00041 #define UCOMMON_NAMESPACE   ucommon
00042 #define NAMESPACE_UCOMMON   namespace ucommon {
00043 #define END_NAMESPACE       }
00044 
00045 #ifndef _REENTRANT
00046 #define _REENTRANT 1
00047 #endif
00048 
00049 #ifndef __PTH__
00050 #ifndef _THREADSAFE
00051 #define _THREADSAFE 1
00052 #endif
00053 
00054 #ifndef _POSIX_PTHREAD_SEMANTICS
00055 #define _POSIX_PTHREAD_SEMANTICS
00056 #endif
00057 #endif
00058 
00059 #if defined(__GNUC__) && (__GNUC < 3) && !defined(_GNU_SOURCE)
00060 #define _GNU_SOURCE
00061 #endif
00062 
00063 #if __GNUC__ > 3 || (__GNUC__ == 3 && (__GNU_MINOR__ > 3))
00064 #define __PRINTF(x,y)   __attribute__ ((format (printf, x, y)))
00065 #define __SCANF(x, y) __attribute__ ((format (scanf, x, y)))
00066 #define __MALLOC      __attribute__ ((malloc))
00067 #endif
00068 
00069 #ifndef __MALLOC
00070 #define __PRINTF(x, y)
00071 #define __SCANF(x, y)
00072 #define __MALLOC
00073 #endif
00074 
00075 #ifndef DEBUG
00076 #ifndef NDEBUG
00077 #define NDEBUG
00078 #endif
00079 #endif
00080 
00081 #ifdef  DEBUG
00082 #ifdef  NDEBUG
00083 #undef  NDEBUG
00084 #endif
00085 #endif
00086 
00087 // see if we are building for or using extended stdc++ runtime library support
00088 
00089 #if defined(NEW_STDCPP) || defined(OLD_STDCPP)
00090 #define _UCOMMON_EXTENDED_
00091 #endif
00092 
00093 // see if targeting legacy Microsoft windows platform
00094 
00095 #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
00096 #define _MSWINDOWS_
00097 
00098 #if defined(_M_X64) || defined(_M_ARM)
00099 #define _MSCONDITIONALS_
00100 #ifndef _WIN32_WINNT    0x0600
00101 #define _WIN32_WINNT
00102 #endif
00103 #endif
00104 
00105 //#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0501
00106 //#undef    _WIN32_WINNT
00107 //#define   _WIN32_WINNT 0x0501
00108 //#endif
00109 
00110 //#ifndef _WIN32_WINNT
00111 //#define   _WIN32_WINNT 0x0501
00112 //#endif
00113 
00114 #pragma warning(disable: 4251)
00115 #pragma warning(disable: 4996)
00116 #pragma warning(disable: 4355)
00117 #pragma warning(disable: 4290)
00118 #pragma warning(disable: 4291)
00119 
00120 #if defined(__BORLANDC__) && !defined(__MT__)
00121 #error Please enable multithreading
00122 #endif
00123 
00124 #if defined(_MSC_VER) && !defined(_MT)
00125 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
00126 #endif
00127 
00128 // Require for compiling with critical sections.
00129 #ifndef _WIN32_WINNT
00130 #define _WIN32_WINNT 0x0501
00131 #endif
00132 
00133 // Make sure we're consistent with _WIN32_WINNT
00134 #ifndef WINVER
00135 #define WINVER _WIN32_WINNT
00136 #endif
00137 
00138 #ifndef WIN32_LEAN_AND_MEAN
00139 #define WIN32_LEAN_AND_MEAN
00140 #endif
00141 
00142 #include <winsock2.h>
00143 #include <ws2tcpip.h>
00144 
00145 #if defined(_MSC_VER)
00146 typedef signed long ssize_t;
00147 typedef int pid_t;
00148 #endif
00149 
00150 #include <process.h>
00151 #ifndef __EXPORT
00152 #ifdef  UCOMMON_STATIC
00153 #define __EXPORT
00154 #else
00155 #define __EXPORT    __declspec(dllimport)
00156 #endif
00157 #endif
00158 #define __LOCAL
00159 #elif UCOMMON_VISIBILITY > 0
00160 #define __EXPORT    __attribute__ ((visibility("default")))
00161 #define __LOCAL     __attribute__ ((visibility("hidden")))
00162 #else
00163 #define __EXPORT
00164 #define __LOCAL
00165 #endif
00166 
00167 #ifdef  _MSWINDOWS_
00168 
00169 #define _UWIN
00170 
00171 #include <sys/stat.h>
00172 #include <io.h>
00173 
00174 typedef DWORD pthread_t;
00175 typedef CRITICAL_SECTION pthread_mutex_t;
00176 typedef char *caddr_t;
00177 typedef HANDLE fd_t;
00178 typedef SOCKET socket_t;
00179 
00180 typedef struct timespec {
00181     time_t tv_sec;
00182     long  tv_nsec;
00183 } timespec_t;
00184 
00185 extern "C" {
00186 
00187     #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
00188 
00189     typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
00190 
00191     __EXPORT int cpr_setenv(const char *s, const char *v, int p);
00192 
00193     inline int setenv(const char *s, const char *v, int overwrite)
00194         {return cpr_setenv(s, v, overwrite);};
00195 
00196     inline void sleep(int seconds)
00197         {::Sleep((seconds * 1000l));};
00198 
00199     inline void pthread_exit(void *p)
00200         {_endthreadex((DWORD)p);};
00201 
00202     inline pthread_t pthread_self(void)
00203         {return (pthread_t)GetCurrentThreadId();};
00204 
00205     inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00206         {InitializeCriticalSection(mutex); return 0;};
00207 
00208     inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00209         {DeleteCriticalSection(mutex);};
00210 
00211     inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00212         {EnterCriticalSection(mutex);};
00213 
00214     inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00215         {LeaveCriticalSection(mutex);};
00216 
00217     inline char *strdup(const char *s)
00218         {return _strdup(s);};
00219 
00220     inline int stricmp(const char *s1, const char *s2)
00221         {return _stricmp(s1, s2);};
00222 
00223     inline int strnicmp(const char *s1, const char *s2, size_t l)
00224         {return _strnicmp(s1, s2, l);};
00225 };
00226 
00227 #elif defined(__PTH__)
00228 
00229 #include <pth.h>
00230 #include <sys/wait.h>
00231 
00232 typedef int socket_t;
00233 typedef int fd_t;
00234 #define INVALID_SOCKET -1
00235 #define INVALID_HANDLE_VALUE -1
00236 #include <signal.h>
00237 
00238 #define pthread_mutex_t pth_mutex_t
00239 #define pthread_cond_t pth_cond_t
00240 #define pthread_t pth_t
00241 
00242 inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
00243     {return pth_sigmask(how, set, oset);};
00244 
00245 inline void pthread_exit(void *p)
00246     {pth_exit(p);};
00247 
00248 inline void pthread_kill(pthread_t tid, int sig)
00249     {pth_raise(tid, sig);};
00250 
00251 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00252     {return pth_mutex_init(mutex) != 0;};
00253 
00254 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00255     {};
00256 
00257 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00258     {pth_mutex_acquire(mutex, 0, NULL);};
00259 
00260 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00261     {pth_mutex_release(mutex);};
00262 
00263 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
00264     {pth_cond_await(cond, mutex, NULL);};
00265 
00266 inline void pthread_cond_signal(pthread_cond_t *cond)
00267     {pth_cond_notify(cond, FALSE);};
00268 
00269 inline void pthread_cond_broadcast(pthread_cond_t *cond)
00270     {pth_cond_notify(cond, TRUE);};
00271 
00272 #else
00273 
00274 #include <pthread.h>
00275 
00276 typedef int socket_t;
00277 typedef int fd_t;
00278 #define INVALID_SOCKET -1
00279 #define INVALID_HANDLE_VALUE -1
00280 #include <signal.h>
00281 
00282 #endif
00283 
00284 #ifdef _MSC_VER
00285 typedef signed __int8 int8_t;
00286 typedef unsigned __int8 uint8_t;
00287 typedef signed __int16 int16_t;
00288 typedef unsigned __int16 uint16_t;
00289 typedef signed __int32 int32_t;
00290 typedef unsigned __int32 uint32_t;
00291 typedef signed __int64 int64_t;
00292 typedef unsigned __int64 uint64_t;
00293 typedef char *caddr_t;
00294 
00295 #include <stdio.h>
00296 #define snprintf _snprintf
00297 #define vsnprintf _vsnprintf
00298 
00299 #else
00300 
00301 #include <sys/stat.h>
00302 #include <sys/types.h>
00303 #include <stdint.h>
00304 #include <unistd.h>
00305 
00306 #endif
00307 
00308 #ifndef _GNU_SOURCE
00309 typedef void (*sighandler_t)(int);  
00310 #endif
00311 typedef unsigned long timeout_t;    
00313 #include <stdlib.h>
00314 #include <errno.h>
00315 
00316 #ifdef  _MSWINDOWS_
00317 #ifndef ENETDOWN
00318 #define ENETDOWN        ((int)(WSAENETDOWN))
00319 #endif
00320 #ifndef EINPROGRESS
00321 #define EINPROGRESS     ((int)(WSAEINPROGRESS))
00322 #endif
00323 #ifndef ENOPROTOOPT
00324 #define ENOPROTOOPT     ((int)(WSAENOPROTOOPT))
00325 #endif
00326 #ifndef EADDRINUSE
00327 #define EADDRINUSE      ((int)(WSAEADDRINUSE))
00328 #endif
00329 #ifndef EADDRNOTAVAIL
00330 #define EADDRNOTAVAIL   ((int)(WSAEADDRNOTAVAIL))
00331 #endif
00332 #ifndef ENETUNREACH
00333 #define ENETUNREACH     ((int)(WSAENETUNREACH))
00334 #endif
00335 #ifndef EHOSTUNREACH
00336 #define EHOSTUNREACH    ((int)(WSAEHOSTUNREACH))
00337 #endif
00338 #ifndef EHOSTDOWN
00339 #define EHOSTDOWN       ((int)(WSAEHOSTDOWN))
00340 #endif
00341 #ifndef ENETRESET
00342 #define ENETRESET       ((int)(WSAENETRESET))
00343 #endif
00344 #ifndef ECONNABORTED
00345 #define ECONNABORTED    ((int)(WSAECONNABORTED))
00346 #endif
00347 #ifndef ECONNRESET
00348 #define ECONNRESET      ((int)(WSAECONNRESET))
00349 #endif
00350 #ifndef EISCONN
00351 #define EISCONN         ((int)(WSAEISCONN))
00352 #endif
00353 #ifndef ENOTCONN
00354 #define ENOTCONN        ((int)(WSAENOTCONN))
00355 #endif
00356 #ifndef ESHUTDOWN
00357 #define ESHUTDOWN       ((int)(WSAESHUTDOWN))
00358 #endif
00359 #ifndef ETIMEDOUT
00360 #define ETIMEDOUT       ((int)(WSAETIMEDOUT))
00361 #endif
00362 #ifndef ECONNREFUSED
00363 #define ECONNREFUSED    ((int)(WSAECONNREFUSED))
00364 #endif
00365 #endif
00366 
00373 __EXPORT void cpr_runtime_error(const char *text);
00374 
00381 extern "C" __EXPORT void *cpr_memalloc(size_t size) __MALLOC;
00382 
00392 extern "C" __EXPORT void *cpr_memassign(size_t size, caddr_t address, size_t known) __MALLOC;
00393 
00400 extern "C" __EXPORT void cpr_memswap(void *mem1, void *mem2, size_t size);
00401 
00402 #ifndef _UCOMMON_EXTENDED_
00403 
00408 inline void *operator new(size_t size)
00409     {return cpr_memalloc(size);}
00410 
00416 inline void *operator new[](size_t size)
00417     {return cpr_memalloc(size);}
00418 #endif
00419 
00420 #ifndef _UCOMMON_EXTENDED_
00421 
00429 inline void *operator new[](size_t size, caddr_t address)
00430     {return cpr_memassign(size, address, size);}
00431 
00441 inline void *operator new[](size_t size, caddr_t address, size_t known)
00442     {return cpr_memassign(size, address, known);}
00443 #endif
00444 
00454 inline void *operator new(size_t size, size_t extra)
00455     {return cpr_memalloc(size + extra);}
00456 
00465 inline void *operator new(size_t size, caddr_t address)
00466     {return cpr_memassign(size, address, size);}
00467 
00478 inline void *operator new(size_t size, caddr_t address, size_t known)
00479     {return cpr_memassign(size, address, known);}
00480 
00481 #ifndef _UCOMMON_EXTENDED_
00482 
00486 inline void operator delete(void *object)
00487     {free(object);}
00488 
00493 inline void operator delete[](void *array)
00494     {free(array);}
00495 
00496 #ifdef  __GNUC__
00497 extern "C" __EXPORT void __cxa_pure_virtual(void);
00498 #endif
00499 #endif
00500 
00501 #ifndef DEBUG
00502 #ifndef NDEBUG
00503 #define NDEBUG
00504 #endif
00505 #endif
00506 
00507 #ifdef  DEBUG
00508 #ifdef  NDEBUG
00509 #undef  NDEBUG
00510 #endif
00511 #endif
00512 
00513 #ifndef PROGRAM_MAIN
00514 #define PROGRAM_MAIN(argc, argv)    extern "C" int main(int argc, char **argv)
00515 #define PROGRAM_EXIT(code)          return code
00516 #endif
00517 
00518 #ifndef SERVICE_MAIN
00519 #define SERVICE_MAIN(id, argc, argv)    void service_##id(int argc, char **argv)
00520 typedef void (*cpr_service_t)(int argc, char **argv);
00521 #endif
00522 
00523 #include <assert.h>
00524 #ifdef  DEBUG
00525 #define crit(x, text)   assert(x)
00526 #else
00527 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
00528 #endif
00529 
00536 template<class T>
00537 inline T *init(T *memory)
00538     {return ((memory) ? new(((caddr_t)memory)) T : NULL);}
00539 
00540 extern "C" {
00541 
00542     __EXPORT uint16_t lsb_getshort(uint8_t *b);
00543     __EXPORT uint32_t lsb_getlong(uint8_t *b);
00544     __EXPORT uint16_t msb_getshort(uint8_t *b);
00545     __EXPORT uint32_t msb_getlong(uint8_t *b);
00546 
00547     __EXPORT void lsb_setshort(uint8_t *b, uint16_t v);
00548     __EXPORT void lsb_setlong(uint8_t *b, uint32_t v);
00549     __EXPORT void msb_setshort(uint8_t *b, uint16_t v);
00550     __EXPORT void msb_setlong(uint8_t *b, uint32_t v);
00551 
00552 }
00553 
00554 typedef long Integer;
00555 typedef unsigned long Unsigned;
00556 typedef double Real;
00557 
00558 #endif