atomic allocator might not be possible

v1
Brett 2024-08-19 20:03:46 -04:00
parent 72211e3d7b
commit 78710a12cc
3 changed files with 85 additions and 85 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake) include(cmake/color.cmake)
set(BLT_VERSION 0.18.38) set(BLT_VERSION 0.18.39)
set(BLT_TEST_VERSION 0.0.1) set(BLT_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -36,89 +36,89 @@
namespace blt namespace blt
{ {
template<typename Alloc = blt::aligned_huge_allocator> // template<typename Alloc = blt::aligned_huge_allocator>
class atomic_bump_allocator // class atomic_bump_allocator
{ // {
private: // private:
struct block // struct block
{ // {
struct block_metadata_t // struct block_metadata_t
{ // {
const blt::size_t size = 0; // const blt::size_t size = 0;
std::atomic<blt::size_t> allocated_objects = 0; // std::atomic<blt::size_t> allocated_objects = 0;
std::atomic<block*> next = nullptr; // std::atomic<block*> next = nullptr;
std::atomic<block*> prev = nullptr; // std::atomic<block*> prev = nullptr;
std::atomic<blt::u8*> offset = nullptr; // std::atomic<blt::u8*> offset = nullptr;
} metadata; // } metadata;
blt::u8 buffer[8]{}; // blt::u8 buffer[8]{};
//
explicit block(blt::size_t size) // explicit block(blt::size_t size)
{ // {
metadata.size = size; // metadata.size = size;
metadata.offset = buffer; // metadata.offset = buffer;
} // }
//
[[nodiscard]] blt::ptrdiff_t storage_size() const noexcept // [[nodiscard]] blt::ptrdiff_t storage_size() const noexcept
{ // {
return static_cast<blt::ptrdiff_t>(metadata.size - sizeof(typename block::block_metadata_t)); // return static_cast<blt::ptrdiff_t>(metadata.size - sizeof(typename block::block_metadata_t));
} // }
//
[[nodiscard]] blt::ptrdiff_t used_bytes_in_block() const noexcept // [[nodiscard]] blt::ptrdiff_t used_bytes_in_block() const noexcept
{ // {
return static_cast<blt::ptrdiff_t>(metadata.offset - buffer); // return static_cast<blt::ptrdiff_t>(metadata.offset - buffer);
} // }
//
[[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const noexcept // [[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const noexcept
{ // {
return storage_size() - used_bytes_in_block(); // return storage_size() - used_bytes_in_block();
} // }
}; // };
//
public: // public:
void* allocate(blt::size_t bytes) // void* allocate(blt::size_t bytes)
{ // {
auto head_ptr = head.load(std::memory_order_relaxed); // auto head_ptr = head.load(std::memory_order_relaxed);
auto new_head = head_ptr; // auto new_head = head_ptr;
do // do
{ // {
if (head_ptr->remaining_bytes_in_block() < bytes) // if (head_ptr->remaining_bytes_in_block() < bytes)
{ // {
//
} // }
} while (!head.compare_exchange_weak(head_ptr, new_head, std::memory_order_release, std::memory_order_acquire)); // } while (!head.compare_exchange_weak(head_ptr, new_head, std::memory_order_release, std::memory_order_acquire));
} // }
//
void deallocate(void* ptr) // void deallocate(void* ptr)
{ // {
auto blk = to_block(ptr); // auto blk = to_block(ptr);
--blk.metadata.allocated_objects; // --blk.metadata.allocated_objects;
if (blk.metadata.allocated_objects == 0) // if (blk.metadata.allocated_objects == 0)
{ // {
if (blk->metadata.prev != nullptr) // if (blk->metadata.prev != nullptr)
blk->metadata.prev->metadata.next = blk->metadata.next; // blk->metadata.prev->metadata.next = blk->metadata.next;
if (blk->metadata.next != nullptr) // if (blk->metadata.next != nullptr)
blk->metadata.next->metadata.prev = blk->metadata.prev; // blk->metadata.next->metadata.prev = blk->metadata.prev;
alloc.deallocate(blk, blk.metadata.size); // alloc.deallocate(blk, blk.metadata.size);
} // }
} // }
//
private: // private:
static inline block* to_block(void* p) // static inline block* to_block(void* p)
{ // {
return reinterpret_cast<block*>(reinterpret_cast<std::uintptr_t>(p) & static_cast<std::uintptr_t>(~(BLT_2MB_SIZE - 1))); // return reinterpret_cast<block*>(reinterpret_cast<std::uintptr_t>(p) & static_cast<std::uintptr_t>(~(BLT_2MB_SIZE - 1)));
} // }
//
inline block* allocate_block(blt::size_t bytes) // inline block* allocate_block(blt::size_t bytes)
{ // {
auto size = align_size_to(bytes, BLT_2MB_SIZE); // auto size = align_size_to(bytes, BLT_2MB_SIZE);
auto ptr = static_cast<block*>(alloc.allocate(size)); // auto ptr = static_cast<block*>(alloc.allocate(size));
new(ptr) block{size}; // new(ptr) block{size};
return ptr; // return ptr;
} // }
//
std::atomic<block*> head = nullptr; // std::atomic<block*> head = nullptr;
Alloc alloc; // Alloc alloc;
}; // };
} }

@ -1 +1 @@
Subproject commit 8a889d3699b3c09ade435641fb034427f3fd12b6 Subproject commit d88c5e15079047777b418132ece5879e7c9aaa2b