UCommon
/usr/src/RPM/BUILD/ucommon-6.3.3/inc/ucommon/platform.h
Go to the documentation of this file.
00001 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
00002 // Copyright (C) 2015 Cherokees of Idaho.
00003 //
00004 // This file is part of GNU uCommon C++.
00005 //
00006 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License as published
00008 // by the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 //
00011 // GNU uCommon C++ is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public License
00017 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00018 
00028 #ifndef _UCOMMON_PLATFORM_H_
00029 #define _UCOMMON_PLATFORM_H_
00030 #define UCOMMON_ABI 6
00031 
00032 #ifndef UCOMMON_SYSRUNTIME
00033 #ifndef NEW_STDCPP
00034 #define NEW_STDCPP
00035 #endif
00036 #define _UCOMMON_EXTENDED_
00037 #endif
00038 
00049 #define UCOMMON_NAMESPACE   ucommon
00050 #define NAMESPACE_UCOMMON   namespace ucommon {
00051 #define END_NAMESPACE       }
00052 
00053 #ifndef _REENTRANT
00054 #define _REENTRANT 1
00055 #endif
00056 
00057 #ifndef __PTH__
00058 #ifndef _THREADSAFE
00059 #define _THREADSAFE 1
00060 #endif
00061 
00062 #ifndef _POSIX_PTHREAD_SEMANTICS
00063 #define _POSIX_PTHREAD_SEMANTICS
00064 #endif
00065 #endif
00066 
00067 #if defined(__GNUC__) && (__GNUC < 3) && !defined(_GNU_SOURCE)
00068 #define _GNU_SOURCE
00069 #endif
00070 
00071 #if __GNUC__ > 3 || (__GNUC__ == 3 && (__GNU_MINOR__ > 3))
00072 #define __PRINTF(x,y)   __attribute__ ((format (printf, x, y)))
00073 #define __SCANF(x, y) __attribute__ ((format (scanf, x, y)))
00074 #define __MALLOC      __attribute__ ((malloc))
00075 #endif
00076 
00077 #ifndef __MALLOC
00078 #define __PRINTF(x, y)
00079 #define __SCANF(x, y)
00080 #define __MALLOC
00081 #endif
00082 
00083 #ifndef DEBUG
00084 #ifndef NDEBUG
00085 #define NDEBUG
00086 #endif
00087 #endif
00088 
00089 #ifdef  DEBUG
00090 #ifdef  NDEBUG
00091 #undef  NDEBUG
00092 #endif
00093 #endif
00094 
00095 // see if targeting legacy Microsoft windows platform
00096 
00097 #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
00098 #define _MSWINDOWS_
00099 
00100 #if defined(_MSC_VER)
00101 #define NOMINMAX
00102 #endif
00103 
00104 #if defined(_M_X64) || defined(_M_ARM)
00105 #define _MSCONDITIONALS_
00106 #ifndef _WIN32_WINNT
00107 #define _WIN32_WINNT    0x0600
00108 #endif
00109 #endif
00110 
00111 //#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0501
00112 //#undef    _WIN32_WINNT
00113 //#define   _WIN32_WINNT 0x0501
00114 //#endif
00115 
00116 //#ifndef _WIN32_WINNT
00117 //#define   _WIN32_WINNT 0x0501
00118 //#endif
00119 
00120 #ifdef  _MSC_VER
00121 #pragma warning(disable: 4251)
00122 #pragma warning(disable: 4996)
00123 #pragma warning(disable: 4355)
00124 #pragma warning(disable: 4290)
00125 #pragma warning(disable: 4291)
00126 #endif
00127 
00128 #if defined(__BORLANDC__) && !defined(__MT__)
00129 #error Please enable multithreading
00130 #endif
00131 
00132 #if defined(_MSC_VER) && !defined(_MT)
00133 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
00134 #endif
00135 
00136 // Require for compiling with critical sections.
00137 #ifndef _WIN32_WINNT
00138 #define _WIN32_WINNT 0x0501
00139 #endif
00140 
00141 // Make sure we're consistent with _WIN32_WINNT
00142 #ifndef WINVER
00143 #define WINVER _WIN32_WINNT
00144 #endif
00145 
00146 #ifndef WIN32_LEAN_AND_MEAN
00147 #define WIN32_LEAN_AND_MEAN
00148 #endif
00149 
00150 #include <winsock2.h>
00151 #include <ws2tcpip.h>
00152 
00153 #if defined(_MSC_VER)
00154 typedef signed long ssize_t;
00155 typedef int pid_t;
00156 #endif
00157 
00158 #include <process.h>
00159 #ifndef __EXPORT
00160 #ifdef  UCOMMON_STATIC
00161 #define __EXPORT
00162 #else
00163 #define __EXPORT    __declspec(dllimport)
00164 #endif
00165 #endif
00166 #define __LOCAL
00167 
00168 // if runtime mode then non-runtime libraries are static on windows...
00169 #if defined(UCOMMON_RUNTIME) || defined(UCOMMON_STATIC)
00170 #define __SHARED
00171 #else
00172 #define __SHARED __EXPORT
00173 #endif
00174 
00175 #else
00176 #define __EXPORT    __attribute__ ((visibility("default")))
00177 #define __LOCAL     __attribute__ ((visibility("hidden")))
00178 #define __SHARED    __attribute__ ((visibility("default")))
00179 #endif
00180 
00181 #ifdef  _MSWINDOWS_
00182 
00183 #define _UWIN
00184 
00185 #include <sys/stat.h>
00186 #include <io.h>
00187 
00188 // gcc c++11 support on mingw requires pthread support library
00189 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 7)) && !defined(UCOMMON_SYSRUNTIME) && defined(__MINGW_WINPTHREAD__)
00190 #include <pthread.h>   // gnu libstdc++ now requires a win pthread
00191 #undef  _MSCONDITIONALS_
00192 #else   
00193 #define _MSTHREADS_
00194 typedef DWORD pthread_t;
00195 typedef CRITICAL_SECTION pthread_mutex_t;
00196 #endif
00197 typedef char *caddr_t;
00198 typedef HANDLE fd_t;
00199 typedef SOCKET socket_t;
00200 
00201 #ifdef  _MSC_VER
00202 typedef struct timespec {
00203     time_t tv_sec;
00204     long  tv_nsec;
00205 } timespec_t;
00206 #endif
00207 
00208 inline void sleep(int seconds)
00209     {::Sleep((seconds * 1000l));}
00210 
00211 extern "C" {
00212 
00213     #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
00214 
00215     typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
00216 
00217 #ifdef _MSTHREADS_
00218     inline void pthread_exit(void *p)
00219         {_endthreadex((DWORD)0);}
00220 
00221     inline pthread_t pthread_self(void)
00222         {return (pthread_t)GetCurrentThreadId();}
00223 
00224     inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00225         {InitializeCriticalSection(mutex); return 0;}
00226 
00227     inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00228         {DeleteCriticalSection(mutex);}
00229 
00230     inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00231         {EnterCriticalSection(mutex);}
00232 
00233     inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00234         {LeaveCriticalSection(mutex);}
00235 #endif
00236 
00237     inline char *strdup(const char *s)
00238         {return _strdup(s);}
00239 
00240     inline int stricmp(const char *s1, const char *s2)
00241         {return _stricmp(s1, s2);}
00242 
00243     inline int strnicmp(const char *s1, const char *s2, size_t l)
00244         {return _strnicmp(s1, s2, l);}
00245 }
00246 
00247 #elif defined(__PTH__)
00248 
00249 #include <pth.h>
00250 #include <sys/wait.h>
00251 
00252 typedef int socket_t;
00253 typedef int fd_t;
00254 #define INVALID_SOCKET -1
00255 #define INVALID_HANDLE_VALUE -1
00256 #include <signal.h>
00257 
00258 #define pthread_mutex_t pth_mutex_t
00259 #define pthread_cond_t pth_cond_t
00260 #define pthread_t pth_t
00261 
00262 inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
00263     {return pth_sigmask(how, set, oset);};
00264 
00265 inline void pthread_exit(void *p)
00266     {pth_exit(p);};
00267 
00268 inline void pthread_kill(pthread_t tid, int sig)
00269     {pth_raise(tid, sig);};
00270 
00271 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00272     {return pth_mutex_init(mutex) != 0;};
00273 
00274 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00275     {};
00276 
00277 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00278     {pth_mutex_acquire(mutex, 0, NULL);};
00279 
00280 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00281     {pth_mutex_release(mutex);};
00282 
00283 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
00284     {pth_cond_await(cond, mutex, NULL);};
00285 
00286 inline void pthread_cond_signal(pthread_cond_t *cond)
00287     {pth_cond_notify(cond, FALSE);};
00288 
00289 inline void pthread_cond_broadcast(pthread_cond_t *cond)
00290     {pth_cond_notify(cond, TRUE);};
00291 
00292 #else
00293 
00294 #include <pthread.h>
00295 
00296 typedef int socket_t;
00297 typedef int fd_t;
00298 #define INVALID_SOCKET -1
00299 #define INVALID_HANDLE_VALUE -1
00300 #include <signal.h>
00301 
00302 #endif
00303 
00304 #if defined(sun) && defined(unix)
00305 #include <malloc.h>
00306 #endif
00307 
00308 #ifdef _MSC_VER
00309 typedef signed __int8 int8_t;
00310 typedef unsigned __int8 uint8_t;
00311 typedef signed __int16 int16_t;
00312 typedef unsigned __int16 uint16_t;
00313 typedef signed __int32 int32_t;
00314 typedef unsigned __int32 uint32_t;
00315 typedef signed __int64 int64_t;
00316 typedef unsigned __int64 uint64_t;
00317 typedef char *caddr_t;
00318 
00319 #include <stdio.h>
00320 #define snprintf _snprintf
00321 #define vsnprintf _vsnprintf
00322 
00323 #else
00324 
00325 #include <sys/stat.h>
00326 #include <sys/types.h>
00327 #include <stdint.h>
00328 #include <unistd.h>
00329 #include <stdio.h>
00330 
00331 #endif
00332 
00333 #undef  getchar
00334 #undef  putchar
00335 
00336 #ifndef _GNU_SOURCE
00337 typedef void (*sighandler_t)(int);  
00338 #endif
00339 typedef unsigned long timeout_t;    
00341 #include <cstdlib>
00342 #include <cctype>
00343 #include <climits>
00344 #include <cerrno>
00345 #ifndef UCOMMON_RUNTIME
00346 #include <new>
00347 #endif
00348 
00349 #ifdef  _MSWINDOWS_
00350 #ifndef ENETDOWN
00351 #define ENETDOWN        ((int)(WSAENETDOWN))
00352 #endif
00353 #ifndef EINPROGRESS
00354 #define EINPROGRESS     ((int)(WSAEINPROGRESS))
00355 #endif
00356 #ifndef ENOPROTOOPT
00357 #define ENOPROTOOPT     ((int)(WSAENOPROTOOPT))
00358 #endif
00359 #ifndef EADDRINUSE
00360 #define EADDRINUSE      ((int)(WSAEADDRINUSE))
00361 #endif
00362 #ifndef EADDRNOTAVAIL
00363 #define EADDRNOTAVAIL   ((int)(WSAEADDRNOTAVAIL))
00364 #endif
00365 #ifndef ENETUNREACH
00366 #define ENETUNREACH     ((int)(WSAENETUNREACH))
00367 #endif
00368 #ifndef EHOSTUNREACH
00369 #define EHOSTUNREACH    ((int)(WSAEHOSTUNREACH))
00370 #endif
00371 #ifndef EHOSTDOWN
00372 #define EHOSTDOWN       ((int)(WSAEHOSTDOWN))
00373 #endif
00374 #ifndef ENETRESET
00375 #define ENETRESET       ((int)(WSAENETRESET))
00376 #endif
00377 #ifndef ECONNABORTED
00378 #define ECONNABORTED    ((int)(WSAECONNABORTED))
00379 #endif
00380 #ifndef ECONNRESET
00381 #define ECONNRESET      ((int)(WSAECONNRESET))
00382 #endif
00383 #ifndef EISCONN
00384 #define EISCONN         ((int)(WSAEISCONN))
00385 #endif
00386 #ifndef ENOTCONN
00387 #define ENOTCONN        ((int)(WSAENOTCONN))
00388 #endif
00389 #ifndef ESHUTDOWN
00390 #define ESHUTDOWN       ((int)(WSAESHUTDOWN))
00391 #endif
00392 #ifndef ETIMEDOUT
00393 #define ETIMEDOUT       ((int)(WSAETIMEDOUT))
00394 #endif
00395 #ifndef ECONNREFUSED
00396 #define ECONNREFUSED    ((int)(WSAECONNREFUSED))
00397 #endif
00398 #endif
00399 
00400 #ifndef DEBUG
00401 #ifndef NDEBUG
00402 #define NDEBUG
00403 #endif
00404 #endif
00405 
00406 #ifdef  DEBUG
00407 #ifdef  NDEBUG
00408 #undef  NDEBUG
00409 #endif
00410 #endif
00411 
00412 #ifndef PROGRAM_MAIN
00413 #define PROGRAM_MAIN(argc, argv)    extern "C" int main(int argc, char **argv)
00414 #define PROGRAM_EXIT(code)          return code
00415 #endif
00416 
00417 #ifndef SERVICE_MAIN
00418 #define SERVICE_MAIN(id, argc, argv)    void service_##id(int argc, char **argv)
00419 typedef void (*cpr_service_t)(int argc, char **argv);
00420 #endif
00421 
00422 #include <assert.h>
00423 #ifdef  DEBUG
00424 #define crit(x, text)   assert(x)
00425 #else
00426 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
00427 #endif
00428 
00435 template<class T>
00436 inline T *init(T *memory)
00437     {return ((memory) ? new(((void *)memory)) T : NULL);}
00438 
00439 typedef long Integer;
00440 typedef unsigned long Unsigned;
00441 typedef double Real;
00442 
00447 inline void strfree(char *str)
00448     {::free(str);}
00449 
00450 #endif