platform.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 : Platform dependent stuff.
  9. // Include this file instead of windows h
  10. #pragma once
  11. #if defined(AZ_RESTRICTED_PLATFORM)
  12. #undef AZ_RESTRICTED_SECTION
  13. #define PLATFORM_H_SECTION_3 3
  14. #define PLATFORM_H_SECTION_5 5
  15. #define PLATFORM_H_SECTION_6 6
  16. #define PLATFORM_H_SECTION_7 7
  17. #define PLATFORM_H_SECTION_8 8
  18. #define PLATFORM_H_SECTION_10 10
  19. #define PLATFORM_H_SECTION_11 11
  20. #define PLATFORM_H_SECTION_12 12
  21. #define PLATFORM_H_SECTION_13 13
  22. #define PLATFORM_H_SECTION_14 14
  23. #define PLATFORM_H_SECTION_15 15
  24. #endif
  25. #if (defined(LINUX) && !defined(ANDROID)) || defined(APPLE)
  26. #define _FILE_OFFSET_BITS 64 // define large file support > 2GB
  27. #endif
  28. #include <AzCore/PlatformIncl.h>
  29. #if defined(AZ_RESTRICTED_PLATFORM)
  30. #define AZ_RESTRICTED_SECTION PLATFORM_H_SECTION_3
  31. #include AZ_RESTRICTED_FILE(platform_h)
  32. #endif
  33. #if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
  34. #undef AZ_RESTRICTED_SECTION_IMPLEMENTED
  35. #elif defined(MOBILE)
  36. #define CONSOLE
  37. #endif
  38. #if defined(AZ_RESTRICTED_PLATFORM)
  39. #define AZ_RESTRICTED_SECTION PLATFORM_H_SECTION_5
  40. #include AZ_RESTRICTED_FILE(platform_h)
  41. #endif
  42. #if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
  43. #undef AZ_RESTRICTED_SECTION_IMPLEMENTED
  44. #elif defined(LINUX) || defined(APPLE)
  45. #define __STDC_FORMAT_MACROS
  46. #include <cinttypes>
  47. #else
  48. #include <cinttypes>
  49. #endif
  50. #if !defined(PRISIZE_T)
  51. #if defined(AZ_RESTRICTED_PLATFORM)
  52. #define AZ_RESTRICTED_SECTION PLATFORM_H_SECTION_6
  53. #include AZ_RESTRICTED_FILE(platform_h)
  54. #endif
  55. #if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
  56. #undef AZ_RESTRICTED_SECTION_IMPLEMENTED
  57. #elif defined(WIN64)
  58. #define PRISIZE_T "I64u" //size_t defined as unsigned __int64
  59. #elif defined(WIN32) || defined(LINUX32)
  60. #define PRISIZE_T "u"
  61. #elif defined(MAC) || defined(LINUX64) || defined(IOS)
  62. #define PRISIZE_T "lu"
  63. #else
  64. #error "Please defined PRISIZE_T for this platform"
  65. #endif
  66. #endif
  67. #if !defined(PRI_THREADID)
  68. #if defined(AZ_RESTRICTED_PLATFORM)
  69. #define AZ_RESTRICTED_SECTION PLATFORM_H_SECTION_7
  70. #include AZ_RESTRICTED_FILE(platform_h)
  71. #endif
  72. #if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
  73. #undef AZ_RESTRICTED_SECTION_IMPLEMENTED
  74. #elif defined(MAC) || defined(IOS) && defined(__LP64__) && defined(__LP64__)
  75. #define PRI_THREADID "lld"
  76. #elif defined(LINUX64) || defined(ANDROID)
  77. #define PRI_THREADID "ld"
  78. #else
  79. #define PRI_THREADID "d"
  80. #endif
  81. #endif
  82. #include "ProjectDefines.h" // to get some defines available in every CryEngine project
  83. // Function attribute for printf/scanf-style parameters.
  84. // This enables extended argument checking by GCC.
  85. //
  86. // Usage:
  87. // Put this after the function or method declaration (not the definition!),
  88. // between the final closing parenthesis and the semicolon.
  89. // The first parameter indicates the 1-based index of the format string
  90. // parameter, the second parameter indicates the 1-based index of the first
  91. // variable parameter. Example:
  92. // void foobar(int a, const char *fmt, ...) PRINTF_PARAMS(2, 3);
  93. //
  94. // For va_list based printf style functions, specfy 0 as the second parameter.
  95. // Example:
  96. // void foobarv(int a, const char *fmt, va_list ap) PRINTF_PARAMS(2, 0);
  97. //
  98. // Note that 'this' is counted as a method argument. For non-static methods,
  99. // add 1 to the indices.
  100. //
  101. // Use PRINTF_EMPTY_STRING when you want to format an empty string using
  102. // a function defined with PRINTF_PARAMS to avoid zero length format string
  103. // warnings when these checks are enabled.
  104. #if defined(__GNUC__) && !defined(_RELEASE)
  105. #define PRINTF_PARAMS(...) __attribute__ ((format (printf, __VA_ARGS__)))
  106. #define SCANF_PARAMS(...) __attribute__ ((format (scanf, __VA_ARGS__)))
  107. #define PRINTF_EMPTY_FORMAT "%s", ""
  108. #else
  109. #define PRINTF_PARAMS(...)
  110. #define SCANF_PARAMS(...)
  111. #define PRINTF_EMPTY_FORMAT ""
  112. #endif
  113. //default stack size for threads, currently only used on pthread platforms
  114. #if defined(AZ_RESTRICTED_PLATFORM)
  115. #define AZ_RESTRICTED_SECTION PLATFORM_H_SECTION_8
  116. #include AZ_RESTRICTED_FILE(platform_h)
  117. #endif
  118. #if defined(AZ_RESTRICTED_SECTION_IMPLEMENTED)
  119. #undef AZ_RESTRICTED_SECTION_IMPLEMENTED
  120. #endif
  121. #include <AzCore/PlatformDef.h>
  122. //////////////////////////////////////////////////////////////////////////
  123. // Define BIT macro for use in enums and bit masks.
  124. #define BIT(x) (1 << (x))
  125. #define BIT64(x) (1ll << (x))
  126. #define TYPED_BIT(type, x) (type(1) << (x))
  127. //////////////////////////////////////////////////////////////////////////
  128. //////////////////////////////////////////////////////////////////////////
  129. // Help message, all help text in code must be wrapped in this define.
  130. // Only include these in non RELEASE builds
  131. #if !defined(_RELEASE)
  132. #define _HELP(x) x
  133. #else
  134. #define _HELP(x) ""
  135. #endif
  136. #ifdef _PREFAST_
  137. # define PREFAST_ASSUME(cond) __analysis_assume(cond)
  138. #else
  139. # define PREFAST_ASSUME(cond)
  140. #endif
  141. #if defined(AZ_RESTRICTED_PLATFORM)
  142. #define AZ_RESTRICTED_SECTION PLATFORM_H_SECTION_10
  143. #include AZ_RESTRICTED_FILE(platform_h)
  144. #else
  145. #if defined(WIN64)
  146. #include "Win64specific.h"
  147. #elif defined(LINUX64) && !defined(ANDROID)
  148. #include "Linux64Specific.h"
  149. #elif defined(MAC)
  150. #include "MacSpecific.h"
  151. #elif defined(ANDROID)
  152. #include "AndroidSpecific.h"
  153. #elif defined(IOS)
  154. #include "iOSSpecific.h"
  155. #endif
  156. #endif
  157. #if !defined(TARGET_DEFAULT_ALIGN)
  158. # error "No default alignment specified for target architecture"
  159. #endif
  160. // Indicates potentially dangerous cast on 64bit machines
  161. typedef UINT_PTR TRUNCATE_PTR;
  162. typedef UINT_PTR EXPAND_PTR;
  163. // Use static branch prediction to improve the generated assembly when possible.
  164. // This feature has an indirect effect on runtime performance, as it ensures assembly code
  165. // which is more likely needed (the programmer has to decide this), is directly after the if
  166. //
  167. // For reference, as far as i am aware, all compilers use the following heuristic for static branch prediction
  168. // if branches are always taken
  169. // forward jumps are not taken
  170. // backwards jumps are taken (eg. jumping back to the beginning of a loop)
  171. #if defined(__clang__) || defined(__GNUC__)
  172. # define IF(condition, hint) if (__builtin_expect(!!(condition), hint))
  173. # define WHILE(condition, hint) while (__builtin_expect(!!(condition), hint))
  174. # define IF_UNLIKELY(condition) if (__builtin_expect(!!(condition), 0))
  175. # define IF_LIKELY(condition) if (__builtin_expect(!!(condition), 1))
  176. #else
  177. // Fallback for compilers which don't support static branch prediction (like MSVC)
  178. # define IF(condition, hint) if ((condition))
  179. # define WHILE(condition, hint) while ((condition))
  180. # define IF_UNLIKELY(condition) if ((condition))
  181. # define IF_LIKELY(condition) if ((condition))
  182. #endif // !defined(__clang__) || defined(__GNUC__)
  183. #include <stdio.h>
  184. //////////////////////////////////////////////////////////////////////////
  185. // Provide special cast function which mirrors C++ style casts to support aliasing correct type punning casts in gcc with strict-aliasing enabled
  186. template<typename DestinationType, typename SourceType>
  187. ILINE DestinationType alias_cast(SourceType pPtr)
  188. {
  189. union
  190. {
  191. SourceType pSrc;
  192. DestinationType pDst;
  193. } conv_union;
  194. conv_union.pSrc = pPtr;
  195. return conv_union.pDst;
  196. }
  197. //////////////////////////////////////////////////////////////////////////
  198. #ifndef DEPRECATED
  199. #define DEPRECATED
  200. #endif
  201. // Assert dialog box macros
  202. #include "CryAssert.h"
  203. //////////////////////////////////////////////////////////////////////////
  204. // Platform dependent functions that emulate Win32 API.
  205. // Mostly used only for debugging!
  206. //////////////////////////////////////////////////////////////////////////
  207. void CrySleep(unsigned int dwMilliseconds);
  208. void CryMessageBox(const char* lpText, const char* lpCaption, unsigned int uType);
  209. //---------------------------------------------------------------------------
  210. // Useful function to clean the structure.
  211. template <class T>
  212. inline void ZeroStruct(T& t)
  213. { memset(static_cast<void*>(&t), 0, sizeof(t)); }
  214. // Useful functions to init and destroy objects.
  215. template<class T>
  216. inline void Construct(T& t)
  217. { new(&t)T(); }
  218. template<class T, class U>
  219. inline void Construct(T& t, U const& u)
  220. { new(&t)T(u); }
  221. template<class T>
  222. inline void Destruct(T& t)
  223. { t.~T(); }
  224. // Cast one type to another, asserting there is no conversion loss.
  225. // Usage: DestType dest = check_cast<DestType>(src);
  226. template<class D, class S>
  227. inline D check_cast(S const& s)
  228. {
  229. D d = D(s);
  230. assert(S(d) == s);
  231. return d;
  232. }
  233. //---------------------------------------------------------------------------
  234. // Quick const-manipulation macros
  235. // Declare a const and variable version of a function simultaneously.
  236. #define CONST_VAR_FUNCTION(head, body) \
  237. inline head body \
  238. inline const head const body
  239. template<class T>
  240. inline
  241. T& non_const(const T& t)
  242. { return const_cast<T&>(t); }
  243. #define using_type(super, type) \
  244. typedef typename super::type type;
  245. typedef unsigned char uchar;
  246. typedef unsigned int uint;
  247. typedef const char* cstr;
  248. //---------------------------------------------------------------------------
  249. // Align function works on integer or pointer values.
  250. // Only support power-of-two alignment.
  251. template<typename T>
  252. inline
  253. T Align(T nData, size_t nAlign)
  254. {
  255. assert((nAlign & (nAlign - 1)) == 0);
  256. size_t size = ((size_t)nData + (nAlign - 1)) & ~(nAlign - 1);
  257. return T(size);
  258. }
  259. template<typename T>
  260. inline
  261. bool IsAligned(T nData, size_t nAlign)
  262. {
  263. assert((nAlign & (nAlign - 1)) == 0);
  264. return (size_t(nData) & (nAlign - 1)) == 0;
  265. }
  266. template<typename T, typename U>
  267. inline
  268. void SetFlags(T& dest, U flags, bool b)
  269. {
  270. if (b)
  271. {
  272. dest |= flags;
  273. }
  274. else
  275. {
  276. dest &= ~flags;
  277. }
  278. }
  279. // Wrapper code for non-windows builds.
  280. #if defined(LINUX) || defined(APPLE)
  281. #include "Linux_Win32Wrapper.h"
  282. #elif defined(AZ_RESTRICTED_PLATFORM)
  283. #define AZ_RESTRICTED_SECTION PLATFORM_H_SECTION_12
  284. #include AZ_RESTRICTED_FILE(platform_h)
  285. #endif
  286. threadID CryGetCurrentThreadId();
  287. #ifdef __GNUC__
  288. #define NO_INLINE __attribute__ ((noinline))
  289. #define NO_INLINE_WEAK __attribute__ ((noinline)) __attribute__((weak)) // marks a function as no_inline, but also as weak to prevent multiple-defined errors
  290. #define __PACKED __attribute__ ((packed))
  291. #else
  292. #define NO_INLINE _declspec(noinline)
  293. #define NO_INLINE_WEAK _declspec(noinline) inline
  294. #define __PACKED
  295. #endif
  296. #if defined(AZ_RESTRICTED_PLATFORM)
  297. #define AZ_RESTRICTED_SECTION PLATFORM_H_SECTION_13
  298. #include AZ_RESTRICTED_FILE(platform_h)
  299. #elif !defined(LINUX) && !defined(APPLE)
  300. typedef int socklen_t;
  301. #endif
  302. // In RELEASE disable printf and fprintf
  303. #if defined(_RELEASE) && !defined(RELEASE_LOGGING)
  304. #if defined(AZ_RESTRICTED_PLATFORM)
  305. #define AZ_RESTRICTED_SECTION PLATFORM_H_SECTION_14
  306. #include AZ_RESTRICTED_FILE(platform_h)
  307. #endif
  308. #endif
  309. #if defined(AZ_RESTRICTED_PLATFORM)
  310. #define AZ_RESTRICTED_SECTION PLATFORM_H_SECTION_15
  311. #include AZ_RESTRICTED_FILE(platform_h)
  312. #endif
  313. void InitRootDir(char szExeFileName[] = nullptr, uint nExeSize = 0, char szExeRootName[] = nullptr, uint nRootSize = 0);