#pragma once #if !defined(MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED) #define MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED 1 #include #include #include #include #include #include #include "../debug/assert.hpp" namespace mijin { template class ObjectPool { private: struct GapInfo { std::uint32_t objectCount; std::uint32_t nextGap; }; static constexpr std::size_t ALIGN = std::max(alignof(TObject), alignof(GapInfo)); struct alignas(ALIGN) ObjectData { std::array bytes; }; struct Page { std::uint32_t freeOffset = 0; std::array data; }; std::vector> pages; public: [[nodiscard]] TObject* allocate(std::size_t count = 1) noexcept; void free(TObject* object, std::size_t count = 1) noexcept; }; template TObject* ObjectPool::allocate(std::size_t count) noexcept { MIJIN_ASSERT(count <= OBJECTS_PER_PAGE, "Cannot allocate more than OBJECTS_PER_PAGE elements at once!"); // first try to find a free spot in the existing pages for (std::unique_ptr& page : pages) { std::uint32_t offset = page->freeOffset; while (offset < OBJECTS_PER_PAGE) { GapInfo& gapInfo = *std::bit_cast(&page->data[offset]); } } } } // namespace mijin #endif // !defined(MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED)