fix allocator issue

misaligned size
v1
Brett 2024-03-08 16:37:51 -05:00
parent 55bae67407
commit 7177b03a43
2 changed files with 22 additions and 8 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.5)
include(cmake/color.cmake)
set(BLT_VERSION 0.14.5)
set(BLT_VERSION 0.14.6)
set(BLT_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT)

View File

@ -608,7 +608,7 @@ namespace blt
struct block
{
struct
struct block_metadata_t
{
blt::size_t allocated_objects = 0;
block* next = nullptr;
@ -623,6 +623,8 @@ namespace blt
}
};
static constexpr blt::size_t BASE_REMAINDER = BLOCK_SIZE - sizeof(typename block::block_metadata_t);
block* base = nullptr;
block* head = nullptr;
@ -681,16 +683,22 @@ namespace blt
head = block;
}
void* allocate_bytes(blt::size_t bytes, blt::size_t alignment)
{
blt::size_t remaining_bytes = BASE_REMAINDER - static_cast<blt::size_t>(head->metadata.offset - head->buffer);
auto pointer = static_cast<void*>(head->metadata.offset);
return std::align(alignment, bytes, pointer, remaining_bytes);
}
template<typename T>
T* allocate_back(blt::size_t count)
{
blt::size_t remaining_bytes = BLOCK_SIZE - static_cast<blt::size_t>(head->metadata.offset - head->buffer);
auto pointer = static_cast<void*>(head->metadata.offset);
const auto aligned_address = std::align(alignof(T) * count, sizeof(T) * count, pointer, remaining_bytes);
blt::size_t bytes = sizeof(T) * count;
const auto aligned_address = allocate_bytes(bytes, alignof(T));
if (aligned_address != nullptr)
{
head->metadata.allocated_objects++;
head->metadata.offset = static_cast<blt::u8*>(aligned_address) + sizeof(T) * count;
head->metadata.offset = static_cast<blt::u8*>(aligned_address) + bytes;
}
return static_cast<T*>(aligned_address);
}
@ -732,7 +740,7 @@ namespace blt
template<typename T>
[[nodiscard]] T* allocate(blt::size_t count = 1)
{
if constexpr (sizeof(T) > BLOCK_SIZE)
if constexpr (sizeof(T) > BASE_REMAINDER)
throw std::bad_alloc();
auto* ptr = attempt_allocation<T>([this]() { allocate_forward(); }, count);
@ -754,10 +762,16 @@ namespace blt
if (blk->metadata.prev != nullptr)
blk->metadata.prev->metadata.next = blk->metadata.next;
del(blk);
//del(blk);
}
}
template<typename T>
auto* blk(T* p)
{
return reinterpret_cast<block*>(reinterpret_cast<std::uintptr_t>(p) & static_cast<std::uintptr_t>(~(BLOCK_SIZE - 1)));
}
template<typename T, typename... Args>
[[nodiscard]] T* emplace(Args&& ... args)
{