Add FixedSizePoolResource PoC
This commit is contained in:
parent
593f7a3499
commit
45cd78a435
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
@ -150,8 +151,9 @@ void *Pool::Allocate() {
|
|||||||
--chunk->blocks_available;
|
--chunk->blocks_available;
|
||||||
return available_block;
|
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_);
|
return allocate_block_from_chunk(last_alloc_chunk_);
|
||||||
|
}
|
||||||
// Find a Chunk with available memory.
|
// Find a Chunk with available memory.
|
||||||
for (auto &chunk : chunks_) {
|
for (auto &chunk : chunks_) {
|
||||||
if (chunk.blocks_available > 0U) {
|
if (chunk.blocks_available > 0U) {
|
||||||
@ -333,4 +335,141 @@ void PoolResource::Release() {
|
|||||||
|
|
||||||
// PoolResource END
|
// 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<Link *>(p)->next = free_list_;
|
||||||
|
free_list_ = static_cast<Link *>(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<std::byte *>(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<impl::FixedSizePool>(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<size_t>::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
|
} // namespace utils
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <new>
|
#include <new>
|
||||||
@ -12,6 +13,9 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#ifndef NDEBUG
|
||||||
|
#include <iostream>
|
||||||
|
#endif
|
||||||
// Although <memory_resource> is in C++17, gcc libstdc++ still needs to
|
// Although <memory_resource> is in C++17, gcc libstdc++ still needs to
|
||||||
// implement it fully. It should be available in the next major release
|
// implement it fully. It should be available in the next major release
|
||||||
// version, i.e. gcc 9.x.
|
// 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; }
|
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 <typename T>
|
||||||
|
concept BlockLinkedListHeader = std::is_base_of_v<BlockLinkedListHeaderBase, T>;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
concept BlockSingleLinkedListHeader = std::is_base_of_v<BlockSingleLinkedListHeaderBase, T>;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
concept BlockListHeader = BlockLinkedListHeader<T> || BlockSingleLinkedListHeader<T>;
|
||||||
|
|
||||||
|
template <BlockListHeader THeader>
|
||||||
|
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<THeader>) {
|
||||||
|
list_.previous = &list_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void *Allocate(const size_t bytes, utils::MemoryResource *rsrc) {
|
||||||
|
if (bytes > static_cast<size_t>(-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<THeader>) {
|
||||||
|
node->previous = &list_;
|
||||||
|
next->previous = node;
|
||||||
|
}
|
||||||
|
|
||||||
|
return static_cast<std::byte *>(p) + header_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T = THeader>
|
||||||
|
requires BlockLinkedListHeader<T> void Deallocate(void *p, utils::MemoryResource *rsrc) {
|
||||||
|
auto *header = static_cast<THeader *>(static_cast<void *>(static_cast<std::byte *>(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<THeader *>(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<THeader>) {
|
||||||
|
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<BlockSingleLinkedListHeaderBase> 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<impl::BlockLinkedListHeaderBase> 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
|
} // namespace utils
|
||||||
|
Loading…
Reference in New Issue
Block a user