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