ucommon
ucommon/platform.h
Go to the documentation of this file.
00001 // Copyright (C) 2006-2014 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 6
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(__ANDROID__) || 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(_MSC_VER)
00099 #define NOMINMAX
00100 #endif
00101 
00102 #if defined(_M_X64) || defined(_M_ARM)
00103 #define _MSCONDITIONALS_
00104 #ifndef _WIN32_WINNT
00105 #define _WIN32_WINNT    0x0600
00106 #endif
00107 #endif
00108 
00109 //#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0501
00110 //#undef    _WIN32_WINNT
00111 //#define   _WIN32_WINNT 0x0501
00112 //#endif
00113 
00114 //#ifndef _WIN32_WINNT
00115 //#define   _WIN32_WINNT 0x0501
00116 //#endif
00117 
00118 #ifdef  _MSC_VER
00119 #pragma warning(disable: 4251)
00120 #pragma warning(disable: 4996)
00121 #pragma warning(disable: 4355)
00122 #pragma warning(disable: 4290)
00123 #pragma warning(disable: 4291)
00124 #endif
00125 
00126 #if defined(__BORLANDC__) && !defined(__MT__)
00127 #error Please enable multithreading
00128 #endif
00129 
00130 #if defined(_MSC_VER) && !defined(_MT)
00131 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
00132 #endif
00133 
00134 // Require for compiling with critical sections.
00135 #ifndef _WIN32_WINNT
00136 #define _WIN32_WINNT 0x0501
00137 #endif
00138 
00139 // Make sure we're consistent with _WIN32_WINNT
00140 #ifndef WINVER
00141 #define WINVER _WIN32_WINNT
00142 #endif
00143 
00144 #ifndef WIN32_LEAN_AND_MEAN
00145 #define WIN32_LEAN_AND_MEAN
00146 #endif
00147 
00148 #include <winsock2.h>
00149 #include <ws2tcpip.h>
00150 
00151 #if defined(_MSC_VER)
00152 typedef signed long ssize_t;
00153 typedef int pid_t;
00154 #endif
00155 
00156 #include <process.h>
00157 #ifndef __EXPORT
00158 #ifdef  UCOMMON_STATIC
00159 #define __EXPORT
00160 #else
00161 #define __EXPORT    __declspec(dllimport)
00162 #endif
00163 #endif
00164 #define __LOCAL
00165 
00166 // if runtime mode then non-runtime libraries are static on windows...
00167 #if defined(UCOMMON_RUNTIME) || defined(UCOMMON_STATIC)
00168 #define __SHARED
00169 #else
00170 #define __SHARED __EXPORT
00171 #endif
00172 
00173 #elif UCOMMON_VISIBILITY > 0
00174 #define __EXPORT    __attribute__ ((visibility("default")))
00175 #define __LOCAL     __attribute__ ((visibility("hidden")))
00176 #define __SHARED    __attribute__ ((visibility("default")))
00177 #else
00178 #define __EXPORT
00179 #define __LOCAL
00180 #define __SHARED
00181 #endif
00182 
00183 #ifdef  _MSWINDOWS_
00184 
00185 #define _UWIN
00186 
00187 #include <sys/stat.h>
00188 #include <io.h>
00189 
00190 // gcc c++11 support on mingw requires pthread support library
00191 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 7)) && defined(_UCOMMON_EXTENDED_) && defined(__MINGW_WINPTHREAD__)
00192 #include <pthread.h>   // gnu libstdc++ now requires a win pthread
00193 #ifdef OLD_STDCPP
00194 #undef OLD_STDCPP
00195 #define NEW_STDCPP
00196 #endif
00197 #undef  _MSCONDITIONALS_
00198 #else   
00199 #define _MSTHREADS_
00200 typedef DWORD pthread_t;
00201 typedef CRITICAL_SECTION pthread_mutex_t;
00202 #endif
00203 typedef char *caddr_t;
00204 typedef HANDLE fd_t;
00205 typedef SOCKET socket_t;
00206 
00207 #ifdef  _MSC_VER
00208 typedef struct timespec {
00209     time_t tv_sec;
00210     long  tv_nsec;
00211 } timespec_t;
00212 #endif
00213 
00214 inline void sleep(int seconds)
00215     {::Sleep((seconds * 1000l));}
00216 
00217 extern "C" {
00218 
00219     #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
00220 
00221     typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
00222 
00223 #ifdef _MSTHREADS_
00224     inline void pthread_exit(void *p)
00225         {_endthreadex((DWORD)0);}
00226 
00227     inline pthread_t pthread_self(void)
00228         {return (pthread_t)GetCurrentThreadId();}
00229 
00230     inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00231         {InitializeCriticalSection(mutex); return 0;}
00232 
00233     inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00234         {DeleteCriticalSection(mutex);}
00235 
00236     inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00237         {EnterCriticalSection(mutex);}
00238 
00239     inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00240         {LeaveCriticalSection(mutex);}
00241 #endif
00242 
00243     inline char *strdup(const char *s)
00244         {return _strdup(s);}
00245 
00246     inline int stricmp(const char *s1, const char *s2)
00247         {return _stricmp(s1, s2);}
00248 
00249     inline int strnicmp(const char *s1, const char *s2, size_t l)
00250         {return _strnicmp(s1, s2, l);}
00251 }
00252 
00253 #elif defined(__PTH__)
00254 
00255 #include <pth.h>
00256 #include <sys/wait.h>
00257 
00258 typedef int socket_t;
00259 typedef int fd_t;
00260 #define INVALID_SOCKET -1
00261 #define INVALID_HANDLE_VALUE -1
00262 #include <signal.h>
00263 
00264 #define pthread_mutex_t pth_mutex_t
00265 #define pthread_cond_t pth_cond_t
00266 #define pthread_t pth_t
00267 
00268 inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
00269     {return pth_sigmask(how, set, oset);};
00270 
00271 inline void pthread_exit(void *p)
00272     {pth_exit(p);};
00273 
00274 inline void pthread_kill(pthread_t tid, int sig)
00275     {pth_raise(tid, sig);};
00276 
00277 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00278     {return pth_mutex_init(mutex) != 0;};
00279 
00280 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00281     {};
00282 
00283 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00284     {pth_mutex_acquire(mutex, 0, NULL);};
00285 
00286 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00287     {pth_mutex_release(mutex);};
00288 
00289 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
00290     {pth_cond_await(cond, mutex, NULL);};
00291 
00292 inline void pthread_cond_signal(pthread_cond_t *cond)
00293     {pth_cond_notify(cond, FALSE);};
00294 
00295 inline void pthread_cond_broadcast(pthread_cond_t *cond)
00296     {pth_cond_notify(cond, TRUE);};
00297 
00298 #else
00299 
00300 #include <pthread.h>
00301 
00302 typedef int socket_t;
00303 typedef int fd_t;
00304 #define INVALID_SOCKET -1
00305 #define INVALID_HANDLE_VALUE -1
00306 #include <signal.h>
00307 
00308 #endif
00309 
00310 #ifdef _MSC_VER
00311 typedef signed __int8 int8_t;
00312 typedef unsigned __int8 uint8_t;
00313 typedef signed __int16 int16_t;
00314 typedef unsigned __int16 uint16_t;
00315 typedef signed __int32 int32_t;
00316 typedef unsigned __int32 uint32_t;
00317 typedef signed __int64 int64_t;
00318 typedef unsigned __int64 uint64_t;
00319 typedef char *caddr_t;
00320 
00321 #include <stdio.h>
00322 #define snprintf _snprintf
00323 #define vsnprintf _vsnprintf
00324 
00325 #else
00326 
00327 #include <sys/stat.h>
00328 #include <sys/types.h>
00329 #include <stdint.h>
00330 #include <unistd.h>
00331 #include <stdio.h>
00332 
00333 #endif
00334 
00335 #undef  getchar
00336 #undef  putchar
00337 
00338 #ifndef _GNU_SOURCE
00339 typedef void (*sighandler_t)(int);  
00340 #endif
00341 typedef unsigned long timeout_t;    
00343 #include <stdlib.h>
00344 #include <ctype.h>
00345 #include <limits.h>
00346 #include <errno.h>
00347 
00348 #ifdef  _MSWINDOWS_
00349 #ifndef ENETDOWN
00350 #define ENETDOWN        ((int)(WSAENETDOWN))
00351 #endif
00352 #ifndef EINPROGRESS
00353 #define EINPROGRESS     ((int)(WSAEINPROGRESS))
00354 #endif
00355 #ifndef ENOPROTOOPT
00356 #define ENOPROTOOPT     ((int)(WSAENOPROTOOPT))
00357 #endif
00358 #ifndef EADDRINUSE
00359 #define EADDRINUSE      ((int)(WSAEADDRINUSE))
00360 #endif
00361 #ifndef EADDRNOTAVAIL
00362 #define EADDRNOTAVAIL   ((int)(WSAEADDRNOTAVAIL))
00363 #endif
00364 #ifndef ENETUNREACH
00365 #define ENETUNREACH     ((int)(WSAENETUNREACH))
00366 #endif
00367 #ifndef EHOSTUNREACH
00368 #define EHOSTUNREACH    ((int)(WSAEHOSTUNREACH))
00369 #endif
00370 #ifndef EHOSTDOWN
00371 #define EHOSTDOWN       ((int)(WSAEHOSTDOWN))
00372 #endif
00373 #ifndef ENETRESET
00374 #define ENETRESET       ((int)(WSAENETRESET))
00375 #endif
00376 #ifndef ECONNABORTED
00377 #define ECONNABORTED    ((int)(WSAECONNABORTED))
00378 #endif
00379 #ifndef ECONNRESET
00380 #define ECONNRESET      ((int)(WSAECONNRESET))
00381 #endif
00382 #ifndef EISCONN
00383 #define EISCONN         ((int)(WSAEISCONN))
00384 #endif
00385 #ifndef ENOTCONN
00386 #define ENOTCONN        ((int)(WSAENOTCONN))
00387 #endif
00388 #ifndef ESHUTDOWN
00389 #define ESHUTDOWN       ((int)(WSAESHUTDOWN))
00390 #endif
00391 #ifndef ETIMEDOUT
00392 #define ETIMEDOUT       ((int)(WSAETIMEDOUT))
00393 #endif
00394 #ifndef ECONNREFUSED
00395 #define ECONNREFUSED    ((int)(WSAECONNREFUSED))
00396 #endif
00397 #endif
00398 
00399 #ifndef DEBUG
00400 #ifndef NDEBUG
00401 #define NDEBUG
00402 #endif
00403 #endif
00404 
00405 #ifdef  DEBUG
00406 #ifdef  NDEBUG
00407 #undef  NDEBUG
00408 #endif
00409 #endif
00410 
00411 #ifndef PROGRAM_MAIN
00412 #define PROGRAM_MAIN(argc, argv)    extern "C" int main(int argc, char **argv)
00413 #define PROGRAM_EXIT(code)          return code
00414 #endif
00415 
00416 #ifndef SERVICE_MAIN
00417 #define SERVICE_MAIN(id, argc, argv)    void service_##id(int argc, char **argv)
00418 typedef void (*cpr_service_t)(int argc, char **argv);
00419 #endif
00420 
00421 #include <assert.h>
00422 #ifdef  DEBUG
00423 #define crit(x, text)   assert(x)
00424 #else
00425 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
00426 #endif
00427 
00434 template<class T>
00435 inline T *init(T *memory)
00436     {return ((memory) ? new(((caddr_t)memory)) T : NULL);}
00437 
00438 typedef long Integer;
00439 typedef unsigned long Unsigned;
00440 typedef double Real;
00441 
00446 inline void strfree(char *str)
00447     {::free(str);}
00448 
00449 #endif