From db99fa5da911089890a3aeb0b5a5a54a4b0cea1f Mon Sep 17 00:00:00 2001 From: iris Date: Tue, 23 Aug 2022 20:47:10 +0100 Subject: [PATCH] wip --- include/iris/graphics/constant_buffer_pool.h | 83 ++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 include/iris/graphics/constant_buffer_pool.h diff --git a/include/iris/graphics/constant_buffer_pool.h b/include/iris/graphics/constant_buffer_pool.h new file mode 100644 index 00000000..f95edc38 --- /dev/null +++ b/include/iris/graphics/constant_buffer_pool.h @@ -0,0 +1,83 @@ +//////////////////////////////////////////////////////////////////////////////// +// Distributed under the Boost Software License, Version 1.0. // +// (See accompanying file LICENSE or copy at // +// https://www.boost.org/LICENSE_1_0.txt) // +//////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include + +#include "core/error_handling.h" + +namespace iris +{ + +template > +class ConstantBufferPool +{ + public: + ConstantBufferPool() + : chunk_alloc_() + , chunks_(nullptr) + , free_list_(nullptr) + { + chunks_ = chunk_alloc_.allocate(N); + + auto *cursor = chunks_; + for (auto i = 0u; i < N; i++) + { + ::new (std::addressof(cursor->buffer)) T(1024u * 8u); + ++cursor; + } + + free_list_ = chunks_; + } + + T *next() + { + ensure(free_list_ != nullptr, "pool has been drained"); + + auto *object = std::addressof(free_list_->buffer); + + if (free_list_->next == nullptr) + { + ++free_list_; + } + else + { + free_list_ = free_list_->next; + } + + return object; + } + + void release(T *object) + { + auto *chunk = reinterpret_cast(object) - 1u; + + chunk->next = free_list_; + free_list_ = chunk; + } + + private: + struct Chunk + { + Chunk(std::size_t size) + : next(nullptr) + , buffer(size) + { + } + + Chunk *next; + T buffer; + }; + + typename std::allocator_traits::template rebind_alloc chunk_alloc_; + + Chunk *chunks_; + + Chunk *free_list_; +}; + +}