|
| 1 | +#pragma once |
| 2 | + |
| 3 | +namespace RE |
| 4 | +{ |
| 5 | + inline void* malloc(std::size_t a_size) |
| 6 | + { |
| 7 | + using func_t = decltype(&malloc); |
| 8 | + static REL::Relocation<func_t> func{ ID::malloc }; |
| 9 | + return func(a_size); |
| 10 | + } |
| 11 | + |
| 12 | + template <class T> |
| 13 | + inline T* malloc(std::size_t a_size) |
| 14 | + { |
| 15 | + return static_cast<T*>(malloc(a_size)); |
| 16 | + } |
| 17 | + |
| 18 | + template <class T> |
| 19 | + inline T* malloc() |
| 20 | + { |
| 21 | + return malloc<T>(sizeof(T)); |
| 22 | + } |
| 23 | + |
| 24 | + inline void* calloc(std::size_t a_num, std::size_t a_size) |
| 25 | + { |
| 26 | + const auto ret = malloc(a_num * a_size); |
| 27 | + if (ret) { |
| 28 | + std::memset(ret, 0, a_num * a_size); |
| 29 | + } |
| 30 | + return ret; |
| 31 | + } |
| 32 | + |
| 33 | + template <class T> |
| 34 | + inline T* calloc(std::size_t a_num, std::size_t a_size) |
| 35 | + { |
| 36 | + return static_cast<T*>(calloc(a_num, a_size)); |
| 37 | + } |
| 38 | + |
| 39 | + template <class T> |
| 40 | + inline T* calloc(std::size_t a_num) |
| 41 | + { |
| 42 | + return calloc<T>(a_num, sizeof(T)); |
| 43 | + } |
| 44 | + |
| 45 | + inline void free(void* a_ptr) |
| 46 | + { |
| 47 | + if (a_ptr) { |
| 48 | + using func_t = decltype(&free); |
| 49 | + static REL::Relocation<func_t> func{ ID::free }; |
| 50 | + func(a_ptr); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#define TES_HEAP_REDEFINE_NEW() \ |
| 56 | + [[nodiscard]] inline void* operator new(std::size_t a_count) \ |
| 57 | + { \ |
| 58 | + const auto mem = RE::malloc(a_count); \ |
| 59 | + if (mem) { \ |
| 60 | + return mem; \ |
| 61 | + } else { \ |
| 62 | + stl::report_and_fail("out of memory"sv); \ |
| 63 | + } \ |
| 64 | + } \ |
| 65 | + \ |
| 66 | + [[nodiscard]] inline void* operator new[](std::size_t a_count) \ |
| 67 | + { \ |
| 68 | + const auto mem = RE::malloc(a_count); \ |
| 69 | + if (mem) { \ |
| 70 | + return mem; \ |
| 71 | + } else { \ |
| 72 | + stl::report_and_fail("out of memory"sv); \ |
| 73 | + } \ |
| 74 | + } \ |
| 75 | + \ |
| 76 | + [[nodiscard]] constexpr void* operator new(std::size_t, void* a_ptr) noexcept { return a_ptr; } \ |
| 77 | + [[nodiscard]] constexpr void* operator new[](std::size_t, void* a_ptr) noexcept { return a_ptr; } \ |
| 78 | + [[nodiscard]] constexpr void* operator new(std::size_t, std::align_val_t, void* a_ptr) noexcept { return a_ptr; } \ |
| 79 | + [[nodiscard]] constexpr void* operator new[](std::size_t, std::align_val_t, void* a_ptr) noexcept { return a_ptr; } \ |
| 80 | + \ |
| 81 | + inline void operator delete(void* a_ptr) { RE::free(a_ptr); } \ |
| 82 | + inline void operator delete[](void* a_ptr) { RE::free(a_ptr); } \ |
| 83 | + inline void operator delete(void* a_ptr, std::size_t) { RE::free(a_ptr); } \ |
| 84 | + inline void operator delete[](void* a_ptr, std::size_t) { RE::free(a_ptr); } |
0 commit comments