From 547fc6d4f092de30f1a2298e988cb99e82c21adf Mon Sep 17 00:00:00 2001 From: Adam Heinermann Date: Fri, 12 Sep 2025 21:53:57 -0700 Subject: [PATCH] feat(memory): add STORM_FREE macro --- storm/Event.cpp | 8 ++++---- storm/Memory.hpp | 3 +++ storm/hash/Hashkey.cpp | 4 ++-- storm/hash/TSHashTable.hpp | 2 +- storm/list/TSList.hpp | 2 +- test/Memory.cpp | 19 +++++++++++++++++++ 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/storm/Event.cpp b/storm/Event.cpp index 442505f..a8996d7 100644 --- a/storm/Event.cpp +++ b/storm/Event.cpp @@ -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; diff --git a/storm/Memory.hpp b/storm/Memory.hpp index e6ccd54..e6c461e 100644 --- a/storm/Memory.hpp +++ b/storm/Memory.hpp @@ -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 diff --git a/storm/hash/Hashkey.cpp b/storm/hash/Hashkey.cpp index 568e9da..27b71ff 100644 --- a/storm/hash/Hashkey.cpp +++ b/storm/hash/Hashkey.cpp @@ -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__); diff --git a/storm/hash/TSHashTable.hpp b/storm/hash/TSHashTable.hpp index 59a4182..e94058f 100644 --- a/storm/hash/TSHashTable.hpp +++ b/storm/hash/TSHashTable.hpp @@ -137,7 +137,7 @@ void TSHashTable::InternalClear(int32_t warn) { template void TSHashTable::InternalDelete(T* ptr) { ptr->~T(); - SMemFree(ptr, __FILE__, __LINE__, 0x0); + STORM_FREE(ptr); } template diff --git a/storm/list/TSList.hpp b/storm/list/TSList.hpp index 76e39b1..bcc98f7 100644 --- a/storm/list/TSList.hpp +++ b/storm/list/TSList.hpp @@ -73,7 +73,7 @@ T* TSList::DeleteNode(T* ptr) { T* next = this->Next(ptr); ptr->~T(); - SMemFree(ptr, __FILE__, __LINE__, 0); + STORM_FREE(ptr); return next; } diff --git a/test/Memory.cpp b/test/Memory.cpp index 14e24b7..849ac1b 100644 --- a/test/Memory.cpp +++ b/test/Memory.cpp @@ -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);