feat(memory): add STORM_FREE macro

This commit is contained in:
Adam Heinermann 2025-09-12 21:53:57 -07:00 committed by fallenoak
parent 05f8a7eebf
commit 547fc6d4f0
6 changed files with 30 additions and 8 deletions

View file

@ -57,7 +57,7 @@ void DeleteIdHashTable(_IDHASHTABLE* pTable) {
delete pEntry;
}
}
SMemFree(pTable->data, __FILE__, __LINE__, 0);
STORM_FREE(pTable->data);
delete pTable;
}
@ -305,7 +305,7 @@ int32_t SEvtRegisterHandler(uint32_t type, uint32_t subtype, uint32_t id, uint32
}
}
if (s_typehashtable) {
SMemFree(s_typehashtable, __FILE__, __LINE__, 0);
STORM_FREE(s_typehashtable);
}
}
s_typehashtable = pNewTable;
@ -350,9 +350,9 @@ int32_t SEvtRegisterHandler(uint32_t type, uint32_t subtype, uint32_t id, uint32
}
}
SMemFree(pTempTable, __FILE__, __LINE__, 0);
STORM_FREE(pTempTable);
if (pTypeHash->idhashtable->data) {
SMemFree(pTypeHash->idhashtable->data, __FILE__, __LINE__, 0);
STORM_FREE(pTypeHash->idhashtable->data);
}
pTypeHash->idhashtable->data = pNewTable;
pTypeHash->idhashtable->size = newsize;

View file

@ -13,6 +13,9 @@
#define STORM_ALLOC_ZERO(bytes) \
SMemAlloc(bytes, __FILE__, __LINE__, SMEM_FLAG_ZEROMEMORY)
#define STORM_FREE(ptr) \
SMemFree(ptr, __FILE__, __LINE__, 0x0)
#define STORM_NEW(t) \
new(SMemAlloc(sizeof(t), __FILE__, __LINE__, 0x0)) t

View file

@ -37,14 +37,14 @@ HASHKEY_STR::HASHKEY_STR(const char* str) {
HASHKEY_STR::~HASHKEY_STR() {
if (this->m_str) {
SMemFree(this->m_str, __FILE__, __LINE__);
STORM_FREE(this->m_str);
}
}
HASHKEY_STR& HASHKEY_STR::operator=(const char* str) {
if (this->m_str != str) {
if (this->m_str) {
SMemFree(this->m_str, __FILE__, __LINE__);
STORM_FREE(this->m_str);
}
this->m_str = SStrDupA(str, __FILE__, __LINE__);

View file

@ -137,7 +137,7 @@ void TSHashTable<T, TKey>::InternalClear(int32_t warn) {
template <class T, class TKey>
void TSHashTable<T, TKey>::InternalDelete(T* ptr) {
ptr->~T();
SMemFree(ptr, __FILE__, __LINE__, 0x0);
STORM_FREE(ptr);
}
template <class T, class TKey>

View file

@ -73,7 +73,7 @@ T* TSList<T, TGetLink>::DeleteNode(T* ptr) {
T* next = this->Next(ptr);
ptr->~T();
SMemFree(ptr, __FILE__, __LINE__, 0);
STORM_FREE(ptr);
return next;
}

View file

@ -31,6 +31,13 @@ TEST_CASE("SMemAlloc", "[memory]") {
SMemFree(ptr);
}
SECTION("allocates memory with null filename") {
void* ptr = SMemAlloc(16, nullptr, 0);
REQUIRE(ptr != nullptr);
CHECK_NOTHROW(memset(ptr, 1, 16));
SMemFree(ptr);
}
SECTION("allocates memory initialized to 0 with flag") {
void* ptr = SMemAlloc(16, __FILE__, __LINE__, SMEM_FLAG_ZEROMEMORY);
REQUIRE(ptr != nullptr);
@ -111,6 +118,11 @@ TEST_CASE("SMemFree full args", "[memory]") {
SECTION("does nothing on nullptr") {
CHECK_NOTHROW(SMemFree(nullptr, __FILE__, __LINE__));
}
SECTION("can take a null filename") {
void* ptr = STORM_ALLOC(10);
CHECK_NOTHROW(SMemFree(ptr, nullptr, 0));
}
}
TEST_CASE("SMemMove", "[memory]") {
@ -153,6 +165,13 @@ TEST_CASE("SMemReAlloc", "[memory]") {
SMemFree(ptr);
}
SECTION("allocates memory with null filename") {
void* ptr = SMemReAlloc(nullptr, 16, nullptr, 0);
REQUIRE(ptr != nullptr);
CHECK_NOTHROW(memset(ptr, 1, 16));
SMemFree(ptr);
}
SECTION("allocates memory initialized to 0 with flag") {
void* ptr = SMemReAlloc(nullptr, 16, __FILE__, __LINE__, SMEM_FLAG_ZEROMEMORY);
REQUIRE(ptr != nullptr);