mirror of
https://github.com/thunderbrewhq/squall.git
synced 2026-02-04 00:49:08 +00:00
feat(memory): replace global new and delete operators
This commit is contained in:
parent
20457f7d4c
commit
f75d9caba3
2 changed files with 38 additions and 0 deletions
|
|
@ -4,6 +4,26 @@
|
||||||
|
|
||||||
constexpr size_t ALIGNMENT = 8;
|
constexpr size_t ALIGNMENT = 8;
|
||||||
|
|
||||||
|
void* operator new(size_t bytes) {
|
||||||
|
return SMemAlloc(bytes, "new", -1, 0x0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* operator new[](size_t bytes) {
|
||||||
|
return SMemAlloc(bytes, "new[]", -1, 0x0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void* ptr) noexcept {
|
||||||
|
if (ptr) {
|
||||||
|
SMemFree(ptr, "delete", -1, 0x0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete[](void* ptr) noexcept {
|
||||||
|
if (ptr) {
|
||||||
|
SMemFree(ptr, "delete[]", -1, 0x0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void* SMemAlloc(size_t bytes, const char* filename, int32_t linenumber, uint32_t flags) {
|
void* SMemAlloc(size_t bytes, const char* filename, int32_t linenumber, uint32_t flags) {
|
||||||
size_t alignedBytes = (bytes + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1);
|
size_t alignedBytes = (bytes + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,24 @@
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
TEST_CASE("operator new", "[memory]") {
|
||||||
|
SECTION("allocates memory") {
|
||||||
|
auto ptr = new int32_t;
|
||||||
|
REQUIRE(ptr != nullptr);
|
||||||
|
|
||||||
|
delete ptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("operator new[]", "[memory]") {
|
||||||
|
SECTION("allocates memory") {
|
||||||
|
auto ptr = new int32_t[10];
|
||||||
|
REQUIRE(ptr != nullptr);
|
||||||
|
|
||||||
|
delete[] ptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("SMemAlloc", "[memory]") {
|
TEST_CASE("SMemAlloc", "[memory]") {
|
||||||
SECTION("allocates memory") {
|
SECTION("allocates memory") {
|
||||||
void* ptr = SMemAlloc(16, __FILE__, __LINE__);
|
void* ptr = SMemAlloc(16, __FILE__, __LINE__);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue