diff --git a/CMakeLists.txt b/CMakeLists.txt index c564d19..13a4799 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/include/blt/std/allocator.h b/include/blt/std/allocator.h index cd0db76..2bde389 100644 --- a/include/blt/std/allocator.h +++ b/include/blt/std/allocator.h @@ -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(head->metadata.offset - head->buffer); + auto pointer = static_cast(head->metadata.offset); + return std::align(alignment, bytes, pointer, remaining_bytes); + } + template T* allocate_back(blt::size_t count) { - blt::size_t remaining_bytes = BLOCK_SIZE - static_cast(head->metadata.offset - head->buffer); - auto pointer = static_cast(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(aligned_address) + sizeof(T) * count; + head->metadata.offset = static_cast(aligned_address) + bytes; } return static_cast(aligned_address); } @@ -732,7 +740,7 @@ namespace blt template [[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([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 + auto* blk(T* p) + { + return reinterpret_cast(reinterpret_cast(p) & static_cast(~(BLOCK_SIZE - 1))); + } + template [[nodiscard]] T* emplace(Args&& ... args) {