| 1 | #include <cstdlib> |
| 2 | |
| 3 | #include <basis/seadNew.h> |
| 4 | #include <heap/seadHeap.h> |
| 5 | #include <heap/seadHeapMgr.h> |
| 6 | |
| 7 | namespace sead |
| 8 | { |
| 9 | namespace system |
| 10 | { |
| 11 | void* NewImpl(Heap* heap, size_t size, s32 alignment, bool abortOnFailure) |
| 12 | { |
| 13 | if (!HeapMgr::sInstancePtr) |
| 14 | { |
| 15 | SEAD_WARN("alloced[%zu] before sead system initialize" , size); |
| 16 | return malloc(size); |
| 17 | } |
| 18 | |
| 19 | if (!heap) |
| 20 | { |
| 21 | heap = sead::HeapMgr::sInstancePtr->getCurrentHeap(); |
| 22 | if (!heap) |
| 23 | { |
| 24 | SEAD_ASSERT_MSG(false, "Current heap is null. Cannot alloc." ); |
| 25 | return nullptr; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | void* result = heap->tryAlloc(size, alignment); |
| 30 | if (!result && abortOnFailure) |
| 31 | { |
| 32 | SEAD_ASSERT_MSG( |
| 33 | false, "alloc failed. size: %zu, allocatable size: %zu, alignment: %d, heap: %s" , size, |
| 34 | heap->getMaxAllocatableSize(alignment), alignment, heap->getName().cstr()); |
| 35 | return nullptr; |
| 36 | } |
| 37 | return result; |
| 38 | } |
| 39 | |
| 40 | void DeleteImpl(void* ptr) |
| 41 | { |
| 42 | if (!sead::HeapMgr::sInstancePtr) |
| 43 | { |
| 44 | SEAD_WARN("free[0x%p] before sead system initialize" , ptr); |
| 45 | free(ptr); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | if (!ptr) |
| 50 | return; |
| 51 | |
| 52 | Heap* containHeap = sead::HeapMgr::sInstancePtr->findContainHeap(ptr); |
| 53 | if (containHeap) |
| 54 | containHeap->free(ptr); |
| 55 | else |
| 56 | SEAD_ASSERT_MSG(false, "delete bad pointer [0x%p]" , ptr); |
| 57 | } |
| 58 | } // namespace system |
| 59 | |
| 60 | #ifdef SEAD_DEBUG |
| 61 | void AllocFailAssert(Heap* heap, size_t size, u32 alignment) |
| 62 | { |
| 63 | if (!heap) |
| 64 | { |
| 65 | heap = HeapMgr::instance()->getCurrentHeap(); |
| 66 | SEAD_ASSERT_MSG(heap, "Current heap is null. Cannot alloc." ); |
| 67 | } |
| 68 | |
| 69 | SEAD_ASSERT_MSG(false, |
| 70 | "alloc failed. size: %zu, allocatable size: %zu, alignment: %u, heap: %s" , size, |
| 71 | heap->getMaxAllocatableSize(alignment), alignment, heap->getName().cstr()); |
| 72 | } |
| 73 | #endif |
| 74 | } // namespace sead |
| 75 | |
| 76 | // operator new(size_t) |
| 77 | |
| 78 | void* operator new(size_t size) |
| 79 | { |
| 80 | return sead::system::NewImpl(heap: nullptr, size, alignment: 8, abortOnFailure: true); |
| 81 | } |
| 82 | |
| 83 | void* operator new[](size_t size) |
| 84 | { |
| 85 | return sead::system::NewImpl(heap: nullptr, size, alignment: 8, abortOnFailure: true); |
| 86 | } |
| 87 | |
| 88 | void* operator new(size_t size, const std::nothrow_t&) noexcept |
| 89 | { |
| 90 | return sead::system::NewImpl(heap: nullptr, size, alignment: 8, abortOnFailure: false); |
| 91 | } |
| 92 | |
| 93 | void* operator new[](size_t size, const std::nothrow_t&) noexcept |
| 94 | { |
| 95 | return sead::system::NewImpl(heap: nullptr, size, alignment: 8, abortOnFailure: false); |
| 96 | } |
| 97 | |
| 98 | // operator new(size_t, s32 alignment) |
| 99 | |
| 100 | void* operator new(size_t size, s32 alignment) |
| 101 | { |
| 102 | return sead::system::NewImpl(heap: nullptr, size, alignment, abortOnFailure: true); |
| 103 | } |
| 104 | |
| 105 | void* operator new[](size_t size, s32 alignment) |
| 106 | { |
| 107 | return sead::system::NewImpl(heap: nullptr, size, alignment, abortOnFailure: true); |
| 108 | } |
| 109 | |
| 110 | void* operator new(size_t size, s32 alignment, const std::nothrow_t&) noexcept |
| 111 | { |
| 112 | return sead::system::NewImpl(heap: nullptr, size, alignment, abortOnFailure: false); |
| 113 | } |
| 114 | |
| 115 | void* operator new[](size_t size, s32 alignment, const std::nothrow_t&) noexcept |
| 116 | { |
| 117 | return sead::system::NewImpl(heap: nullptr, size, alignment, abortOnFailure: false); |
| 118 | } |
| 119 | |
| 120 | // operator new(size_t, sead::Heap*, s32 alignment) |
| 121 | |
| 122 | void* operator new(size_t size, sead::Heap* heap, s32 alignment) |
| 123 | { |
| 124 | return sead::system::NewImpl(heap, size, alignment, abortOnFailure: true); |
| 125 | } |
| 126 | |
| 127 | void* operator new[](size_t size, sead::Heap* heap, s32 alignment) |
| 128 | { |
| 129 | return sead::system::NewImpl(heap, size, alignment, abortOnFailure: true); |
| 130 | } |
| 131 | |
| 132 | void* operator new(size_t size, sead::Heap* heap, s32 alignment, const std::nothrow_t&) noexcept |
| 133 | { |
| 134 | return sead::system::NewImpl(heap, size, alignment, abortOnFailure: false); |
| 135 | } |
| 136 | |
| 137 | void* operator new[](size_t size, sead::Heap* heap, s32 alignment, const std::nothrow_t&) noexcept |
| 138 | { |
| 139 | return sead::system::NewImpl(heap, size, alignment, abortOnFailure: false); |
| 140 | } |
| 141 | |
| 142 | // operator new(size_t, sead::Heap*, const std::nothrow_t&) |
| 143 | |
| 144 | void* operator new(size_t size, sead::Heap* heap, const std::nothrow_t&) noexcept |
| 145 | { |
| 146 | return sead::system::NewImpl(heap, size, alignment: 8, abortOnFailure: false); |
| 147 | } |
| 148 | |
| 149 | void* operator new[](size_t size, sead::Heap* heap, const std::nothrow_t&) noexcept |
| 150 | { |
| 151 | return sead::system::NewImpl(heap, size, alignment: 8, abortOnFailure: false); |
| 152 | } |
| 153 | |
| 154 | // operator delete(void*) |
| 155 | |
| 156 | void operator delete(void* ptr) noexcept |
| 157 | { |
| 158 | sead::system::DeleteImpl(ptr); |
| 159 | } |
| 160 | |
| 161 | void operator delete[](void* ptr) noexcept |
| 162 | { |
| 163 | sead::system::DeleteImpl(ptr); |
| 164 | } |
| 165 | |
| 166 | void operator delete(void* ptr, const std::nothrow_t&) noexcept |
| 167 | { |
| 168 | sead::system::DeleteImpl(ptr); |
| 169 | } |
| 170 | |
| 171 | void operator delete[](void* ptr, const std::nothrow_t&) noexcept |
| 172 | { |
| 173 | sead::system::DeleteImpl(ptr); |
| 174 | } |
| 175 | |
| 176 | // operator delete(void*, s32) |
| 177 | |
| 178 | void operator delete(void* ptr, s32) |
| 179 | { |
| 180 | sead::system::DeleteImpl(ptr); |
| 181 | } |
| 182 | |
| 183 | void operator delete[](void* ptr, s32) |
| 184 | { |
| 185 | sead::system::DeleteImpl(ptr); |
| 186 | } |
| 187 | |
| 188 | void operator delete(void* ptr, s32, const std::nothrow_t&) noexcept |
| 189 | { |
| 190 | sead::system::DeleteImpl(ptr); |
| 191 | } |
| 192 | |
| 193 | void operator delete[](void* ptr, s32, const std::nothrow_t&) noexcept |
| 194 | { |
| 195 | sead::system::DeleteImpl(ptr); |
| 196 | } |
| 197 | |
| 198 | // operator delete(void*, sead::Heap*, const std::nothrow_t&) |
| 199 | |
| 200 | void operator delete(void* ptr, sead::Heap*, const std::nothrow_t&) noexcept |
| 201 | { |
| 202 | sead::system::DeleteImpl(ptr); |
| 203 | } |
| 204 | |
| 205 | void operator delete[](void* ptr, sead::Heap*, const std::nothrow_t&) noexcept |
| 206 | { |
| 207 | sead::system::DeleteImpl(ptr); |
| 208 | } |
| 209 | |
| 210 | // operator delete(void*, sead::Heap*, s32) |
| 211 | |
| 212 | void operator delete(void* ptr, sead::Heap*, s32) |
| 213 | { |
| 214 | sead::system::DeleteImpl(ptr); |
| 215 | } |
| 216 | |
| 217 | void operator delete[](void* ptr, sead::Heap*, s32) |
| 218 | { |
| 219 | sead::system::DeleteImpl(ptr); |
| 220 | } |
| 221 | |
| 222 | void operator delete(void* ptr, sead::Heap*, s32, const std::nothrow_t&) noexcept |
| 223 | { |
| 224 | sead::system::DeleteImpl(ptr); |
| 225 | } |
| 226 | |
| 227 | void operator delete[](void* ptr, sead::Heap*, s32, const std::nothrow_t&) noexcept |
| 228 | { |
| 229 | sead::system::DeleteImpl(ptr); |
| 230 | } |
| 231 | |