Type-integrated, O(1), aligned, thread-safe, stupid-simple C++ object pool template.
Code an object pool from scratch, compliant with C++11. Make it efficient, but also extremely simple to use.
- Ultra-fast setup using inherited, type-integrated template.
- O(1) allocation/deallocation of pooled objects.
- O(1) space complexity (no space required above space required for objects).
- Proper alignment of objects via use of
aligned_storage
. - Thread-safe allocation/deallocation action.
- Pooled objects allocated/deallocated with normal
new
anddelete
calls. - Automatic Pool creation/destruction.
- Fixed-size object pool.
- Pool statistics (e.g. peak pool size) available via inherited methods.
- C++11
std::thread
-
Copy the template source files from the src directory into your project.
-
Include
PooledObject.hpp
:#include "PooledObject.hpp"
-
Select the target class (
Foo
). Inherit from the object pool template. The template parameters are the name of the target class (repeated), and the size of the object pool (i.e. the number ofFoo
objects it will hold):class Foo : public PooledObject<Foo, 10> { int x; };
-
The object pool is created automatically with the first use of a
new
call forFoo
. Alternately, you can create the object pool ahead of time:Foo::manual_allocate_object_pool();
-
Objects will be allocated/deallocated from the pool with normal
new
anddelete
calls:Foo* f = new Foo{}; delete f;
-
When
main
exits, the pool will be destroyed.
-
Get current peak size (the max number of objects that has been allocated at any point in time):
std::size_t peak_size = Foo::get_peak_size();
- Overloads the type's
operator new
andoperator delete
. - O(1) time complexity achieved by utilizing a singly-linked list for allocation and deallocation.
- O(1) space complexity achieved by utilizing a
union
for type. - One-time-only pool creation realized via
std::call_once
. - Pool destruction with program termination realized via
std::atexit
. - A debug assert will be raised if an allocation would exceed the pool's fixed size.
- Feel free to report a bug or propose a feature by opening a new Issue.
- Follow the project's Contributing guidelines.
- Respect the project's Code Of Conduct.