From 45cd78a435a85cf0ec468383eaaeae158af683f0 Mon Sep 17 00:00:00 2001 From: Antonio Andelic Date: Wed, 10 Feb 2021 10:36:27 +0100 Subject: [PATCH] Add FixedSizePoolResource PoC --- src/utils/memory.cpp | 141 +++++++++++++++++++++++++++- src/utils/memory.hpp | 215 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 355 insertions(+), 1 deletion(-) diff --git a/src/utils/memory.cpp b/src/utils/memory.cpp index 7acffa5c5..20e8c43a8 100644 --- a/src/utils/memory.cpp +++ b/src/utils/memory.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -150,8 +151,9 @@ void *Pool::Allocate() { --chunk->blocks_available; return available_block; }; - if (last_alloc_chunk_ && last_alloc_chunk_->blocks_available > 0U) + if (last_alloc_chunk_ && last_alloc_chunk_->blocks_available > 0U) { return allocate_block_from_chunk(last_alloc_chunk_); + } // Find a Chunk with available memory. for (auto &chunk : chunks_) { if (chunk.blocks_available > 0U) { @@ -333,4 +335,141 @@ void PoolResource::Release() { // PoolResource END +namespace impl { +namespace { +constexpr size_t kInitialChunkSize = 1; +} // namespace +FixedSizePool::FixedSizePool(const size_t block_size, const size_t max_blocks_per_chunk_, + utils::MemoryResource *upstream) + : block_size_{block_size}, + max_blocks_per_chunk_{max_blocks_per_chunk_}, + chunk_size_{kInitialChunkSize}, + upstream_{upstream} { + internal_block_size_ = RoundUp(block_size, alignof(std::max_align_t)); +} + +void *FixedSizePool::Allocate() { + if (begin_ == end_) { + if (free_list_) { + Link *p = free_list_; + free_list_ = p->next; + return p; + } + + Replenish(); + } + + auto *p = begin_; + begin_ += internal_block_size_; + return p; +} + +void FixedSizePool::Deallocate(void *p) { + static_cast(p)->next = free_list_; + free_list_ = static_cast(p); +} + +void FixedSizePool::Release() { + chunk_list_.Release(upstream_); + + chunk_size_ = kInitialChunkSize; + begin_ = nullptr; + end_ = nullptr; + free_list_ = nullptr; +} + +FixedSizePool::~FixedSizePool() { Release(); } + +void FixedSizePool::Replenish() { + begin_ = static_cast(chunk_list_.Allocate(chunk_size_ * internal_block_size_, upstream_)); + end_ = begin_ + chunk_size_ * internal_block_size_; + + // TODO (antonio2368): Add more growth strategies + const auto next_chunk_size = chunk_size_ * 2; + if (next_chunk_size <= max_blocks_per_chunk_) { + chunk_size_ = next_chunk_size; + } +} + +} // namespace impl + +namespace { +inline uint64_t CeilLog2(const uint64_t x) { return utils::Log2(x) + !utils::IsPow2(x); } +} // namespace + +FixedSizePoolResource::FixedSizePoolResource(const size_t max_blocks_per_chunk, const size_t max_block_size, + utils::MemoryResource *upstream) + : upstream_{upstream} { + max_block_size_ = min_block_size_; + + num_pools_ = CeilLog2(max_block_size) - CeilLog2(min_block_size_) + 1; + MG_ASSERT(num_pools_ > 0, "Invalid max size of the block."); + + auto allocator = Allocator(upstream_); + pools_ = allocator.allocate(num_pools_); + + for (size_t i = 0; i < num_pools_; ++i) { + allocator.construct(pools_ + i, max_block_size_, max_blocks_per_chunk, upstream); + MG_ASSERT(max_block_size_ <= std::numeric_limits::max() / 2); + max_block_size_ *= 2; + } + + max_block_size_ /= 2; +} + +void *FixedSizePoolResource::DoAllocate(const size_t bytes, const size_t alignment) { + MG_ASSERT(bytes > 0); + void *ret{nullptr}; + if (bytes <= max_block_size_) { + auto *pool = FindPool(bytes); + ret = pool->Allocate(); + } else { + // add to the list of allocated chunks + ret = large_blocks_.Allocate(bytes, upstream_); + } + + return ret; +} + +void FixedSizePoolResource::DoDeallocate(void *p, const size_t bytes, const size_t alignment) { + auto *pool = FindPool(bytes); + + if (pool == nullptr) { + large_blocks_.Deallocate(p, upstream_); + } else { + pool->Deallocate(p); + } +} + +impl::FixedSizePool *FixedSizePoolResource::FindPool(const size_t size) const { + if (size > max_block_size_) { + return nullptr; + } + + auto *pool = pools_; + while (size > pool->BlockSize()) { + ++pool; + } + + return pool; +} + +void FixedSizePoolResource::Release() { + for (size_t i = 0; i < num_pools_; ++i) { + pools_[i].Release(); + } + + large_blocks_.Release(upstream_); +} + +FixedSizePoolResource::~FixedSizePoolResource() { + Release(); + + for (size_t i = 0; i < num_pools_; ++i) { + pools_[i].~FixedSizePool(); + } + + upstream_->Deallocate(pools_, sizeof(impl::FixedSizePool) * num_pools_, alignof(impl::FixedSizePool)); +} + } // namespace utils diff --git a/src/utils/memory.hpp b/src/utils/memory.hpp index 0d5607dde..bdfe73cd8 100644 --- a/src/utils/memory.hpp +++ b/src/utils/memory.hpp @@ -5,6 +5,7 @@ #pragma once #include +#include #include #include #include @@ -12,6 +13,9 @@ #include #include #include +#ifndef NDEBUG +#include +#endif // Although is in C++17, gcc libstdc++ still needs to // implement it fully. It should be available in the next major release // version, i.e. gcc 9.x. @@ -552,4 +556,215 @@ class LimitedMemoryResource final : public utils::MemoryResource { bool DoIsEqual(const MemoryResource &other) const noexcept override { return this == &other; } }; +namespace impl { + +// https://stackoverflow.com/a/9194117 +// if y is power 2 we can do (x + y - 1) & ~(y - 1); +// https://stackoverflow.com/a/14561794 +constexpr size_t RoundUp(const size_t x, const size_t y) { + MG_ASSERT(y >= 1); + return (x + y - 1) / y * y; +} + +struct LinkedListNode { + LinkedListNode *next; + LinkedListNode *previous; +}; + +struct BlockLinkedListHeaderBase : public LinkedListNode { + size_t size; + + using NodeType = LinkedListNode; +}; + +struct SingleLinkedListNode { + SingleLinkedListNode *next; +}; + +struct BlockSingleLinkedListHeaderBase : public SingleLinkedListNode { + size_t size; + + using NodeType = SingleLinkedListNode; +}; + +template +concept BlockLinkedListHeader = std::is_base_of_v; + +template +concept BlockSingleLinkedListHeader = std::is_base_of_v; + +template +concept BlockListHeader = BlockLinkedListHeader || BlockSingleLinkedListHeader; + +template +class BlockList { + using NodeType = typename THeader::NodeType; + + public: + static const size_t header_size = RoundUp(sizeof(THeader), alignof(std::max_align_t)); + + explicit BlockList() { + // create a circular linked list + list_.next = &list_; + + if constexpr (BlockLinkedListHeader) { + list_.previous = &list_; + } + } + + void *Allocate(const size_t bytes, utils::MemoryResource *rsrc) { + if (bytes > static_cast(-1) - header_size) { + throw utils::BadAlloc("Requested too many bytes to allocate"); + } + + void *p = rsrc->Allocate(bytes + header_size); + auto *node = new (p) THeader; + node->size = bytes + header_size; + + auto *next = list_.next; + node->next = next; + list_.next = node; + + if constexpr (BlockLinkedListHeader) { + node->previous = &list_; + next->previous = node; + } + + return static_cast(p) + header_size; + } + + template + requires BlockLinkedListHeader void Deallocate(void *p, utils::MemoryResource *rsrc) { + auto *header = static_cast(static_cast(static_cast(p) - header_size)); + auto *next = header->next; + auto *previous = header->previous; + previous->next = next; + next->previous = previous; + + const size_t size = header->size; + header->~THeader(); + rsrc->Deallocate(header, size, alignof(std::max_align_t)); + } + + void Release(utils::MemoryResource *rsrc) { + NodeType *node = list_.next; + while (node != &list_) { + auto *header = static_cast(node); + node = node->next; + const size_t size = header->size; + header->~THeader(); + rsrc->Deallocate(header, size, alignof(std::max_align_t)); + } + + // create a circular linked list + list_.next = &list_; + + if constexpr (BlockLinkedListHeader) { + list_.previous = &list_; + } + } + + private: + NodeType list_; +}; + +// https://github.com/bloomberg/bde/blob/master/groups/bdl/bdlma/bdlma_pool.h +class FixedSizePool final { + public: + FixedSizePool(size_t block_size, size_t max_blocks_per_chunk, utils::MemoryResource *upstream); + + FixedSizePool(const FixedSizePool &) = delete; + FixedSizePool &operator=(const FixedSizePool &) = delete; + + FixedSizePool(FixedSizePool &&) = default; + FixedSizePool &operator=(FixedSizePool &&) = default; + + void *Allocate(); + void Deallocate(void *p); + + void Release(); + + auto BlockSize() const { return block_size_; } + + ~FixedSizePool(); + + private: + struct Link { + Link *next; + }; + + std::byte *begin_{nullptr}; + std::byte *end_{nullptr}; + + Link *free_list_{nullptr}; + + size_t internal_block_size_; + size_t block_size_; + size_t max_blocks_per_chunk_; + size_t chunk_size_; + + BlockList chunk_list_; + + utils::MemoryResource *upstream_; + + void Replenish(); +}; +} // namespace impl + +// https://github.com/bloomberg/bde/blob/master/groups/bdl/bdlma/bdlma_multipool.h +class FixedSizePoolResource : public MemoryResource { + public: + explicit FixedSizePoolResource(size_t max_blocks_per_chunk, size_t max_block_size, + utils::MemoryResource *upstream = utils::NewDeleteResource()); + + void Release(); + + FixedSizePoolResource(const FixedSizePoolResource &) = delete; + FixedSizePoolResource &operator=(const FixedSizePoolResource &) = delete; + + FixedSizePoolResource(FixedSizePoolResource &&) = default; + FixedSizePoolResource &operator=(FixedSizePoolResource &&) = default; + + ~FixedSizePoolResource() override; + + private: + impl::FixedSizePool *FindPool(size_t size) const; + + void *DoAllocate(size_t bytes, size_t alignment) override; + void DoDeallocate(void *p, size_t bytes, size_t alignment) override; + + bool DoIsEqual(const MemoryResource &other) const noexcept override { return this == &other; } + + impl::FixedSizePool *pools_{nullptr}; + size_t max_block_size_; + size_t min_block_size_{8}; + size_t num_pools_{0}; + + impl::BlockList large_blocks_; + + utils::MemoryResource *upstream_; +}; + +#ifndef NDEBUG +class PrintMemoryResource : public MemoryResource { + public: + explicit PrintMemoryResource(utils::MemoryResource *memory) : memory_(memory) {} + + private: + void *DoAllocate(size_t bytes, size_t alignment) override { + std::cout << "Allocating " << bytes << std::endl; + return memory_->Allocate(bytes, alignment); + } + + void DoDeallocate(void *p, size_t bytes, size_t alignment) override { + std::cout << "Deallocating " << bytes << std::endl; + return memory_->Deallocate(p, bytes, alignment); + } + + bool DoIsEqual(const utils::MemoryResource &other) const noexcept override { return memory_->IsEqual(other); } + + utils::MemoryResource *memory_; +}; +#endif + } // namespace utils