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