Add allocator, start work on box
This commit is contained in:
38
asl/allocator.cpp
Normal file
38
asl/allocator.cpp
Normal file
@ -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);
|
||||
}
|
Reference in New Issue
Block a user