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 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 
00100 //#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0501
00101 //#undef    _WIN32_WINNT
00102 //#define   _WIN32_WINNT 0x0501
00103 //#endif
00104 
00105 //#ifndef _WIN32_WINNT
00106 //#define   _WIN32_WINNT 0x0501
00107 //#endif
00108 
00109 #pragma warning(disable: 4251)
00110 #pragma warning(disable: 4996)
00111 #pragma warning(disable: 4355)
00112 #pragma warning(disable: 4290)
00113 #pragma warning(disable: 4291)
00114 
00115 #if defined(__BORLANDC__) && !defined(__MT__)
00116 #error Please enable multithreading
00117 #endif
00118 
00119 #if defined(_MSC_VER) && !defined(_MT)
00120 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
00121 #endif
00122 
00123 // Require for compiling with critical sections.
00124 #ifndef _WIN32_WINNT
00125 #define _WIN32_WINNT 0x0501
00126 #endif
00127 
00128 // Make sure we're consistent with _WIN32_WINNT
00129 #ifndef WINVER
00130 #define WINVER _WIN32_WINNT
00131 #endif
00132 
00133 #ifndef WIN32_LEAN_AND_MEAN
00134 #define WIN32_LEAN_AND_MEAN
00135 #endif
00136 
00137 #include <winsock2.h>
00138 #include <ws2tcpip.h>
00139 
00140 #if defined(_MSC_VER)
00141 typedef signed long ssize_t;
00142 typedef int pid_t;
00143 #endif
00144 
00145 #include <process.h>
00146 #ifndef __EXPORT
00147 #ifdef  UCOMMON_STATIC
00148 #define __EXPORT
00149 #else
00150 #define __EXPORT    __declspec(dllimport)
00151 #endif
00152 #endif
00153 #define __LOCAL
00154 #elif UCOMMON_VISIBILITY > 0
00155 #define __EXPORT    __attribute__ ((visibility("default")))
00156 #define __LOCAL     __attribute__ ((visibility("hidden")))
00157 #else
00158 #define __EXPORT
00159 #define __LOCAL
00160 #endif
00161 
00162 #ifdef  _MSWINDOWS_
00163 
00164 #define _UWIN
00165 
00166 #include <sys/stat.h>
00167 #include <io.h>
00168 
00169 typedef DWORD pthread_t;
00170 typedef CRITICAL_SECTION pthread_mutex_t;
00171 typedef char *caddr_t;
00172 typedef HANDLE fd_t;
00173 typedef SOCKET socket_t;
00174 
00175 typedef struct timespec {
00176     time_t tv_sec;
00177     long  tv_nsec;
00178 } timespec_t;
00179 
00180 extern "C" {
00181 
00182     #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
00183 
00184     typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
00185 
00186     __EXPORT int cpr_setenv(const char *s, const char *v, int p);
00187 
00188     inline int setenv(const char *s, const char *v, int overwrite)
00189         {return cpr_setenv(s, v, overwrite);};
00190 
00191     inline void sleep(int seconds)
00192         {::Sleep((seconds * 1000l));};
00193 
00194     inline void pthread_exit(void *p)
00195         {_endthreadex((DWORD)p);};
00196 
00197     inline pthread_t pthread_self(void)
00198         {return (pthread_t)GetCurrentThreadId();};
00199 
00200     inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00201         {InitializeCriticalSection(mutex); return 0;};
00202 
00203     inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00204         {DeleteCriticalSection(mutex);};
00205 
00206     inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00207         {EnterCriticalSection(mutex);};
00208 
00209     inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00210         {LeaveCriticalSection(mutex);};
00211 
00212     inline char *strdup(const char *s)
00213         {return _strdup(s);};
00214 
00215     inline int stricmp(const char *s1, const char *s2)
00216         {return _stricmp(s1, s2);};
00217 
00218     inline int strnicmp(const char *s1, const char *s2, size_t l)
00219         {return _strnicmp(s1, s2, l);};
00220 };
00221 
00222 #elif defined(__PTH__)
00223 
00224 #include <pth.h>
00225 #include <sys/wait.h>
00226 
00227 typedef int socket_t;
00228 typedef int fd_t;
00229 #define INVALID_SOCKET -1
00230 #define INVALID_HANDLE_VALUE -1
00231 #include <signal.h>
00232 
00233 #define pthread_mutex_t pth_mutex_t
00234 #define pthread_cond_t pth_cond_t
00235 #define pthread_t pth_t
00236 
00237 inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
00238     {return pth_sigmask(how, set, oset);};
00239 
00240 inline void pthread_exit(void *p)
00241     {pth_exit(p);};
00242 
00243 inline void pthread_kill(pthread_t tid, int sig)
00244     {pth_raise(tid, sig);};
00245 
00246 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00247     {return pth_mutex_init(mutex) != 0;};
00248 
00249 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00250     {};
00251 
00252 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00253     {pth_mutex_acquire(mutex, 0, NULL);};
00254 
00255 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00256     {pth_mutex_release(mutex);};
00257 
00258 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
00259     {pth_cond_await(cond, mutex, NULL);};
00260 
00261 inline void pthread_cond_signal(pthread_cond_t *cond)
00262     {pth_cond_notify(cond, FALSE);};
00263 
00264 inline void pthread_cond_broadcast(pthread_cond_t *cond)
00265     {pth_cond_notify(cond, TRUE);};
00266 
00267 #else
00268 
00269 #include <pthread.h>
00270 
00271 typedef int socket_t;
00272 typedef int fd_t;
00273 #define INVALID_SOCKET -1
00274 #define INVALID_HANDLE_VALUE -1
00275 #include <signal.h>
00276 
00277 #endif
00278 
00279 #ifdef _MSC_VER
00280 typedef signed __int8 int8_t;
00281 typedef unsigned __int8 uint8_t;
00282 typedef signed __int16 int16_t;
00283 typedef unsigned __int16 uint16_t;
00284 typedef signed __int32 int32_t;
00285 typedef unsigned __int32 uint32_t;
00286 typedef signed __int64 int64_t;
00287 typedef unsigned __int64 uint64_t;
00288 typedef char *caddr_t;
00289 
00290 #include <stdio.h>
00291 #define snprintf _snprintf
00292 #define vsnprintf _vsnprintf
00293 
00294 #else
00295 
00296 #include <sys/stat.h>
00297 #include <sys/types.h>
00298 #include <stdint.h>
00299 #include <unistd.h>
00300 
00301 #endif
00302 
00303 #ifndef _GNU_SOURCE
00304 typedef void (*sighandler_t)(int);  
00305 #endif
00306 typedef unsigned long timeout_t;    
00308 #include <stdlib.h>
00309 #include <errno.h>
00310 
00311 #ifdef  _MSWINDOWS_
00312 #ifndef ENETDOWN
00313 #define ENETDOWN        ((int)(WSAENETDOWN))
00314 #endif
00315 #ifndef EINPROGRESS
00316 #define EINPROGRESS     ((int)(WSAEINPROGRESS))
00317 #endif
00318 #ifndef ENOPROTOOPT
00319 #define ENOPROTOOPT     ((int)(WSAENOPROTOOPT))
00320 #endif
00321 #ifndef EADDRINUSE
00322 #define EADDRINUSE      ((int)(WSAEADDRINUSE))
00323 #endif
00324 #ifndef EADDRNOTAVAIL
00325 #define EADDRNOTAVAIL   ((int)(WSAEADDRNOTAVAIL))
00326 #endif
00327 #ifndef ENETUNREACH
00328 #define ENETUNREACH     ((int)(WSAENETUNREACH))
00329 #endif
00330 #ifndef EHOSTUNREACH
00331 #define EHOSTUNREACH    ((int)(WSAEHOSTUNREACH))
00332 #endif
00333 #ifndef EHOSTDOWN
00334 #define EHOSTDOWN       ((int)(WSAEHOSTDOWN))
00335 #endif
00336 #ifndef ENETRESET
00337 #define ENETRESET       ((int)(WSAENETRESET))
00338 #endif
00339 #ifndef ECONNABORTED
00340 #define ECONNABORTED    ((int)(WSAECONNABORTED))
00341 #endif
00342 #ifndef ECONNRESET
00343 #define ECONNRESET      ((int)(WSAECONNRESET))
00344 #endif
00345 #ifndef EISCONN
00346 #define EISCONN         ((int)(WSAEISCONN))
00347 #endif
00348 #ifndef ENOTCONN
00349 #define ENOTCONN        ((int)(WSAENOTCONN))
00350 #endif
00351 #ifndef ESHUTDOWN
00352 #define ESHUTDOWN       ((int)(WSAESHUTDOWN))
00353 #endif
00354 #ifndef ETIMEDOUT
00355 #define ETIMEDOUT       ((int)(WSAETIMEDOUT))
00356 #endif
00357 #ifndef ECONNREFUSED
00358 #define ECONNREFUSED    ((int)(WSAECONNREFUSED))
00359 #endif
00360 #endif
00361 
00368 __EXPORT void cpr_runtime_error(const char *text);
00369 
00376 extern "C" __EXPORT void *cpr_memalloc(size_t size) __MALLOC;
00377 
00387 extern "C" __EXPORT void *cpr_memassign(size_t size, caddr_t address, size_t known) __MALLOC;
00388 
00395 extern "C" __EXPORT void cpr_memswap(void *mem1, void *mem2, size_t size);
00396 
00397 #ifndef _UCOMMON_EXTENDED_
00398 
00403 inline void *operator new(size_t size)
00404     {return cpr_memalloc(size);}
00405 
00411 inline void *operator new[](size_t size)
00412     {return cpr_memalloc(size);}
00413 #endif
00414 
00415 #ifndef _UCOMMON_EXTENDED_
00416 
00424 inline void *operator new[](size_t size, caddr_t address)
00425     {return cpr_memassign(size, address, size);}
00426 
00436 inline void *operator new[](size_t size, caddr_t address, size_t known)
00437     {return cpr_memassign(size, address, known);}
00438 #endif
00439 
00449 inline void *operator new(size_t size, size_t extra)
00450     {return cpr_memalloc(size + extra);}
00451 
00460 inline void *operator new(size_t size, caddr_t address)
00461     {return cpr_memassign(size, address, size);}
00462 
00473 inline void *operator new(size_t size, caddr_t address, size_t known)
00474     {return cpr_memassign(size, address, known);}
00475 
00476 #ifndef _UCOMMON_EXTENDED_
00477 
00481 inline void operator delete(void *object)
00482     {free(object);}
00483 
00488 inline void operator delete[](void *array)
00489     {free(array);}
00490 
00491 #ifdef  __GNUC__
00492 extern "C" __EXPORT void __cxa_pure_virtual(void);
00493 #endif
00494 #endif
00495 
00496 #ifndef DEBUG
00497 #ifndef NDEBUG
00498 #define NDEBUG
00499 #endif
00500 #endif
00501 
00502 #ifdef  DEBUG
00503 #ifdef  NDEBUG
00504 #undef  NDEBUG
00505 #endif
00506 #endif
00507 
00508 #ifndef PROGRAM_MAIN
00509 #define PROGRAM_MAIN(argc, argv)    extern "C" int main(int argc, char **argv)
00510 #define PROGRAM_EXIT(code)          return code
00511 #endif
00512 
00513 #ifndef SERVICE_MAIN
00514 #define SERVICE_MAIN(id, argc, argv)    void service_##id(int argc, char **argv)
00515 typedef void (*cpr_service_t)(int argc, char **argv);
00516 #endif
00517 
00518 #include <assert.h>
00519 #ifdef  DEBUG
00520 #define crit(x, text)   assert(x)
00521 #else
00522 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
00523 #endif
00524 
00531 template<class T>
00532 inline T *init(T *memory)
00533     {return ((memory) ? new(((caddr_t)memory)) T : NULL);}
00534 
00535 extern "C" {
00536 
00537     __EXPORT uint16_t lsb_getshort(uint8_t *b);
00538     __EXPORT uint32_t lsb_getlong(uint8_t *b);
00539     __EXPORT uint16_t msb_getshort(uint8_t *b);
00540     __EXPORT uint32_t msb_getlong(uint8_t *b);
00541 
00542     __EXPORT void lsb_setshort(uint8_t *b, uint16_t v);
00543     __EXPORT void lsb_setlong(uint8_t *b, uint32_t v);
00544     __EXPORT void msb_setshort(uint8_t *b, uint16_t v);
00545     __EXPORT void msb_setlong(uint8_t *b, uint32_t v);
00546 
00547 }
00548 
00549 typedef long Integer;
00550 typedef unsigned long Unsigned;
00551 typedef double Real;
00552 
00553 #endif