LinuxSpecific.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : Specific to Linux declarations, inline functions etc.
  9. #ifndef CRYINCLUDE_CRYCOMMON_LINUXSPECIFIC_H
  10. #define CRYINCLUDE_CRYCOMMON_LINUXSPECIFIC_H
  11. #pragma once
  12. #include <stdlib.h>
  13. #include <time.h>
  14. #include <pthread.h>
  15. #include <math.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include <ctype.h>
  19. #include <algorithm>
  20. #include <signal.h>
  21. #ifndef __COUNTER__
  22. #define __COUNTER__ __LINE__
  23. #endif
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <stdio.h>
  28. #if defined(APPLE)
  29. #include <dirent.h>
  30. #endif //defined(APPLE)
  31. #if !defined(SKIP_INET_INCLUDES)
  32. #include <arpa/inet.h>
  33. #include <netinet/in.h>
  34. #include <arpa/inet.h>
  35. #endif // defined(SKIP_INET_INCLUDES)
  36. #include <vector>
  37. #include <string>
  38. typedef void* LPVOID;
  39. #define VOID void
  40. #define PVOID void*
  41. typedef unsigned int UINT;
  42. typedef char CHAR;
  43. typedef float FLOAT;
  44. #define PHYSICS_EXPORTS
  45. // MSVC compiler-specific keywords
  46. #define __forceinline inline
  47. #define _inline inline
  48. #define __cdecl
  49. #define _cdecl
  50. #define __stdcall
  51. #define _stdcall
  52. #define __fastcall
  53. #define _fastcall
  54. #define IN
  55. #define OUT
  56. #ifdef AZ_MONOLITHIC_BUILD
  57. #if !defined(USE_STATIC_NAME_TABLE)
  58. #define USE_STATIC_NAME_TABLE 1
  59. #endif
  60. #endif // AZ_MONOLITHIC_BUILD
  61. #if !defined(_STLP_HASH_MAP) && !defined(USING_STLPORT)
  62. #define _STLP_HASH_MAP 1
  63. #endif
  64. // Enable memory address tracing code.
  65. #if !defined(MM_TRACE_ADDRS) // && !defined(NDEBUG)
  66. #define MM_TRACE_ADDRS 1
  67. #endif
  68. #ifndef STDMETHODCALLTYPE_DEFINED
  69. #define STDMETHODCALLTYPE_DEFINED
  70. #define STDMETHODCALLTYPE
  71. #endif
  72. #define _PACK __attribute__ ((packed))
  73. // Safe memory freeing
  74. #ifndef SAFE_DELETE
  75. #define SAFE_DELETE(p) { if (p) { delete (p); (p) = NULL; } \
  76. }
  77. #endif
  78. #ifndef SAFE_DELETE_ARRAY
  79. #define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p); (p) = NULL; } \
  80. }
  81. #endif
  82. #ifndef SAFE_RELEASE
  83. #define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p) = NULL; } \
  84. }
  85. #endif
  86. #define MAKEWORD(a, b) ((WORD)(((BYTE)((DWORD_PTR)(a) & 0xff)) | ((WORD)((BYTE)((DWORD_PTR)(b) & 0xff))) << 8))
  87. #define MAKELONG(a, b) ((LONG)(((WORD)((DWORD_PTR)(a) & 0xffff)) | ((DWORD)((WORD)((DWORD_PTR)(b) & 0xffff))) << 16))
  88. #define LOWORD(l) ((WORD)((DWORD_PTR)(l) & 0xffff))
  89. #define HIWORD(l) ((WORD)((DWORD_PTR)(l) >> 16))
  90. #define LOBYTE(w) ((BYTE)((DWORD_PTR)(w) & 0xff))
  91. #define HIBYTE(w) ((BYTE)((DWORD_PTR)(w) >> 8))
  92. #define CALLBACK
  93. #define WINAPI
  94. #ifndef __cplusplus
  95. #ifndef _WCHAR_T_DEFINED
  96. typedef unsigned short wchar_t;
  97. #define TCHAR wchar_t;
  98. #define _WCHAR_T_DEFINED
  99. #endif
  100. #endif
  101. typedef wchar_t WCHAR; // wc, 16-bit UNICODE character
  102. typedef WCHAR* PWCHAR;
  103. typedef WCHAR* LPWCH, * PWCH;
  104. typedef const WCHAR* LPCWCH, * PCWCH;
  105. typedef WCHAR* NWPSTR;
  106. typedef WCHAR* LPWSTR, * PWSTR;
  107. typedef WCHAR* LPUWSTR, * PUWSTR;
  108. typedef const WCHAR* LPCWSTR, * PCWSTR;
  109. typedef const WCHAR* LPCUWSTR, * PCUWSTR;
  110. typedef LPCWSTR LPCTSTR;
  111. typedef LPWSTR LPTSTR;
  112. typedef char TCHAR;
  113. typedef DWORD COLORREF;
  114. #define RGB(r,g,b) ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))
  115. #define GetRValue(rgb) (LOBYTE(rgb))
  116. #define GetGValue(rgb) (LOBYTE(((WORD)(rgb)) >> 8))
  117. #define GetBValue(rgb) (LOBYTE((rgb)>>16))
  118. #define MAKEFOURCC(ch0, ch1, ch2, ch3) \
  119. ((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | \
  120. ((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24))
  121. #define FILE_ATTRIBUTE_NORMAL 0x00000080
  122. typedef int BOOL;
  123. typedef int32_t LONG;
  124. typedef uint32_t ULONG;
  125. typedef int HRESULT;
  126. #define BST_UNCHECKED 0x0000
  127. #ifndef MAXUINT
  128. #define MAXUINT ((uint) ~((uint)0))
  129. #endif
  130. #ifndef MAXINT
  131. #define MAXINT ((int)(MAXUINT >> 1))
  132. #endif
  133. #if !defined(__clang__)
  134. typedef int32 __int32;
  135. typedef uint32 __uint32;
  136. typedef int64 __int64;
  137. typedef uint64 __uint64;
  138. #endif
  139. typedef unsigned long int threadID;
  140. #define TRUE 1
  141. #define FALSE 0
  142. #ifndef MAX_PATH
  143. #define MAX_PATH 256
  144. #endif
  145. #ifndef _MAX_PATH
  146. #define _MAX_PATH MAX_PATH
  147. #endif
  148. #define _PTRDIFF_T_DEFINED 1
  149. //#define __TIMESTAMP__ __DATE__" "__TIME__
  150. // function renaming
  151. #define _finite __finite
  152. #define _snprintf snprintf
  153. #define _isnan isnan
  154. #define stricmp strcasecmp
  155. #define _stricmp strcasecmp
  156. #define strnicmp strncasecmp
  157. #define _strnicmp strncasecmp
  158. #define wcsicmp wcscasecmp
  159. #define wcsnicmp wcsncasecmp
  160. typedef union _LARGE_INTEGER
  161. {
  162. struct
  163. {
  164. DWORD LowPart;
  165. LONG HighPart;
  166. };
  167. struct
  168. {
  169. DWORD LowPart;
  170. LONG HighPart;
  171. } u;
  172. long long QuadPart;
  173. } LARGE_INTEGER;
  174. // stdlib.h stuff
  175. #define _MAX_DRIVE 3 // max. length of drive component
  176. #define _MAX_DIR 256 // max. length of path component
  177. #define _MAX_FNAME 256 // max. length of file name component
  178. #define _MAX_EXT 256 // max. length of extension component
  179. // fcntl.h
  180. #define _O_RDONLY 0x0000 /* open for reading only */
  181. #define _O_WRONLY 0x0001 /* open for writing only */
  182. #define _O_RDWR 0x0002 /* open for reading and writing */
  183. #define _O_APPEND 0x0008 /* writes done at eof */
  184. #define _O_CREAT 0x0100 /* create and open file */
  185. #define _O_TRUNC 0x0200 /* open and truncate */
  186. #define _O_EXCL 0x0400 /* open only if file doesn't already exist */
  187. #define _O_TEXT 0x4000 /* file mode is text (translated) */
  188. #define _O_BINARY 0x8000 /* file mode is binary (untranslated) */
  189. #define _O_RAW _O_BINARY
  190. #define _O_NOINHERIT 0x0080 /* child process doesn't inherit file */
  191. #define _O_TEMPORARY 0x0040 /* temporary file bit */
  192. #define _O_SHORT_LIVED 0x1000 /* temporary storage file, try not to flush */
  193. #define _O_SEQUENTIAL 0x0020 /* file access is primarily sequential */
  194. #define _O_RANDOM 0x0010 /* file access is primarily random */
  195. enum
  196. {
  197. IDOK = 1,
  198. IDCANCEL = 2,
  199. IDABORT = 3,
  200. IDRETRY = 4,
  201. IDIGNORE = 5,
  202. IDYES = 6,
  203. IDNO = 7,
  204. IDTRYAGAIN = 10,
  205. IDCONTINUE = 11
  206. };
  207. #define MB_OK 0x00000000L
  208. #define MB_OKCANCEL 0x00000001L
  209. #define MB_ABORTRETRYIGNORE 0x00000002L
  210. #define MB_YESNOCANCEL 0x00000003L
  211. #define MB_YESNO 0x00000004L
  212. #define MB_RETRYCANCEL 0x00000005L
  213. #define MB_CANCELTRYCONTINUE 0x00000006L
  214. #define MB_ICONQUESTION 0x00000020L
  215. #define MB_ICONEXCLAMATION 0x00000030L
  216. #define MB_ICONERROR 0x00000010L
  217. #define MB_ICONWARNING 0x00000030L
  218. #define MB_ICONINFORMATION 0x00000040L
  219. #define MB_SETFOREGROUND 0x00010000L
  220. #define MB_APPLMODAL 0x00000000L
  221. #define MK_LBUTTON 0x0001
  222. #define MK_RBUTTON 0x0002
  223. #define MK_SHIFT 0x0004
  224. #define MK_CONTROL 0x0008
  225. #define MK_MBUTTON 0x0010
  226. #define SM_MOUSEPRESENT 0x00000000L
  227. #define SM_CMOUSEBUTTONS 43
  228. #define VK_TAB 0x09
  229. #define VK_SHIFT 0x10
  230. #define VK_MENU 0x12
  231. #define VK_ESCAPE 0x1B
  232. #define VK_SPACE 0x20
  233. #define VK_DELETE 0x2E
  234. #define VK_OEM_COMMA 0xBC // ',' any country
  235. #define VK_OEM_PERIOD 0xBE // '.' any country
  236. #define VK_OEM_3 0xC0 // '`~' for US
  237. #define VK_OEM_4 0xDB // '[{' for US
  238. #define VK_OEM_6 0xDD // ']}' for US
  239. #define WAIT_TIMEOUT 258L // dderror
  240. #define WM_MOVE 0x0003
  241. #define WM_USER 0x0400
  242. #define WHEEL_DELTA 120
  243. #define WS_CHILD 0x40000000L
  244. #define WS_VISIBLE 0x10000000L
  245. #define CB_ERR (-1)
  246. // io.h stuff
  247. typedef unsigned int _fsize_t;
  248. #define _flushall()
  249. struct _OVERLAPPED;
  250. typedef void (* LPOVERLAPPED_COMPLETION_ROUTINE)(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, struct _OVERLAPPED* lpOverlapped);
  251. typedef struct _OVERLAPPED
  252. {
  253. void* pCaller;//this is orginally reserved for internal purpose, we store the Caller pointer here
  254. LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine; ////this is orginally ULONG_PTR InternalHigh and reserved for internal purpose
  255. union
  256. {
  257. struct
  258. {
  259. DWORD Offset;
  260. DWORD OffsetHigh;
  261. };
  262. PVOID Pointer;
  263. };
  264. DWORD dwNumberOfBytesTransfered; //additional member temporary speciying the number of bytes to be read
  265. /*HANDLE*/ void* hEvent;
  266. } OVERLAPPED, * LPOVERLAPPED;
  267. typedef struct _SECURITY_ATTRIBUTES
  268. {
  269. DWORD nLength;
  270. LPVOID lpSecurityDescriptor;
  271. BOOL bInheritHandle;
  272. } SECURITY_ATTRIBUTES, * PSECURITY_ATTRIBUTES, * LPSECURITY_ATTRIBUTES;
  273. #ifdef __cplusplus
  274. extern bool QueryPerformanceCounter(LARGE_INTEGER*);
  275. extern bool QueryPerformanceFrequency(LARGE_INTEGER* frequency);
  276. static pthread_mutex_t mutex_t;
  277. template<typename T>
  278. const volatile T InterlockedIncrement(volatile T* pT)
  279. {
  280. pthread_mutex_lock(&mutex_t);
  281. ++(*pT);
  282. pthread_mutex_unlock(&mutex_t);
  283. return *pT;
  284. }
  285. template<typename T>
  286. const volatile T InterlockedDecrement(volatile T* pT)
  287. {
  288. pthread_mutex_lock(&mutex_t);
  289. --(*pT);
  290. pthread_mutex_unlock(&mutex_t);
  291. return *pT;
  292. }
  293. #if 0
  294. template<typename S, typename T>
  295. inline const S& min(const S& rS, const T& rT)
  296. {
  297. return (rS <= rT) ? rS : rT;
  298. }
  299. template<typename S, typename T>
  300. inline const S& max(const S& rS, const T& rT)
  301. {
  302. return (rS >= rT) ? rS : rT;
  303. }
  304. #endif
  305. template<typename S, typename T>
  306. inline S __min(const S& rS, const T& rT)
  307. {
  308. return std::min(rS, rT);
  309. }
  310. template<typename S, typename T>
  311. inline S __max(const S& rS, const T& rT)
  312. {
  313. return std::max(rS, rT);
  314. }
  315. typedef enum
  316. {
  317. INVALID_HANDLE_VALUE = -1l
  318. }INVALID_HANDLE_VALUE_ENUM;
  319. //for compatibility reason we got to create a class which actually contains an int rather than a void* and make sure it does not get mistreated
  320. template <class T, T U>
  321. //U is default type for invalid handle value, T the encapsulated handle type to be used instead of void* (as under windows and never linux)
  322. class CHandle
  323. {
  324. public:
  325. typedef T HandleType;
  326. typedef void* PointerType; //for compatibility reason to encapsulate a void* as an int
  327. static const HandleType sciInvalidHandleValue = U;
  328. CHandle(const CHandle<T, U>& cHandle)
  329. : m_Value(cHandle.m_Value){}
  330. CHandle(const HandleType cHandle = U)
  331. : m_Value(cHandle){}
  332. CHandle(const PointerType cpHandle)
  333. : m_Value(reinterpret_cast<HandleType>(cpHandle)){}
  334. CHandle(INVALID_HANDLE_VALUE_ENUM)
  335. : m_Value(U){} //to be able to use a common value for all InvalidHandle - types
  336. #if defined(LINUX64) && !defined(__clang__)
  337. //treat __null tyope also as invalid handle type
  338. CHandle(__typeof__(__null))
  339. : m_Value(U){} //to be able to use a common value for all InvalidHandle - types
  340. #endif
  341. operator HandleType(){
  342. return m_Value;
  343. }
  344. bool operator!() const{return m_Value == sciInvalidHandleValue; }
  345. const CHandle& operator =(const CHandle& crHandle){m_Value = crHandle.m_Value; return *this; }
  346. const CHandle& operator =(const PointerType cpHandle){m_Value = (HandleType) reinterpret_cast<UINT_PTR>(cpHandle); return *this; }
  347. const bool operator ==(const CHandle& crHandle) const{return m_Value == crHandle.m_Value; }
  348. const bool operator ==(const HandleType cHandle) const{return m_Value == cHandle; }
  349. const bool operator ==(const PointerType cpHandle) const{return m_Value == (HandleType) reinterpret_cast<UINT_PTR>(cpHandle); }
  350. const bool operator !=(const HandleType cHandle) const{return m_Value != cHandle; }
  351. const bool operator !=(const CHandle& crHandle) const{return m_Value != crHandle.m_Value; }
  352. const bool operator !=(const PointerType cpHandle) const{return m_Value != (HandleType) reinterpret_cast<UINT_PTR>(cpHandle); }
  353. const bool operator < (const CHandle& crHandle) const{return m_Value < crHandle.m_Value; }
  354. HandleType Handle() const{return m_Value; }
  355. private:
  356. HandleType m_Value; //the actual value, remember that file descriptors are ints under linux
  357. typedef void ReferenceType; //for compatibility reason to encapsulate a void* as an int
  358. //forbid these function which would actually not work on an int
  359. PointerType operator->();
  360. PointerType operator->() const;
  361. ReferenceType operator*();
  362. ReferenceType operator*() const;
  363. operator PointerType();
  364. };
  365. typedef CHandle<int, (int) - 1l> HANDLE;
  366. typedef HANDLE EVENT_HANDLE;
  367. typedef pid_t THREAD_HANDLE;
  368. typedef HANDLE HKEY;
  369. // typedef HANDLE HDC;
  370. typedef HANDLE HBITMAP;
  371. typedef HANDLE HMENU;
  372. inline int64 CryGetTicks()
  373. {
  374. LARGE_INTEGER counter;
  375. QueryPerformanceCounter(&counter);
  376. return counter.QuadPart;
  377. }
  378. #endif //__cplusplus
  379. inline int _CrtCheckMemory() { return 1; };
  380. typedef void* HGLRC;
  381. typedef void* HDC;
  382. typedef void* PROC;
  383. typedef void* PIXELFORMATDESCRIPTOR;
  384. template <typename T, size_t N>
  385. char (*RtlpNumberOf( T (&)[N] ))[N];
  386. #define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A)))
  387. #define ARRAYSIZE(A) RTL_NUMBER_OF_V2(A)
  388. #undef SUCCEEDED
  389. #define SUCCEEDED(x) ((x) >= 0)
  390. #undef FAILED
  391. #define FAILED(x) (!(SUCCEEDED(x)))
  392. #endif // CRYINCLUDE_CRYCOMMON_LINUXSPECIFIC_H
  393. // vim:ts=2:sw=2:tw=78