Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bitshifter/objectpool
Object pool implementation in C++11
https://github.com/bitshifter/objectpool
Last synced: 3 months ago
JSON representation
Object pool implementation in C++11
- Host: GitHub
- URL: https://github.com/bitshifter/objectpool
- Owner: bitshifter
- License: zlib
- Created: 2016-03-26T03:30:47.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-11-29T21:41:25.000Z (about 4 years ago)
- Last Synced: 2024-11-01T00:51:27.551Z (3 months ago)
- Language: C++
- Size: 342 KB
- Stars: 92
- Watchers: 5
- Forks: 22
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- AwesomeCppGameDev - objectpool
README
## Object pool allocator
This is a C++11 implementation of an object pool allocator.
For more information on object pool allocators and their purpose see
http://gameprogrammingpatterns.com/object-pool.html.Both a fixed size pool (`FixedObjectPool`) and a dynamically growing pool
(`DynamicObjectPool`) implementation are included.The main features of this implementation are:
* `new_object` method uses C++11 std::forward to pass construction arguments
to the constructor of the new object being created in the pool
* `for_each` method will iterate over all live objects in the pool calling
the given function on them
* `delete_all` method will free all pool objects at once, skipping the
destructor call for trivial types
* maintains a freelist of next available pool entry for fast allocationThese object pool classes are not designed with exceptions in mind as most
game code avoids using exceptions.## Example usage
```cpp
// some type to be pooled
struct Enemy {
/* some data */
Enemy(const char* name);
void update(double delta_time);
};// created fixed size pool with space for 64 enemies
FixedObjectPool enemy_pool(64);// allocate an enemy - construction parameters are forwarded
Enemy* baddie = enemy_pool.new_object("The Mekon");// update all live enemies by executing the lambda on all allocated objects
enemy_pool.for_each([delta_time](Enemy* enemy)
{
enemy->update(delta_time);
});// delete a single enemy
enemy_pool.delete_object(baddie);// delete all enemies at once (beware of dangling pointers)
enemy_pool.delete_all();
```## Implementation details
Both `FixedObjectPool` and `DynamicObjectPool` are implemented using the
`ObjectPoolBlock` class.`ObjectPoolBlock` is a single allocation containing the `ObjectPoolBlock`
instance, indices of used pool entries and the pool memory itself.Occupancy is tracked using indexes into available entries in the block for
constant time allocation. The `ObjectPoolBlock` keeps the next free index head.
This index can be used to find the next available block entry when allocating
a new entry.A separate list of indices is used to track occupancy versus reusing object
pool memory for this purpose to avoid polluting CPU caches with objects which
are deleted and thus no longer in use.## Unit testing
Unit tests are written using the [Catch](https://github.com/philsquared/Catch)
unit testing framework. Unit tests are run through the `runtest` executable.## Micro-benchmarking
This repository also includes `bench.hpp` which is a single header file
micro-benchmarking framework inspired by Catch and Rust's
[benchmarking tests](https://doc.rust-lang.org/book/benchmark-tests.html).It's my intention to make this standalone at some point but at the
moment it's very much a work in progress.Currently each micro-benchmark compares the performance of the following:
* Fixed pool
* Dynamic pool with 64, 128 and 256 entry blocks
* The default allocatorBenchmarks output nanoseconds per iteration (lower is better) and megabytes per
second throughput (higher is better).## Prerequisites
The test and benchmarking applications require [CMake](http://www.cmake.org) to
generate build files.## Compiling and running
To generate a build, compile and run follow these steps:
~~~
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make
./runtests
./runbench
~~~## License
This software is licensed under the zlib license, see the LICENSE file for
details.