diff --git a/bc/Memory.cpp b/bc/Memory.cpp index b8d1152..3e2e416 100644 --- a/bc/Memory.cpp +++ b/bc/Memory.cpp @@ -1,5 +1,5 @@ #include "bc/Memory.hpp" -#include +#include "bc/memory/Storm.hpp" void* Blizzard::Memory::Allocate(uint32_t bytes) { return SMemAlloc(bytes, __FILE__, __LINE__, 0x0); diff --git a/bc/memory/Storm.cpp b/bc/memory/Storm.cpp new file mode 100644 index 0000000..5886048 --- /dev/null +++ b/bc/memory/Storm.cpp @@ -0,0 +1,68 @@ +#include "bc/memory/Storm.hpp" + +#include + +constexpr size_t ALIGNMENT = 8; + +void* SMemAlloc(size_t bytes, const char* filename, int32_t linenumber, uint32_t flags) { + size_t alignedBytes = (bytes + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1); + + void* result; + + if (flags & 0x8) { + result = calloc(1, alignedBytes); + } else { + result = malloc(alignedBytes); + } + + if (result) { + return result; + } else { + // TODO handle errors + return nullptr; + } +} + +void SMemFree(void* ptr) { + if (ptr) { + free(ptr); + } +} + +void SMemFree(void* ptr, const char* filename, int32_t linenumber, uint32_t flags) { + if (ptr) { + free(ptr); + } +} + +void* SMemReAlloc(void* ptr, size_t bytes, const char* filename, int32_t linenumber, uint32_t flags) { + if (flags == 0xB00BEEE5) { + return nullptr; + } + + if (!ptr) { + return SMemAlloc(bytes, filename, linenumber, flags); + } + + if (flags & 0x10) { + return nullptr; + } + + size_t alignedBytes = (bytes + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1); + + void* result = realloc(ptr, alignedBytes); + + if (result) { + if (flags & 0x8) { + // TODO zero out expanded portion + } + + return result; + } else { + if (alignedBytes) { + // TODO handle errors + } + + return nullptr; + } +} diff --git a/bc/memory/Storm.hpp b/bc/memory/Storm.hpp new file mode 100644 index 0000000..ba55825 --- /dev/null +++ b/bc/memory/Storm.hpp @@ -0,0 +1,14 @@ +#ifndef BC_MEMORY_STORM_HPP +#define BC_MEMORY_STORM_HPP + +#include + +void* SMemAlloc(size_t bytes, const char* filename, int32_t linenumber, uint32_t flags); + +void SMemFree(void* ptr); + +void SMemFree(void* ptr, 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); + +#endif diff --git a/build.zig b/build.zig index 267ed96..ad70016 100644 --- a/build.zig +++ b/build.zig @@ -16,10 +16,6 @@ pub fn build(b: *std.Build) void { // Add system detection defines system.add_defines(bc); - // Publicly link mem - const mem = b.dependency("mem", .{}); - bc.linkLibrary(mem.artifact("mem")); - bc.addIncludePath(b.path(".")); bc.addCSourceFiles(.{ @@ -48,6 +44,8 @@ pub fn build(b: *std.Build) void { "bc/time/Time.cpp", + "bc/memory/Storm.cpp", + "bc/Debug.cpp", "bc/Lock.cpp", "bc/Memory.cpp", @@ -72,7 +70,6 @@ pub fn build(b: *std.Build) void { // Add system detection defines system.add_defines(bc_test_exe); - bc_test_exe.linkLibrary(mem.artifact("mem")); bc_test_exe.linkLibrary(bc); bc_test_exe.addIncludePath(b.path(".")); diff --git a/lib/mem b/lib/mem deleted file mode 160000 index 5153788..0000000 --- a/lib/mem +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 515378839ec59155ef2b8df141f588f3f12f89d2