CommonPal.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #define __MAKE_WARNING__(X) "This compiler does not support '" ## X ## "'"
  7. // Define _ALWAYSINLINE for template that want to always inline, but doesn't allow inline linkage in clang
  8. #if defined(__GNUC__) || defined(__clang__)
  9. #if __has_attribute(always_inline)
  10. #define _ALWAYSINLINE __attribute__((always_inline))
  11. #define __forceinline inline _ALWAYSINLINE
  12. #else // No always_inline support
  13. #pragma message __MAKE_WARNING__("always_inline")
  14. #define _ALWAYSINLINE inline
  15. #define __forceinline _ALWAYSINLINE
  16. #endif
  17. #if __has_attribute(noinline)
  18. #ifdef CLANG_HAS_DISABLE_TAIL_CALLS
  19. #define _NOINLINE __attribute__((noinline, disable_tail_calls))
  20. #else
  21. #define _NOINLINE __attribute__((noinline))
  22. #endif
  23. #else // No noinline support
  24. #pragma message __MAKE_WARNING__("noinline")
  25. #define _NOINLINE
  26. #endif
  27. #else // Windows
  28. #define _ALWAYSINLINE __forceinline
  29. #define _NOINLINE __declspec(noinline)
  30. #define __forceinline inline
  31. #endif
  32. // Only VC compiler support overflow guard
  33. #if defined(__GNUC__) || defined(__clang__)
  34. #define DECLSPEC_GUARD_OVERFLOW
  35. #else // Windows
  36. #define DECLSPEC_GUARD_OVERFLOW __declspec(guard(overflow))
  37. #endif
  38. #ifdef __clang__
  39. #define CLANG_WNO_BEGIN_(x) \
  40. _Pragma("clang diagnostic push")\
  41. _Pragma(#x)
  42. #define CLANG_WNO_BEGIN(x) CLANG_WNO_BEGIN_(clang diagnostic ignored x)
  43. #define CLANG_WNO_END
  44. _Pragma("clang diagnostic pop")
  45. #else
  46. #define CLANG_WNO_BEGIN(x)
  47. #define CLANG_WNO_END
  48. #endif
  49. #ifndef __has_builtin
  50. #define __has_builtin(x) 0
  51. #endif
  52. #ifdef _WIN32
  53. #pragma warning(push)
  54. #pragma warning(disable: 4995) /* 'function': name was marked as #pragma deprecated */
  55. // === Windows Header Files ===
  56. #define INC_OLE2 /* for windows.h */
  57. #define CONST_VTABLE /* for objbase.h */
  58. #include <windows.h>
  59. /* Don't want GetObject and GetClassName to be defined to GetObjectW and GetClassNameW */
  60. #undef GetObject
  61. #undef GetClassName
  62. #undef Yield /* winbase.h defines this but we want to use it for Js::OpCode::Yield; it is Win16 legacy, no harm undef'ing it */
  63. #pragma warning(pop)
  64. // xplat-todo: get a better name for this macro
  65. #define _u(s) L##s
  66. #define INIT_PRIORITY(x)
  67. #define get_cpuid __cpuid
  68. #if defined(__clang__)
  69. __forceinline void __int2c()
  70. {
  71. __asm int 0x2c
  72. }
  73. #endif
  74. #else // !_WIN32
  75. #define USING_PAL_STDLIB 1
  76. #define STRSAFE_INLINE 1
  77. #ifdef PAL_STDCPP_COMPAT
  78. #include <wchar.h>
  79. #include <math.h>
  80. #include <time.h>
  81. #if defined(_AMD64_) || defined(__i686__)
  82. #include <smmintrin.h>
  83. #include <xmmintrin.h>
  84. #endif // defined(_AMD64_) || defined(__i686__)
  85. #endif
  86. #include "inc/pal.h"
  87. #include "inc/rt/palrt.h"
  88. #include "inc/rt/no_sal2.h"
  89. #include "inc/rt/oaidl.h"
  90. #define _u(s) u##s
  91. typedef GUID UUID;
  92. #define INIT_PRIORITY(x) __attribute__((init_priority(x)))
  93. #ifdef PAL_STDCPP_COMPAT
  94. #define __in
  95. #define __out
  96. #define FILE PAL_FILE
  97. #endif
  98. #if defined(_AMD64_) || defined(__i686__)
  99. // xplat-todo: verify below is correct
  100. #include <cpuid.h>
  101. inline int get_cpuid(int cpuInfo[4], int function_id)
  102. {
  103. return __get_cpuid(
  104. static_cast<unsigned int>(function_id),
  105. reinterpret_cast<unsigned int*>(&cpuInfo[0]),
  106. reinterpret_cast<unsigned int*>(&cpuInfo[1]),
  107. reinterpret_cast<unsigned int*>(&cpuInfo[2]),
  108. reinterpret_cast<unsigned int*>(&cpuInfo[3]));
  109. }
  110. #elif defined(_ARM_)
  111. inline int get_cpuid(int cpuInfo[4], int function_id)
  112. {
  113. int empty[4] = {0};
  114. memcpy(cpuInfo, empty, sizeof(int) * 4);
  115. // xplat-todo: implement me!!
  116. }
  117. #endif
  118. inline void DebugBreak()
  119. {
  120. __builtin_trap();
  121. }
  122. #define EXIT_FAILURE 1
  123. #define _BitScanForward BitScanForward
  124. #define _BitScanForward64 BitScanForward64
  125. #define _BitScanReverse BitScanReverse
  126. #define _BitScanReverse64 BitScanReverse64
  127. #define _bittest BitTest
  128. #define _bittestandset BitTestAndSet
  129. #define _interlockedbittestandset InterlockedBitTestAndSet
  130. #define DbgRaiseAssertionFailure() __builtin_trap()
  131. // These are not available in pal
  132. #define fwprintf_s fwprintf
  133. // PAL LoadLibraryExW not supported
  134. #define LOAD_LIBRARY_SEARCH_SYSTEM32 0
  135. // winnt.h
  136. #define FAST_FAIL_INVALID_ARG 5
  137. // winerror.h
  138. #define FACILITY_JSCRIPT 2306
  139. #define JSCRIPT_E_CANTEXECUTE _HRESULT_TYPEDEF_(0x89020001L)
  140. #define DISP_E_UNKNOWNINTERFACE _HRESULT_TYPEDEF_(0x80020001L)
  141. #define DISP_E_MEMBERNOTFOUND _HRESULT_TYPEDEF_(0x80020003L)
  142. #define DISP_E_UNKNOWNNAME _HRESULT_TYPEDEF_(0x80020006L)
  143. #define DISP_E_NONAMEDARGS _HRESULT_TYPEDEF_(0x80020007L)
  144. #define DISP_E_EXCEPTION _HRESULT_TYPEDEF_(0x80020009L)
  145. #define DISP_E_BADINDEX _HRESULT_TYPEDEF_(0x8002000BL)
  146. #define DISP_E_UNKNOWNLCID _HRESULT_TYPEDEF_(0x8002000CL)
  147. #define DISP_E_ARRAYISLOCKED _HRESULT_TYPEDEF_(0x8002000DL)
  148. #define DISP_E_BADPARAMCOUNT _HRESULT_TYPEDEF_(0x8002000EL)
  149. #define DISP_E_PARAMNOTOPTIONAL _HRESULT_TYPEDEF_(0x8002000FL)
  150. #define DISP_E_NOTACOLLECTION _HRESULT_TYPEDEF_(0x80020011L)
  151. #define TYPE_E_DLLFUNCTIONNOTFOUND _HRESULT_TYPEDEF_(0x8002802FL)
  152. #define TYPE_E_TYPEMISMATCH _HRESULT_TYPEDEF_(0x80028CA0L)
  153. #define TYPE_E_OUTOFBOUNDS _HRESULT_TYPEDEF_(0x80028CA1L)
  154. #define TYPE_E_IOERROR _HRESULT_TYPEDEF_(0x80028CA2L)
  155. #define TYPE_E_CANTCREATETMPFILE _HRESULT_TYPEDEF_(0x80028CA3L)
  156. #define TYPE_E_CANTLOADLIBRARY _HRESULT_TYPEDEF_(0x80029C4AL)
  157. #define STG_E_TOOMANYOPENFILES _HRESULT_TYPEDEF_(0x80030004L)
  158. #define STG_E_ACCESSDENIED _HRESULT_TYPEDEF_(0x80030005L)
  159. #define STG_E_INSUFFICIENTMEMORY _HRESULT_TYPEDEF_(0x80030008L)
  160. #define STG_E_NOMOREFILES _HRESULT_TYPEDEF_(0x80030012L)
  161. #define STG_E_DISKISWRITEPROTECTED _HRESULT_TYPEDEF_(0x80030013L)
  162. #define STG_E_READFAULT _HRESULT_TYPEDEF_(0x8003001EL)
  163. #define STG_E_SHAREVIOLATION _HRESULT_TYPEDEF_(0x80030020L)
  164. #define STG_E_LOCKVIOLATION _HRESULT_TYPEDEF_(0x80030021L)
  165. #define STG_E_MEDIUMFULL _HRESULT_TYPEDEF_(0x80030070L)
  166. #define STG_E_INVALIDNAME _HRESULT_TYPEDEF_(0x800300FCL)
  167. #define STG_E_INUSE _HRESULT_TYPEDEF_(0x80030100L)
  168. #define STG_E_NOTCURRENT _HRESULT_TYPEDEF_(0x80030101L)
  169. #define STG_E_CANTSAVE _HRESULT_TYPEDEF_(0x80030103L)
  170. #define REGDB_E_CLASSNOTREG _HRESULT_TYPEDEF_(0x80040154L)
  171. #define MK_E_UNAVAILABLE _HRESULT_TYPEDEF_(0x800401E3L)
  172. #define MK_E_INVALIDEXTENSION _HRESULT_TYPEDEF_(0x800401E6L)
  173. #define MK_E_CANTOPENFILE _HRESULT_TYPEDEF_(0x800401EAL)
  174. #define CO_E_APPNOTFOUND _HRESULT_TYPEDEF_(0x800401F5L)
  175. #define CO_E_APPDIDNTREG _HRESULT_TYPEDEF_(0x800401FEL)
  176. #define GetScode(hr) ((SCODE) (hr))
  177. // activscp.h
  178. #define SCRIPT_E_RECORDED _HRESULT_TYPEDEF_(0x86664004L)
  179. #define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS (0x00000004)
  180. typedef
  181. enum tagBREAKPOINT_STATE
  182. {
  183. BREAKPOINT_DELETED = 0,
  184. BREAKPOINT_DISABLED = 1,
  185. BREAKPOINT_ENABLED = 2
  186. } BREAKPOINT_STATE;
  187. typedef
  188. enum tagBREAKRESUME_ACTION
  189. {
  190. BREAKRESUMEACTION_ABORT = 0,
  191. BREAKRESUMEACTION_CONTINUE = ( BREAKRESUMEACTION_ABORT + 1 ) ,
  192. BREAKRESUMEACTION_STEP_INTO = ( BREAKRESUMEACTION_CONTINUE + 1 ) ,
  193. BREAKRESUMEACTION_STEP_OVER = ( BREAKRESUMEACTION_STEP_INTO + 1 ) ,
  194. BREAKRESUMEACTION_STEP_OUT = ( BREAKRESUMEACTION_STEP_OVER + 1 ) ,
  195. BREAKRESUMEACTION_IGNORE = ( BREAKRESUMEACTION_STEP_OUT + 1 ) ,
  196. BREAKRESUMEACTION_STEP_DOCUMENT = ( BREAKRESUMEACTION_IGNORE + 1 )
  197. } BREAKRESUMEACTION;
  198. enum tagDBGPROP_ATTRIB_FLAGS
  199. {
  200. DBGPROP_ATTRIB_NO_ATTRIB= 0,
  201. DBGPROP_ATTRIB_VALUE_IS_INVALID= 0x8,
  202. DBGPROP_ATTRIB_VALUE_IS_EXPANDABLE= 0x10,
  203. DBGPROP_ATTRIB_VALUE_IS_FAKE= 0x20,
  204. DBGPROP_ATTRIB_VALUE_IS_METHOD= 0x100,
  205. DBGPROP_ATTRIB_VALUE_IS_EVENT= 0x200,
  206. DBGPROP_ATTRIB_VALUE_IS_RAW_STRING= 0x400,
  207. DBGPROP_ATTRIB_VALUE_READONLY= 0x800,
  208. DBGPROP_ATTRIB_ACCESS_PUBLIC= 0x1000,
  209. DBGPROP_ATTRIB_ACCESS_PRIVATE= 0x2000,
  210. DBGPROP_ATTRIB_ACCESS_PROTECTED= 0x4000,
  211. DBGPROP_ATTRIB_ACCESS_FINAL= 0x8000,
  212. DBGPROP_ATTRIB_STORAGE_GLOBAL= 0x10000,
  213. DBGPROP_ATTRIB_STORAGE_STATIC= 0x20000,
  214. DBGPROP_ATTRIB_STORAGE_FIELD= 0x40000,
  215. DBGPROP_ATTRIB_STORAGE_VIRTUAL= 0x80000,
  216. DBGPROP_ATTRIB_TYPE_IS_CONSTANT= 0x100000,
  217. DBGPROP_ATTRIB_TYPE_IS_SYNCHRONIZED= 0x200000,
  218. DBGPROP_ATTRIB_TYPE_IS_VOLATILE= 0x400000,
  219. DBGPROP_ATTRIB_HAS_EXTENDED_ATTRIBS= 0x800000,
  220. DBGPROP_ATTRIB_FRAME_INTRYBLOCK= 0x1000000,
  221. DBGPROP_ATTRIB_FRAME_INCATCHBLOCK= 0x2000000,
  222. DBGPROP_ATTRIB_FRAME_INFINALLYBLOCK= 0x4000000,
  223. DBGPROP_ATTRIB_VALUE_IS_RETURN_VALUE= 0x8000000
  224. };
  225. typedef DWORD DBGPROP_ATTRIB_FLAGS;
  226. enum tagDBGPROP_INFO_FLAgS
  227. {
  228. DBGPROP_INFO_NAME= 0x1,
  229. DBGPROP_INFO_TYPE= 0x2,
  230. DBGPROP_INFO_VALUE= 0x4,
  231. DBGPROP_INFO_FULLNAME= 0x20,
  232. DBGPROP_INFO_ATTRIBUTES= 0x8,
  233. DBGPROP_INFO_DEBUGPROP= 0x10,
  234. DBGPROP_INFO_BEAUTIFY= 0x2000000,
  235. DBGPROP_INFO_CALLTOSTRING= 0x4000000,
  236. DBGPROP_INFO_AUTOEXPAND= 0x800000
  237. };
  238. typedef DWORD DBGPROP_INFO_FLAGS;
  239. #define DBGPROP_INFO_STANDARD ((((DBGPROP_INFO_NAME | DBGPROP_INFO_TYPE) | DBGPROP_INFO_VALUE) | DBGPROP_INFO_ATTRIBUTES))
  240. #define DBGPROP_INFO_ALL ((((((DBGPROP_INFO_NAME | DBGPROP_INFO_TYPE ) | DBGPROP_INFO_VALUE) | DBGPROP_INFO_FULLNAME) | DBGPROP_INFO_ATTRIBUTES) | DBGPROP_INFO_DEBUGPROP))
  241. // _countof
  242. #if defined _M_X64 || defined _M_ARM || defined _M_ARM64
  243. #define _UNALIGNED __unaligned
  244. #else
  245. #define _UNALIGNED
  246. #endif
  247. #ifdef __cplusplus
  248. extern "C++"
  249. {
  250. template <typename _CountofType, size_t _SizeOfArray>
  251. char(*__countof_helper(_UNALIGNED _CountofType(&_Array)[_SizeOfArray]))[_SizeOfArray];
  252. #define __crt_countof(_Array) (sizeof(*__countof_helper(_Array)) + 0)
  253. }
  254. #else
  255. #define __crt_countof(_Array) (sizeof(_Array) / sizeof(_Array[0]))
  256. #endif
  257. #ifndef _countof
  258. #define _countof __crt_countof
  259. #endif
  260. // _countof
  261. #define ARRAYSIZE(A) _countof(A)
  262. //
  263. // Singly linked list structure. Can be used as either a list head, or
  264. // as link words.
  265. //
  266. typedef struct _SINGLE_LIST_ENTRY {
  267. struct _SINGLE_LIST_ENTRY *Next;
  268. } SINGLE_LIST_ENTRY, *PSINGLE_LIST_ENTRY;
  269. #if defined(_WIN64)
  270. //
  271. // The type SINGLE_LIST_ENTRY is not suitable for use with SLISTs. For
  272. // WIN64, an entry on an SLIST is required to be 16-byte aligned, while a
  273. // SINGLE_LIST_ENTRY structure has only 8 byte alignment.
  274. //
  275. // Therefore, all SLIST code should use the SLIST_ENTRY type instead of the
  276. // SINGLE_LIST_ENTRY type.
  277. //
  278. #pragma warning(push)
  279. #pragma warning(disable:4324) // structure padded due to align()
  280. typedef struct DECLSPEC_ALIGN(16) _SLIST_ENTRY {
  281. struct _SLIST_ENTRY *Next;
  282. } SLIST_ENTRY, *PSLIST_ENTRY;
  283. #pragma warning(pop)
  284. #else
  285. typedef struct _SINGLE_LIST_ENTRY SLIST_ENTRY, *PSLIST_ENTRY;
  286. #endif // _WIN64
  287. #if defined(_AMD64_)
  288. typedef union DECLSPEC_ALIGN(16) _SLIST_HEADER {
  289. struct { // original struct
  290. ULONGLONG Alignment;
  291. ULONGLONG Region;
  292. } DUMMYSTRUCTNAME;
  293. struct { // x64 16-byte header
  294. ULONGLONG Depth : 16;
  295. ULONGLONG Sequence : 48;
  296. ULONGLONG Reserved : 4;
  297. ULONGLONG NextEntry : 60; // last 4 bits are always 0's
  298. } HeaderX64;
  299. } SLIST_HEADER, *PSLIST_HEADER;
  300. #elif defined(_X86_)
  301. typedef union _SLIST_HEADER {
  302. ULONGLONG Alignment;
  303. struct {
  304. SLIST_ENTRY Next;
  305. WORD Depth;
  306. WORD CpuId;
  307. } DUMMYSTRUCTNAME;
  308. } SLIST_HEADER, *PSLIST_HEADER;
  309. #elif defined(_ARM_)
  310. typedef union _SLIST_HEADER {
  311. ULONGLONG Alignment;
  312. struct {
  313. SLIST_ENTRY Next;
  314. WORD Depth;
  315. WORD Reserved;
  316. } DUMMYSTRUCTNAME;
  317. } SLIST_HEADER, *PSLIST_HEADER;
  318. #endif
  319. PALIMPORT VOID PALAPI InitializeSListHead(IN OUT PSLIST_HEADER ListHead);
  320. PALIMPORT PSLIST_ENTRY PALAPI InterlockedPushEntrySList(IN OUT PSLIST_HEADER ListHead, IN OUT PSLIST_ENTRY ListEntry);
  321. PALIMPORT PSLIST_ENTRY PALAPI InterlockedPopEntrySList(IN OUT PSLIST_HEADER ListHead);
  322. template <class T>
  323. inline T InterlockedExchangeAdd(
  324. IN OUT T volatile *Addend,
  325. IN T Value)
  326. {
  327. return __sync_fetch_and_add(Addend, Value);
  328. }
  329. template <class T>
  330. inline T InterlockedExchangeSubtract(
  331. IN OUT T volatile *Addend,
  332. IN T Value)
  333. {
  334. return __sync_fetch_and_sub(Addend, Value);
  335. }
  336. template <class T>
  337. inline T InterlockedIncrement(
  338. IN OUT T volatile *Addend)
  339. {
  340. return __sync_add_and_fetch(Addend, T(1));
  341. }
  342. template <class T>
  343. inline T InterlockedDecrement(
  344. IN OUT T volatile *Addend)
  345. {
  346. return __sync_sub_and_fetch(Addend, T(1));
  347. }
  348. inline __int64 _abs64(__int64 n)
  349. {
  350. return n < 0 ? -n : n;
  351. }
  352. int GetCurrentThreadStackLimits(ULONG_PTR* lowLimit, ULONG_PTR* highLimit);
  353. bool IsAddressOnStack(ULONG_PTR address);
  354. errno_t rand_s(unsigned int* randomValue);
  355. inline char16* wmemset(char16* wcs, char16 wc, size_t n)
  356. {
  357. while (n)
  358. {
  359. wcs[--n] = wc;
  360. }
  361. return wcs;
  362. }
  363. inline errno_t wmemcpy_s(char16* dest, size_t destSize, const char16* src, size_t count)
  364. {
  365. return memcpy_s(dest, sizeof(char16) * destSize, src, sizeof(char16) * count);
  366. }
  367. inline int _wunlink(const char16* filename)
  368. {
  369. // WARN: does not set errno when fail
  370. return DeleteFile(filename) ? 0 : -1;
  371. }
  372. template <size_t size>
  373. inline errno_t _wcserror_s(char16 (&buffer)[size], int errnum)
  374. {
  375. const char* str = strerror(errnum);
  376. // WARN: does not return detail errno when fail
  377. return MultiByteToWideChar(CP_ACP, 0, str, -1, buffer, size) ? 0 : -1;
  378. }
  379. #define midl_user_allocate(size) \
  380. HeapAlloc(GetProcessHeap(), 0, (size))
  381. #define midl_user_free(ptr) \
  382. if (ptr != NULL) { HeapFree(GetProcessHeap(), NULL, ptr); }
  383. DWORD __cdecl CharLowerBuffW(const char16* lpsz, DWORD cchLength);
  384. DWORD __cdecl CharUpperBuffW(const char16* lpsz, DWORD cchLength);
  385. #define MAXUINT32 ((uint32_t)~((uint32_t)0))
  386. #define MAXINT32 ((int32_t)(MAXUINT32 >> 1))
  387. #define BYTE_MAX 0xff
  388. #define USHORT_MAX 0xffff
  389. #ifdef UNICODE
  390. #define StringCchPrintf StringCchPrintfW
  391. #endif
  392. #endif // _WIN32
  393. // Use intsafe.h for internal builds (currently missing some files with stdint.h)
  394. #if defined(_WIN32) && defined(NTBUILD)
  395. #define ENABLE_INTSAFE_SIGNED_FUNCTIONS 1
  396. #include <intsafe.h>
  397. #else
  398. #include <stdint.h>
  399. #endif
  400. #if defined(_MSC_VER) && !defined(__clang__)
  401. // ms-specific keywords
  402. #define _ABSTRACT abstract
  403. // MSVC2015 does not support C++11 semantics for `typename QualifiedName` declarations
  404. // outside of template code.
  405. #define _TYPENAME
  406. #else
  407. #define _ABSTRACT
  408. #define _TYPENAME typename
  409. #endif
  410. #if (defined(_MSC_VER) && _MSC_VER < 1900) || defined(NTBUILD)
  411. // "noexcept" not supported before VS 2015
  412. #define _NOEXCEPT_ throw()
  413. #else
  414. #define _NOEXCEPT_ noexcept
  415. #endif
  416. #ifdef _MSC_VER
  417. extern "C" PVOID _ReturnAddress(VOID);
  418. #pragma intrinsic(_ReturnAddress)
  419. extern "C" void * _AddressOfReturnAddress(void);
  420. #elif defined(__GNUC__) || defined(__clang__)
  421. #define _ReturnAddress() __builtin_return_address(0)
  422. #if !__has_builtin(_AddressOfReturnAddress)
  423. __forceinline void * _AddressOfReturnAddress()
  424. {
  425. return (void*)((char*) __builtin_frame_address(0) + sizeof(void*));
  426. }
  427. #endif
  428. #else
  429. #error _AddressOfReturnAddress and _ReturnAddress not defined for this platform
  430. #endif
  431. // Define strsafe related types and defines for non-VC++ compilers
  432. #ifndef _MSC_VER
  433. // xplat-todo: figure out why strsafe.h includes stdio etc
  434. // which prevents me from directly including PAL's strsafe.h
  435. #ifdef __cplusplus
  436. #define _STRSAFE_EXTERN_C extern "C"
  437. #else
  438. #define _STRSAFE_EXTERN_C extern
  439. #endif
  440. // If you do not want to use these functions inline (and instead want to link w/ strsafe.lib), then
  441. // #define STRSAFE_LIB before including this header file.
  442. #if defined(STRSAFE_LIB)
  443. #define STRSAFEAPI _STRSAFE_EXTERN_C HRESULT __stdcall
  444. #pragma comment(lib, "strsafe.lib")
  445. #elif defined(STRSAFE_LIB_IMPL)
  446. #define STRSAFEAPI _STRSAFE_EXTERN_C HRESULT __stdcall
  447. #else
  448. #define STRSAFEAPI inline HRESULT __stdcall
  449. #define STRSAFE_INLINE
  450. #endif
  451. STRSAFEAPI StringCchPrintfW(WCHAR* pszDest, size_t cchDest, const WCHAR* pszFormat, ...);
  452. STRSAFEAPI StringVPrintfWorkerW(WCHAR* pszDest, size_t cchDest, const WCHAR* pszFormat, va_list argList);
  453. #define STRSAFE_MAX_CCH 2147483647 // max # of characters we support (same as INT_MAX)
  454. // STRSAFE error return codes
  455. //
  456. #define STRSAFE_E_INSUFFICIENT_BUFFER ((HRESULT)0x8007007AL) // 0x7A = 122L = ERROR_INSUFFICIENT_BUFFER
  457. #define STRSAFE_E_INVALID_PARAMETER ((HRESULT)0x80070057L) // 0x57 = 87L = ERROR_INVALID_PARAMETER
  458. #define STRSAFE_E_END_OF_FILE ((HRESULT)0x80070026L) // 0x26 = 38L = ERROR_HANDLE_EOF
  459. #endif
  460. // Provide the definitions for non-windows platforms
  461. #ifndef _MSC_VER
  462. STRSAFEAPI StringVPrintfWorkerW(WCHAR* pszDest, size_t cchDest, const WCHAR* pszFormat, va_list argList)
  463. {
  464. HRESULT hr = S_OK;
  465. if (cchDest == 0)
  466. {
  467. // can not null terminate a zero-byte dest buffer
  468. hr = STRSAFE_E_INVALID_PARAMETER;
  469. }
  470. else
  471. {
  472. int iRet;
  473. size_t cchMax;
  474. // leave the last space for the null terminator
  475. cchMax = cchDest - 1;
  476. iRet = _vsnwprintf(pszDest, cchMax, pszFormat, argList);
  477. // ASSERT((iRet < 0) || (((size_t)iRet) <= cchMax));
  478. if ((iRet < 0) || (((size_t)iRet) > cchMax))
  479. {
  480. // need to null terminate the string
  481. pszDest += cchMax;
  482. *pszDest = _u('\0');
  483. // we have truncated pszDest
  484. hr = STRSAFE_E_INSUFFICIENT_BUFFER;
  485. }
  486. else if (((size_t)iRet) == cchMax)
  487. {
  488. // need to null terminate the string
  489. pszDest += cchMax;
  490. *pszDest = _u('\0');
  491. }
  492. }
  493. return hr;
  494. }
  495. STRSAFEAPI StringCchPrintfW(WCHAR* pszDest, size_t cchDest, const WCHAR* pszFormat, ...)
  496. {
  497. HRESULT hr;
  498. if (cchDest > STRSAFE_MAX_CCH)
  499. {
  500. hr = STRSAFE_E_INVALID_PARAMETER;
  501. }
  502. else
  503. {
  504. va_list argList;
  505. va_start(argList, pszFormat);
  506. hr = StringVPrintfWorkerW(pszDest, cchDest, pszFormat, argList);
  507. va_end(argList);
  508. }
  509. return hr;
  510. }
  511. #endif
  512. #ifndef _WIN32
  513. __inline
  514. HRESULT ULongMult(ULONG ulMultiplicand, ULONG ulMultiplier, ULONG* pulResult);
  515. #endif
  516. /* **** WARNING : finallyFunc is not allowed to raise exception *****
  517. * **** DO NOT ADD stack probe or memory allocations within the finallyFunc ****
  518. */
  519. template <class TryFunc, class FinallyFunc>
  520. void TryFinally(const TryFunc& tryFunc, const FinallyFunc& finallyFunc)
  521. {
  522. class FinallyObject
  523. {
  524. public:
  525. FinallyObject(const FinallyFunc& finallyFunc) : finallyFunc(finallyFunc), abnormalTermination(true) {}
  526. ~FinallyObject() { finallyFunc(abnormalTermination); }
  527. void SetHasNoAbnormalTermination() { abnormalTermination = false; }
  528. private:
  529. const FinallyFunc& finallyFunc;
  530. bool abnormalTermination;
  531. } finallyObject(finallyFunc);
  532. tryFunc();
  533. finallyObject.SetHasNoAbnormalTermination();
  534. }
  535. #ifdef DISABLE_SEH
  536. #define __TRY_FINALLY_BEGIN TryFinally([&]()
  537. #define __FINALLY , [&](bool /* hasException */)
  538. #define __TRY_FINALLY_END );
  539. #else
  540. #define __TRY_FINALLY_BEGIN __try
  541. #define __FINALLY __finally
  542. #define __TRY_FINALLY_END
  543. #endif
  544. namespace PlatformAgnostic
  545. {
  546. __forceinline unsigned char _BitTestAndSet(LONG *_BitBase, int _BitPos)
  547. {
  548. #if defined(__clang__) && !defined(_ARM_)
  549. // Clang doesn't expand _bittestandset intrinic to bts, and it's implemention also doesn't work for _BitPos >= 32
  550. unsigned char retval = 0;
  551. asm(
  552. "bts %[_BitPos], %[_BitBase]\n\t"
  553. "setc %b[retval]\n\t"
  554. : [_BitBase] "+m" (*_BitBase), [retval] "+rm" (retval)
  555. : [_BitPos] "ri" (_BitPos)
  556. : "cc" // clobber condition code
  557. );
  558. return retval;
  559. #else
  560. return _bittestandset(_BitBase, _BitPos);
  561. #endif
  562. }
  563. __forceinline unsigned char _BitTest(LONG *_BitBase, int _BitPos)
  564. {
  565. #if defined(__clang__) && !defined(_ARM_)
  566. // Clang doesn't expand _bittest intrinic to bt, and it's implemention also doesn't work for _BitPos >= 32
  567. unsigned char retval;
  568. asm(
  569. "bt %[_BitPos], %[_BitBase]\n\t"
  570. "setc %b[retval]\n\t"
  571. : [retval] "+rm" (retval)
  572. : [_BitPos] "ri" (_BitPos), [_BitBase] "m" (*_BitBase)
  573. : "cc" // clobber condition code
  574. );
  575. return retval;
  576. #else
  577. return _bittest(_BitBase, _BitPos);
  578. #endif
  579. }
  580. __forceinline unsigned char _InterlockedBitTestAndSet(volatile LONG *_BitBase, int _BitPos)
  581. {
  582. #if defined(__clang__) && !defined(_ARM_)
  583. // Clang doesn't expand _interlockedbittestandset intrinic to lock bts, and it's implemention also doesn't work for _BitPos >= 32
  584. unsigned char retval;
  585. asm(
  586. "lock bts %[_BitPos], %[_BitBase]\n\t"
  587. "setc %b[retval]\n\t"
  588. : [_BitBase] "+m" (*_BitBase), [retval] "+rm" (retval)
  589. : [_BitPos] "ri" (_BitPos)
  590. : "cc" // clobber condition code
  591. );
  592. return retval;
  593. #else
  594. return _interlockedbittestandset(_BitBase, _BitPos);
  595. #endif
  596. }
  597. __forceinline unsigned char _InterlockedBitTestAndReset(volatile LONG *_BitBase, int _BitPos)
  598. {
  599. #if defined(__clang__) && !defined(_ARM_)
  600. // Clang doesn't expand _interlockedbittestandset intrinic to lock btr, and it's implemention also doesn't work for _BitPos >= 32
  601. unsigned char retval;
  602. asm(
  603. "lock btr %[_BitPos], %[_BitBase]\n\t"
  604. "setc %b[retval]\n\t"
  605. : [_BitBase] "+m" (*_BitBase), [retval] "+rm" (retval)
  606. : [_BitPos] "ri" (_BitPos)
  607. : "cc" // clobber condition code
  608. );
  609. return retval;
  610. #elif !defined(__ANDROID__)
  611. return _interlockedbittestandreset(_BitBase, (long)_BitPos);
  612. #else
  613. // xplat-todo: Implement _interlockedbittestandreset for Android
  614. abort();
  615. #endif
  616. }
  617. };
  618. #include "PlatformAgnostic/DateTime.h"
  619. #include "PlatformAgnostic/Numbers.h"
  620. #include "PlatformAgnostic/SystemInfo.h"
  621. #include "PlatformAgnostic/Thread.h"
  622. #include "PlatformAgnostic/AssemblyCommon.h"
  623. #include "PlatformAgnostic/Debugger.h"