HeapAllocator.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. #define HeapNew(T, ...) AllocatorNew(HeapAllocator, &HeapAllocator::Instance, T, __VA_ARGS__)
  6. #define HeapNewZ(T, ...) AllocatorNewZ(HeapAllocator, &HeapAllocator::Instance, T, __VA_ARGS__)
  7. #define HeapNewPlus(size, T, ...) AllocatorNewPlus(HeapAllocator, &HeapAllocator::Instance, size, T, __VA_ARGS__)
  8. #define HeapNewPlusZ(size, T, ...) AllocatorNewPlusZ(HeapAllocator, &HeapAllocator::Instance, size, T, __VA_ARGS__)
  9. #define HeapNewStruct(T) AllocatorNewStruct(HeapAllocator, &HeapAllocator::Instance, T);
  10. #define HeapNewStructZ(T) AllocatorNewStructZ(HeapAllocator, &HeapAllocator::Instance, T);
  11. #define HeapNewStructPlus(size, T, ...) AllocatorNewStructPlus(HeapAllocator, &HeapAllocator::Instance, size, T)
  12. #define HeapNewStructPlusZ(size, T, ...) AllocatorNewStructPlusZ(HeapAllocator, &HeapAllocator::Instance, size, T)
  13. #define HeapNewArray(T, count, ...) AllocatorNewArray(HeapAllocator, &HeapAllocator::Instance, T, count)
  14. #define HeapNewArrayZ(T, count, ...) AllocatorNewArrayZ(HeapAllocator, &HeapAllocator::Instance, T, count)
  15. #define HeapDelete(obj, ...) \
  16. AllocatorDelete(HeapAllocator, &HeapAllocator::Instance, obj, ##__VA_ARGS__)
  17. #define HeapDeletePlus(size, obj, ...) \
  18. AllocatorDeletePlus(HeapAllocator, &HeapAllocator::Instance, size, obj, ##__VA_ARGS__)
  19. #define HeapDeletePlusPrefix(size, obj, ...) \
  20. AllocatorDeletePlusPrefix(HeapAllocator, &HeapAllocator::Instance, size, obj, ##__VA_ARGS__)
  21. #define HeapDeleteArray(count, obj) AllocatorDeleteArray(HeapAllocator, &HeapAllocator::Instance, count, obj)
  22. #define HeapNewNoThrow(T, ...) AllocatorNewNoThrow(HeapAllocator, &HeapAllocator::Instance, T, __VA_ARGS__)
  23. #define HeapNewNoThrowZ(T, ...) AllocatorNewNoThrowZ(HeapAllocator, &HeapAllocator::Instance, T, __VA_ARGS__)
  24. #define HeapNewNoThrowPlus(size, T, ...) AllocatorNewNoThrowPlus(HeapAllocator, &HeapAllocator::Instance, size, T, __VA_ARGS__)
  25. #define HeapNewNoThrowPlusZ(size, T, ...) AllocatorNewNoThrowPlusZ(HeapAllocator, &HeapAllocator::Instance, size, T, __VA_ARGS__)
  26. #define HeapNewNoThrowPlusPrefixZ(size, T, ...) AllocatorNewNoThrowPlusPrefixZ(HeapAllocator, &HeapAllocator::Instance, size, T, __VA_ARGS__)
  27. #define HeapNewNoThrowStruct(T) AllocatorNewNoThrowStruct(HeapAllocator, &HeapAllocator::Instance, T)
  28. #define HeapNewNoThrowStructZ(T) AllocatorNewNoThrowStructZ(HeapAllocator, &HeapAllocator::Instance, T)
  29. #define HeapNewNoThrowArray(T, count, ...) AllocatorNewNoThrowArray(HeapAllocator, &HeapAllocator::Instance, T, count)
  30. #define HeapNewNoThrowArrayZ(T, count, ...) AllocatorNewNoThrowArrayZ(HeapAllocator, &HeapAllocator::Instance, T, count)
  31. #define NoMemProtectHeapNewNoThrow(T, ...) AllocatorNewNoThrow(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), T, __VA_ARGS__)
  32. #define NoMemProtectHeapNewNoThrowZ(T, ...) AllocatorNewNoThrowZ(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), T, __VA_ARGS__)
  33. #define NoMemProtectHeapNewNoThrowPlus(size, T, ...) AllocatorNewNoThrowPlus(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), size, T, __VA_ARGS__)
  34. #define NoMemProtectHeapNewNoThrowPlusZ(size, T, ...) AllocatorNewNoThrowPlusZ(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), size, T, __VA_ARGS__)
  35. #define NoMemProtectHeapNewNoThrowPlusPrefixZ(size, T, ...) AllocatorNewNoThrowPlusPrefixZ(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), size, T, __VA_ARGS__)
  36. #define NoMemProtectHeapNewNoThrowStruct(T) AllocatorNewNoThrowStruct(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), T)
  37. #define NoMemProtectHeapNewNoThrowStructZ(T) AllocatorNewNoThrowStructZ(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), T)
  38. #define NoMemProtectHeapNewNoThrowArray(T, count, ...) AllocatorNewNoThrowArray(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), T, count)
  39. #define NoMemProtectHeapNewNoThrowArrayZ(T, count, ...) AllocatorNewNoThrowArrayZ(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), T, count)
  40. #define NoMemProtectHeapDelete(obj) AllocatorDelete(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), obj)
  41. #define NoMemProtectHeapDeletePlus(size, obj) AllocatorDeletePlus(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), size, obj)
  42. #define NoMemProtectHeapDeletePlusPrefix(size, obj) AllocatorDeletePlusPrefix(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), size, obj)
  43. #define NoMemProtectHeapDeleteArray(count, obj) AllocatorDeleteArray(HeapAllocator, HeapAllocator::GetNoMemProtectInstance(), count, obj)
  44. #define NoCheckHeapNew(T, ...) AllocatorNew(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, T, __VA_ARGS__)
  45. #define NoCheckHeapNewZ(T, ...) AllocatorNewZ(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, T, __VA_ARGS__)
  46. #define NoCheckHeapNewPlus(size, T, ...) AllocatorNewPlus(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, size, T, __VA_ARGS__)
  47. #define NoCheckHeapNewPlusZ(size, T, ...) AllocatorNewPlusZ(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, size, T, __VA_ARGS__)
  48. #define NoCheckHeapNewStruct(T) AllocatorNewStruct(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, T)
  49. #define NoCheckHeapNewStructZ(T) AllocatorNewStructZ(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, T)
  50. #define NoCheckHeapNewArray(T, count, ...) AllocatorNewArray(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, T, count)
  51. #define NoCheckHeapNewArrayZ(T, count, ...) AllocatorNewArrayZ(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, T, count)
  52. #define NoCheckHeapDelete(obj) AllocatorDelete(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, obj)
  53. #define NoCheckHeapDeletePlus(size, obj) AllocatorDeletePlus(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, size, obj)
  54. #define NoCheckHeapDeleteArray(count, obj) AllocatorDeleteArray(NoCheckHeapAllocator, &NoCheckHeapAllocator::Instance, count, obj)
  55. namespace Memory
  56. {
  57. #ifdef HEAP_TRACK_ALLOC
  58. struct HeapAllocatorData;
  59. struct HeapAllocRecord
  60. {
  61. HeapAllocRecord * prev;
  62. HeapAllocRecord * next;
  63. size_t allocId;
  64. size_t size;
  65. TrackAllocData allocData;
  66. HeapAllocatorData* data;
  67. #if defined(CHECK_MEMORY_LEAK) || defined(LEAK_REPORT)
  68. #ifdef STACK_BACK_TRACE
  69. StackBackTrace * stacktrace;
  70. #endif
  71. #endif
  72. };
  73. struct HeapAllocatorData
  74. {
  75. void LogAlloc(HeapAllocRecord * record, size_t requestedBytes, TrackAllocData const& data);
  76. void LogFree(HeapAllocRecord * record);
  77. bool CheckLeaks();
  78. HeapAllocRecord * head;
  79. size_t allocCount;
  80. size_t deleteCount;
  81. size_t outstandingBytes;
  82. static uint const StackTraceDepth = 10;
  83. };
  84. #endif
  85. struct HeapAllocator
  86. {
  87. template<typename T>
  88. struct AutoFree
  89. {
  90. private:
  91. T * obj = nullptr;
  92. public:
  93. ~AutoFree()
  94. {
  95. HeapDelete(obj);
  96. }
  97. void Set(T* obj)
  98. {
  99. Assert(this->obj == nullptr);
  100. this->obj = obj;
  101. }
  102. void Release()
  103. {
  104. this->obj = nullptr;
  105. }
  106. T* Get() const
  107. {
  108. return this->obj;
  109. }
  110. };
  111. template<typename T>
  112. struct AutoFreeArray
  113. {
  114. private:
  115. T* obj = nullptr;
  116. size_t count = 0;
  117. public:
  118. ~AutoFreeArray()
  119. {
  120. HeapDeleteArray(count, obj);
  121. }
  122. void Set(__ecount(count) T* obj, size_t count)
  123. {
  124. Assert(this->obj == nullptr);
  125. this->obj = obj;
  126. this->count = count;
  127. }
  128. void Release()
  129. {
  130. this->obj = nullptr;
  131. this->count = 0;
  132. }
  133. T* Get() const
  134. {
  135. return this->obj;
  136. }
  137. };
  138. static const bool FakeZeroLengthArray = false;
  139. char * Alloc(DECLSPEC_GUARD_OVERFLOW size_t byteSize)
  140. {
  141. return AllocT<false>(byteSize);
  142. }
  143. template <bool noThrow>
  144. char * AllocT(DECLSPEC_GUARD_OVERFLOW size_t byteSize);
  145. // This exists solely to make the AllocateXXX macros more polymorphic
  146. char * AllocLeaf(DECLSPEC_GUARD_OVERFLOW size_t byteSize)
  147. {
  148. return Alloc(byteSize);
  149. }
  150. char * NoThrowAlloc(DECLSPEC_GUARD_OVERFLOW size_t byteSize)
  151. {
  152. return AllocT<true>(byteSize);
  153. }
  154. char * AllocZero(DECLSPEC_GUARD_OVERFLOW size_t byteSize)
  155. {
  156. char * buffer = Alloc(byteSize);
  157. memset(buffer, 0, byteSize);
  158. return buffer;
  159. }
  160. char * NoThrowAllocZero(DECLSPEC_GUARD_OVERFLOW size_t byteSize)
  161. {
  162. char * buffer = NoThrowAlloc(byteSize);
  163. if (buffer != nullptr)
  164. {
  165. memset(buffer, 0, byteSize);
  166. }
  167. return buffer;
  168. }
  169. void Free(void * buffer, size_t byteSize);
  170. static HeapAllocator Instance;
  171. static HeapAllocator * GetNoMemProtectInstance();
  172. private:
  173. HANDLE m_privateHeap;
  174. void InitPrivateHeap();
  175. void DestroyPrivateHeap();
  176. HANDLE GetPrivateHeap();
  177. public:
  178. #ifdef TRACK_ALLOC
  179. // Doesn't support tracking information, dummy implementation
  180. HeapAllocator * TrackAllocInfo(TrackAllocData const& data);
  181. void ClearTrackAllocInfo(TrackAllocData* data = NULL);
  182. #ifdef HEAP_TRACK_ALLOC
  183. static void InitializeThread()
  184. {
  185. memset(&nextAllocData, 0, sizeof(nextAllocData));
  186. }
  187. static bool CheckLeaks();
  188. THREAD_LOCAL static TrackAllocData nextAllocData;
  189. HeapAllocatorData data;
  190. static CriticalSection cs;
  191. #endif // HEAP_TRACK_ALLOC
  192. #endif // TRACK_ALLOC
  193. HeapAllocator(bool useAllocMemProtect = true);
  194. ~HeapAllocator();
  195. #ifdef INTERNAL_MEM_PROTECT_HEAP_ALLOC
  196. void FinishMemProtectHeapCollect();
  197. private:
  198. bool DoUseMemProtectHeap();
  199. static HeapAllocator NoMemProtectInstance;
  200. #if DBG
  201. bool isUsed;
  202. bool allocMemProtect;
  203. void * memProtectHeapHandle;
  204. #endif // DBG
  205. #endif // INTERNAL_MEM_PROTECT_HEAP_ALLOC
  206. }; // HeapAllocator.
  207. class NoThrowHeapAllocator
  208. {
  209. public:
  210. static const bool FakeZeroLengthArray = false;
  211. char * Alloc(DECLSPEC_GUARD_OVERFLOW size_t byteSize);
  212. char * AllocZero(DECLSPEC_GUARD_OVERFLOW size_t byteSize);
  213. void Free(void * buffer, size_t byteSize);
  214. char * NoThrowAlloc(DECLSPEC_GUARD_OVERFLOW size_t byteSize)
  215. {
  216. return Alloc(byteSize);
  217. }
  218. char * NoThrowAllocZero(DECLSPEC_GUARD_OVERFLOW size_t byteSize)
  219. {
  220. return AllocZero(byteSize);
  221. }
  222. static NoThrowHeapAllocator Instance;
  223. #ifdef TRACK_ALLOC
  224. // Doesn't support tracking information, dummy implementation
  225. NoThrowHeapAllocator * TrackAllocInfo(TrackAllocData const& data);
  226. void ClearTrackAllocInfo(TrackAllocData* data = NULL);
  227. #endif
  228. };
  229. #ifdef INTERNAL_MEM_PROTECT_HEAP_ALLOC
  230. class NoThrowNoMemProtectHeapAllocator
  231. {
  232. public:
  233. static const bool FakeZeroLengthArray = false;
  234. char * Alloc(DECLSPEC_GUARD_OVERFLOW size_t byteSize);
  235. char * AllocZero(DECLSPEC_GUARD_OVERFLOW size_t byteSize);
  236. void Free(void * buffer, size_t byteSize);
  237. static NoThrowNoMemProtectHeapAllocator Instance;
  238. #ifdef TRACK_ALLOC
  239. // Doesn't support tracking information, dummy implementation
  240. NoThrowNoMemProtectHeapAllocator * TrackAllocInfo(TrackAllocData const& data);
  241. void ClearTrackAllocInfo(TrackAllocData* data = NULL);
  242. #endif
  243. };
  244. #endif
  245. class NoCheckHeapAllocator
  246. {
  247. public:
  248. static const bool FakeZeroLengthArray = false;
  249. char * Alloc(DECLSPEC_GUARD_OVERFLOW size_t byteSize)
  250. {
  251. if (processHeap == NULL)
  252. {
  253. processHeap = GetProcessHeap();
  254. }
  255. char * buffer = (char*)HeapAlloc(processHeap, 0, byteSize);
  256. if (buffer == nullptr)
  257. {
  258. // NoCheck heap allocator is only used by debug only code, and if we fail to allocate
  259. // memory, we will just raise an exception and kill the process
  260. DebugHeap_OOM_fatal_error();
  261. }
  262. return buffer;
  263. }
  264. char * AllocZero(DECLSPEC_GUARD_OVERFLOW size_t byteSize)
  265. {
  266. if (processHeap == NULL)
  267. {
  268. processHeap = GetProcessHeap();
  269. }
  270. char * buffer = (char*)HeapAlloc(processHeap, HEAP_ZERO_MEMORY, byteSize);
  271. if (buffer == nullptr)
  272. {
  273. // NoCheck heap allocator is only used by debug only code, and if we fail to allocate
  274. // memory, we will just raise an exception and kill the process
  275. DebugHeap_OOM_fatal_error();
  276. }
  277. return buffer;
  278. }
  279. void Free(void * buffer, size_t byteSize)
  280. {
  281. Assert(processHeap != NULL);
  282. HeapFree(processHeap, 0, buffer);
  283. }
  284. #ifdef TRACK_ALLOC
  285. // Doesn't support tracking information, dummy implementation
  286. NoCheckHeapAllocator * TrackAllocInfo(TrackAllocData const& data) { return this; }
  287. void ClearTrackAllocInfo(TrackAllocData* data = NULL) {}
  288. #endif
  289. static NoCheckHeapAllocator Instance;
  290. static HANDLE processHeap;
  291. };
  292. #ifdef CHECK_MEMORY_LEAK
  293. class MemoryLeakCheck
  294. {
  295. public:
  296. MemoryLeakCheck() : head(NULL), tail(NULL), leakedBytes(0), leakedCount(0), enableOutput(true) {}
  297. ~MemoryLeakCheck();
  298. static void AddLeakDump(char16 const * dump, size_t bytes, size_t count);
  299. static void SetEnableOutput(bool flag) { leakCheck.enableOutput = flag; }
  300. static bool IsEnableOutput() { return leakCheck.enableOutput; }
  301. private:
  302. static MemoryLeakCheck leakCheck;
  303. struct LeakRecord
  304. {
  305. char16 const * dump;
  306. LeakRecord * next;
  307. };
  308. CriticalSection cs;
  309. LeakRecord * head;
  310. LeakRecord * tail;
  311. size_t leakedBytes;
  312. size_t leakedCount;
  313. bool enableOutput;
  314. };
  315. #endif
  316. } // namespace Memory
  317. #ifdef INTERNAL_MEM_PROTECT_HEAP_ALLOC
  318. //----------------------------------------
  319. // NoThrowNoMemProtectHeapAllocator overrides
  320. //----------------------------------------
  321. template <>
  322. _Ret_maybenull_ inline void * __cdecl
  323. operator new(DECLSPEC_GUARD_OVERFLOW size_t byteSize, NoThrowNoMemProtectHeapAllocator * alloc, char * (NoThrowNoMemProtectHeapAllocator::*AllocFunc)(size_t))
  324. {
  325. return ::operator new(byteSize, alloc, true, AllocFunc);
  326. }
  327. template <>
  328. _Ret_maybenull_ inline void * __cdecl
  329. operator new[](DECLSPEC_GUARD_OVERFLOW size_t byteSize, NoThrowNoMemProtectHeapAllocator * alloc, char * (NoThrowNoMemProtectHeapAllocator::*AllocFunc)(size_t))
  330. {
  331. return ::operator new[](byteSize, alloc, true, AllocFunc);
  332. }
  333. template <>
  334. _Ret_maybenull_ inline void * __cdecl
  335. operator new(DECLSPEC_GUARD_OVERFLOW size_t byteSize, NoThrowNoMemProtectHeapAllocator * alloc, char * (NoThrowNoMemProtectHeapAllocator::*AllocFunc)(size_t), DECLSPEC_GUARD_OVERFLOW size_t plusSize)
  336. {
  337. return ::operator new(byteSize, alloc, true, AllocFunc, plusSize);
  338. }
  339. inline void __cdecl
  340. operator delete(void * obj, NoThrowNoMemProtectHeapAllocator * alloc, char * (NoThrowNoMemProtectHeapAllocator::*AllocFunc)(size_t))
  341. {
  342. alloc->Free(obj, (size_t)-1);
  343. }
  344. inline void __cdecl
  345. operator delete(void * obj, NoThrowNoMemProtectHeapAllocator * alloc, char * (NoThrowNoMemProtectHeapAllocator::*AllocFunc)(size_t), size_t plusSize)
  346. {
  347. alloc->Free(obj, (size_t)-1);
  348. }
  349. #else
  350. typedef NoThrowHeapAllocator NoThrowNoMemProtectHeapAllocator;
  351. #endif
  352. //----------------------------------------
  353. // Default operator new/delete overrides
  354. //----------------------------------------
  355. #if defined(NTBUILD) && !defined(USED_IN_STATIC_LIB)
  356. _Ret_maybenull_ void * __cdecl operator new(DECLSPEC_GUARD_OVERFLOW size_t byteSize);
  357. _Ret_maybenull_ void * __cdecl operator new[](DECLSPEC_GUARD_OVERFLOW size_t byteSize);
  358. #endif
  359. //----------------------------------------
  360. // HeapAllocator overrides
  361. //----------------------------------------
  362. inline void __cdecl
  363. operator delete(void * obj, HeapAllocator * alloc, char * (HeapAllocator::*AllocFunc)(size_t))
  364. {
  365. alloc->Free(obj, (size_t)-1);
  366. }
  367. inline void __cdecl
  368. operator delete(void * obj, HeapAllocator * alloc, char * (HeapAllocator::*AllocFunc)(size_t), size_t plusSize)
  369. {
  370. alloc->Free(obj, (size_t)-1);
  371. }
  372. //----------------------------------------
  373. // NoThrowHeapAllocator overrides
  374. //----------------------------------------
  375. template <>
  376. _Ret_maybenull_ inline void * __cdecl
  377. operator new(DECLSPEC_GUARD_OVERFLOW size_t byteSize, NoThrowHeapAllocator * alloc, char * (NoThrowHeapAllocator::*AllocFunc)(size_t))
  378. {
  379. return ::operator new(byteSize, alloc, true, AllocFunc);
  380. }
  381. template <>
  382. _Ret_maybenull_ inline void * __cdecl
  383. operator new[](DECLSPEC_GUARD_OVERFLOW size_t byteSize, NoThrowHeapAllocator * alloc, char * (NoThrowHeapAllocator::*AllocFunc)(size_t))
  384. {
  385. return ::operator new[](byteSize, alloc, true, AllocFunc);
  386. }
  387. template <>
  388. _Ret_maybenull_ inline void * __cdecl
  389. operator new(DECLSPEC_GUARD_OVERFLOW size_t byteSize, NoThrowHeapAllocator * alloc, char * (NoThrowHeapAllocator::*AllocFunc)(size_t), size_t plusSize)
  390. {
  391. return ::operator new(byteSize, alloc, true, AllocFunc, plusSize);
  392. }
  393. inline void __cdecl
  394. operator delete(void * obj, NoThrowHeapAllocator * alloc, char * (NoThrowHeapAllocator::*AllocFunc)(size_t))
  395. {
  396. alloc->Free(obj, (size_t)-1);
  397. }
  398. inline void __cdecl
  399. operator delete(void * obj, NoThrowHeapAllocator * alloc, char * (NoThrowHeapAllocator::*AllocFunc)(size_t), size_t plusSize)
  400. {
  401. alloc->Free(obj, (size_t)-1);
  402. }
  403. template <>
  404. _Ret_notnull_ inline void * __cdecl
  405. operator new(DECLSPEC_GUARD_OVERFLOW size_t byteSize, NoCheckHeapAllocator * alloc, char * (NoCheckHeapAllocator::*AllocFunc)(size_t))
  406. {
  407. Assert(byteSize != 0);
  408. void * buffer = (alloc->*AllocFunc)(byteSize);
  409. return buffer;
  410. }
  411. template <>
  412. _Ret_notnull_ inline void * __cdecl
  413. operator new(DECLSPEC_GUARD_OVERFLOW size_t byteSize, NoCheckHeapAllocator * alloc, char * (NoCheckHeapAllocator::*AllocFunc)(size_t), DECLSPEC_GUARD_OVERFLOW size_t plusSize)
  414. {
  415. Assert(byteSize != 0);
  416. Assert(plusSize != 0);
  417. void * buffer = (alloc->*AllocFunc)(AllocSizeMath::Add(byteSize, plusSize));
  418. return buffer;
  419. }
  420. _Ret_notnull_ inline void * __cdecl
  421. operator new[](DECLSPEC_GUARD_OVERFLOW size_t byteSize, NoCheckHeapAllocator * alloc, char * (NoCheckHeapAllocator::*AllocFunc)(size_t))
  422. {
  423. void * buffer = (alloc->*AllocFunc)(byteSize);
  424. return buffer;
  425. }
  426. inline void __cdecl
  427. operator delete(void * obj, NoCheckHeapAllocator * alloc, char * (NoCheckHeapAllocator::*AllocFunc)(size_t))
  428. {
  429. alloc->Free(obj, (size_t)-1);
  430. }
  431. inline void __cdecl
  432. operator delete(void * obj, NoCheckHeapAllocator * alloc, char * (NoCheckHeapAllocator::*AllocFunc)(size_t), size_t plusSize)
  433. {
  434. alloc->Free(obj, (size_t)-1);
  435. }
  436. // Free routine where we don't care about following C++ semantics (e.g. calling the destructor)
  437. inline void HeapFree(void* obj, size_t size)
  438. {
  439. AllocatorFree(&HeapAllocator::Instance, &HeapAllocator::Free, obj, size);
  440. }