63 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| 
 | |
| #pragma once
 | |
| 
 | |
| #if !defined(MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED)
 | |
| #define MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED 1
 | |
| 
 | |
| #include <array>
 | |
| #include <cstddef>
 | |
| #include <cstdint>
 | |
| #include <limits>
 | |
| #include <memory>
 | |
| #include <vector>
 | |
| #include "../debug/assert.hpp"
 | |
| 
 | |
| namespace mijin
 | |
| {
 | |
| template<typename TObject, std::size_t OBJECTS_PER_PAGE = 1024>
 | |
| 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<std::byte, sizeof(TObject)> bytes;
 | |
|     };
 | |
|     struct Page
 | |
|     {
 | |
|         std::uint32_t freeOffset = 0;
 | |
|         std::array<ObjectData, OBJECTS_PER_PAGE> data;
 | |
|     };
 | |
| 
 | |
|     std::vector<std::unique_ptr<Page>> pages;
 | |
| public:
 | |
|     [[nodiscard]] TObject* allocate(std::size_t count = 1) noexcept;
 | |
|     void free(TObject* object, std::size_t count = 1) noexcept;
 | |
| };
 | |
| 
 | |
| template<typename TObject, std::size_t OBJECTS_PER_PAGE>
 | |
| TObject* ObjectPool<TObject, OBJECTS_PER_PAGE>::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>& page : pages)
 | |
|     {
 | |
|         std::uint32_t offset = page->freeOffset;
 | |
|         while (offset < OBJECTS_PER_PAGE)
 | |
|         {
 | |
|             GapInfo& gapInfo = *std::bit_cast<GapInfo*>(&page->data[offset]);
 | |
|             
 | |
|         }
 | |
|     }
 | |
| }
 | |
| } // namespace mijin
 | |
| 
 | |
| #endif // !defined(MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED)
 |