CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
ObjectPool.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Queue.hpp"
11#include "Vector.hpp"
12
14
16
17#include <memory>
18
19namespace CeresEngine {
20
21 template<typename T, typename RawAllocator = DefaultAllocator> class ObjectPool {
22 public:
24 static inline const constexpr size_t defaultChunkSize = 32;
25
26 private:
30
33
34 // The free list stores the objects that are not currently in use by
35 // clients.
37
38 // Stores pointers to all the objects, in use or not. This vector is
39 // needed in order to ensure that all objects are freed properly in the
40 // destructor.
42
43 public:
51 if(chunkSize <= 0) {
52 throw std::invalid_argument("chunk size must be positive");
53 }
54 }
55
56 ObjectPool(const ObjectPool<T>& other) = delete;
57 ObjectPool<T>& operator=(const ObjectPool<T>& other) = delete;
58 ObjectPool(ObjectPool<T>&& other) = delete;
60
64 assert(mFreeList.size() == mAllObjects.size() * mChunkSize);
65
66 // free each of the allocation chunks
67 for_each(mAllObjects.begin(), mAllObjects.end(), [&](T* obj) { mAllocator.deallocate(obj, mChunkSize); });
68 }
69
70 public:
76 template<typename... Args> T* allocate(Args&&... args) {
77 if(mFreeList.empty()) {
79 }
80 T* obj = mFreeList.front();
81 mFreeList.pop();
82
83 return new(obj) T(std::forward<Args>(args)...);
84 }
85
89 void release(T* obj) {
90 obj->~T();
91 mFreeList.push(obj);
92 }
93
94 protected:
95 // Allocates chunkSize new objects and adds them to the freeList
97 T* newObjects = mAllocator.allocate(mChunkSize);
98 mAllObjects.push_back(newObjects);
99 for(size_t i = 0; i < mChunkSize; ++i) {
100 mFreeList.push(&newObjects[i]);
101 }
102 }
103 };
104
105} // namespace CeresEngine
#define CE_NO_UNIQUE_ADDRESS
Definition Macros.hpp:407
Definition ObjectPool.hpp:21
Queue< T * > mFreeList
Definition ObjectPool.hpp:36
void allocateChunk()
Definition ObjectPool.hpp:96
void release(T *obj)
Return the object to the pool.
Definition ObjectPool.hpp:89
ObjectPool< T > & operator=(ObjectPool< T > &&other)=delete
ObjectPool(ObjectPool< T > &&other)=delete
~ObjectPool()
Frees all the allocated objects.
Definition ObjectPool.hpp:63
ObjectPool(const size_t chunkSize=defaultChunkSize)
Create an object pool with chunkSize objects.
Definition ObjectPool.hpp:50
Vector< T * > mAllObjects
Definition ObjectPool.hpp:41
StdAllocator< T, RawAllocator > mAllocator
The allocator used to allocate memory for objects.
Definition ObjectPool.hpp:29
ObjectPool(const ObjectPool< T > &other)=delete
T * allocate(Args &&... args)
Reserve an object for use.
Definition ObjectPool.hpp:76
ObjectPool< T > & operator=(const ObjectPool< T > &other)=delete
static const constexpr size_t defaultChunkSize
The default allocation chunk size.
Definition ObjectPool.hpp:24
size_t mChunkSize
The pool chunk size.
Definition ObjectPool.hpp:32
Definition Application.hpp:19
std::queue< T, Container > Queue
The Queue class is a container adapter that gives the programmer the functionality of a queue - speci...
Definition Queue.hpp:19
foonathan::memory::std_allocator< T, RawAllocator > StdAllocator
Definition Allocator.hpp:225
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
constexpr ForEachAlgorithmFunctor for_each
Applies a function to a range of elements.
Definition ForEach.hpp:123
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25