mirror of
https://github.com/thunderbrewhq/squall.git
synced 2026-02-04 00:49:08 +00:00
chore(memory): add additional tests and flag
This commit is contained in:
parent
2e044682e1
commit
8e9fb741b6
3 changed files with 83 additions and 5 deletions
|
|
@ -7,7 +7,7 @@ void* SMemAlloc(size_t bytes, const char* filename, int32_t linenumber, uint32_t
|
||||||
|
|
||||||
void* result;
|
void* result;
|
||||||
|
|
||||||
if (flags & 0x8) {
|
if (flags & SMEM_FLAG_ZEROMEMORY) {
|
||||||
result = calloc(1, alignedBytes);
|
result = calloc(1, alignedBytes);
|
||||||
} else {
|
} else {
|
||||||
result = malloc(alignedBytes);
|
result = malloc(alignedBytes);
|
||||||
|
|
|
||||||
|
|
@ -4,12 +4,16 @@
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
void* SMemAlloc(size_t bytes, const char* filename, int32_t linenumber, uint32_t flags);
|
|
||||||
|
#define SMEM_FLAG_ZEROMEMORY 8
|
||||||
|
|
||||||
|
|
||||||
|
void* SMemAlloc(size_t bytes, const char* filename, int32_t linenumber, uint32_t flags = 0);
|
||||||
|
|
||||||
void SMemFree(void* ptr);
|
void SMemFree(void* ptr);
|
||||||
|
|
||||||
void SMemFree(void* ptr, const char* filename, int32_t linenumber, uint32_t flags);
|
void SMemFree(void* ptr, const char* filename, int32_t linenumber, uint32_t flags = 0);
|
||||||
|
|
||||||
void* SMemReAlloc(void* ptr, size_t bytes, const char* filename, int32_t linenumber, uint32_t flags);
|
void* SMemReAlloc(void* ptr, size_t bytes, const char* filename, int32_t linenumber, uint32_t flags = 0);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,84 @@
|
||||||
#include "storm/Memory.hpp"
|
#include "storm/Memory.hpp"
|
||||||
#include "test/Test.hpp"
|
#include "test/Test.hpp"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <numeric>
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE("SMemAlloc", "[memory]") {
|
TEST_CASE("SMemAlloc", "[memory]") {
|
||||||
SECTION("allocates memory") {
|
SECTION("allocates memory") {
|
||||||
void* ptr = SMemAlloc(16, __FILE__, __LINE__, 0x0);
|
void* ptr = SMemAlloc(16, __FILE__, __LINE__);
|
||||||
|
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);
|
||||||
|
|
||||||
|
uint8_t* pArray = static_cast<uint8_t*>(ptr);
|
||||||
|
CHECK(std::accumulate(pArray, pArray + 16, 0u) == 0);
|
||||||
|
|
||||||
|
SMemFree(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("allocates a memory pointer even if the size is 0") {
|
||||||
|
void* ptr = SMemAlloc(0, __FILE__, __LINE__);
|
||||||
REQUIRE(ptr != nullptr);
|
REQUIRE(ptr != nullptr);
|
||||||
SMemFree(ptr);
|
SMemFree(ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("SMemFree", "[memory]") {
|
||||||
|
SECTION("does nothing on nullptr") {
|
||||||
|
CHECK_NOTHROW(SMemFree(nullptr));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("SMemFree full args", "[memory]") {
|
||||||
|
SECTION("does nothing on nullptr") {
|
||||||
|
CHECK_NOTHROW(SMemFree(nullptr, __FILE__, __LINE__));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("SMemReAlloc", "[memory]") {
|
||||||
|
SECTION("allocates memory") {
|
||||||
|
void* ptr = SMemReAlloc(nullptr, 16, __FILE__, __LINE__);
|
||||||
|
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);
|
||||||
|
|
||||||
|
uint8_t* pArray = static_cast<uint8_t*>(ptr);
|
||||||
|
CHECK(std::accumulate(pArray, pArray + 16, 0u) == 0);
|
||||||
|
|
||||||
|
SMemFree(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("allocates a memory pointer even if the size is 0") {
|
||||||
|
void* ptr = SMemReAlloc(nullptr, 0, __FILE__, __LINE__);
|
||||||
|
REQUIRE(ptr != nullptr);
|
||||||
|
SMemFree(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("reallocates memory") {
|
||||||
|
void* ptr = SMemAlloc(16, __FILE__, __LINE__);
|
||||||
|
REQUIRE(ptr != nullptr);
|
||||||
|
|
||||||
|
*static_cast<uint32_t*>(ptr) = 123456;
|
||||||
|
|
||||||
|
// 1MB
|
||||||
|
void* ptr2 = SMemReAlloc(ptr, 1024 * 1024, __FILE__, __LINE__);
|
||||||
|
REQUIRE(ptr2 != nullptr);
|
||||||
|
|
||||||
|
CHECK(*static_cast<uint32_t*>(ptr2) == 123456);
|
||||||
|
CHECK_NOTHROW(memset(ptr2, 1, 1024 * 1024));
|
||||||
|
|
||||||
|
SMemFree(ptr2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue