From 7d6946a885f620f346d5b8ecda8cd90aea1aa631 Mon Sep 17 00:00:00 2001 From: jacopodl Date: Thu, 11 Jul 2024 12:54:00 +0200 Subject: [PATCH] feat: added two helper functions that call std::malloc and std::free respectively. In the event of an error they behave in a manner compliant with Argon's memory management --- argon/vm/memory/memory.cpp | 22 +++++++++++++++++++++- argon/vm/memory/memory.h | 8 ++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/argon/vm/memory/memory.cpp b/argon/vm/memory/memory.cpp index f0b444ef..09b10b37 100644 --- a/argon/vm/memory/memory.cpp +++ b/argon/vm/memory/memory.cpp @@ -38,4 +38,24 @@ void *argon::vm::memory::Realloc(void *ptr, size_t size) { Panic((datatype::ArObject *) datatype::error_oom); return mem; -} \ No newline at end of file +} + +// MALLOC WRAPPER + +void *argon::vm::memory::Copy2Malloc(void *src, size_t size) { + auto *buf = WMalloc(size); + + if(buf!= nullptr) + argon::vm::memory::MemoryCopy(buf, src, size); + + return buf; +} + +void *argon::vm::memory::WMalloc(size_t size) { + void *ptr = malloc(size); + + if(ptr == nullptr) + Panic((datatype::ArObject *) datatype::error_oom); + + return ptr; +} diff --git a/argon/vm/memory/memory.h b/argon/vm/memory/memory.h index 2b56d3ff..7d140b40 100644 --- a/argon/vm/memory/memory.h +++ b/argon/vm/memory/memory.h @@ -26,6 +26,14 @@ namespace argon::vm::memory { void *Realloc(void *ptr, size_t size); void Free(void *ptr); + + // MALLOC WRAPPER + + void *Copy2Malloc(void *src, size_t size); + + void *WMalloc(size_t size); + + const auto WFree= free; } #endif // !ARGON_VM_MEMORY_MEMORY_H_