diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-11-19 00:08:33 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | d241eaf1b209dcfb05656842dd6250067b704d99 (patch) | |
tree | 49f34f6226f4614611d16ba6332b1b50b51a4712 /asl/allocator.cpp | |
parent | 58200ce939a591008a8d9406f437252ce2b175cf (diff) |
Add allocator, start work on box
Diffstat (limited to 'asl/allocator.cpp')
-rw-r--r-- | asl/allocator.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/asl/allocator.cpp b/asl/allocator.cpp new file mode 100644 index 0000000..9559637 --- /dev/null +++ b/asl/allocator.cpp @@ -0,0 +1,38 @@ +#include "asl/allocator.hpp"
+#include "asl/assert.hpp"
+#include "asl/utility.hpp"
+#include "asl/memory.hpp"
+
+#include <cstdlib>
+
+// @Todo zalloc
+// @Todo Cookies
+// @Todo Debug values
+
+void* asl::GlobalHeap::alloc(const layout& layout)
+{
+ void* ptr = ::_aligned_malloc(
+ static_cast<size_t>(layout.size),
+ static_cast<size_t>(layout.align));
+ ASL_ASSERT(ptr != nullptr); // @Todo panic
+ return ptr;
+}
+
+void* asl::GlobalHeap::realloc(void* old_ptr, const layout& old_layout, const layout& new_layout)
+{
+ if (new_layout.align <= old_layout.align)
+ {
+ void* new_ptr = ::realloc(old_ptr, static_cast<size_t>(new_layout.size));
+ ASL_ASSERT(new_ptr != nullptr); // @Todo panic
+ return new_ptr;
+ }
+
+ void* new_ptr = alloc(new_layout);
+ asl::memcpy(new_ptr, old_ptr, asl::min(old_layout.size, new_layout.size));
+ return new_ptr;
+}
+
+void asl::GlobalHeap::dealloc(void* ptr, const layout&)
+{
+ ::free(ptr);
+}
|