diff --git a/storm/Memory.cpp b/storm/Memory.cpp index 4147c57..9b4d58a 100644 --- a/storm/Memory.cpp +++ b/storm/Memory.cpp @@ -70,3 +70,10 @@ void* SMemReAlloc(void* ptr, size_t bytes, const char* filename, int32_t linenum return nullptr; } } + +void SMemZero(void* ptr, size_t bytes) { + uint8_t* ptrdata = static_cast(ptr); + for (size_t i = 0; i < bytes; i++) { + ptrdata[i] = 0; + } +} diff --git a/storm/Memory.hpp b/storm/Memory.hpp index e5ac2f0..8bf77c5 100644 --- a/storm/Memory.hpp +++ b/storm/Memory.hpp @@ -18,4 +18,6 @@ void SMemFree(void* ptr, const char* filename, int32_t linenumber, uint32_t flag void* SMemReAlloc(void* ptr, size_t bytes, const char* filename, int32_t linenumber, uint32_t flags = 0); +void SMemZero(void* ptr, size_t bytes); + #endif diff --git a/test/Memory.cpp b/test/Memory.cpp index ba67c7e..c41385e 100644 --- a/test/Memory.cpp +++ b/test/Memory.cpp @@ -103,3 +103,21 @@ TEST_CASE("SMemReAlloc", "[memory]") { SMemFree(ptr2); } } + +TEST_CASE("SMemZero", "[memory]") { + std::vector data = { 1, 255, 128, 42, 69, 99, 13, 37 }; + + SECTION("zeroes out memory") { + std::vector result = { 0, 0, 0, 0, 0, 99, 13, 37 }; + SMemZero(data.data(), 5); + + CHECK_THAT(data, Catch::Matchers::Equals(result)); + } + + SECTION("does nothing if size is 0") { + std::vector change = data; + SMemZero(change.data(), 0); + + CHECK_THAT(change, Catch::Matchers::Equals(data)); + } +}