30 ::operator
delete(
static_cast<void*
>(ptr));
73 :
growth_(growth == 0 ? 1 : growth) {}
81 template<
typename... Args>
84 bool reused_local =
false;
86 std::lock_guard<std::mutex> lock(
mutex_);
98 *reused = reused_local;
101 new (raw) T(std::forward<Args>(args)...);
105 template<
typename... Args>
107 return acquire(
static_cast<bool*
>(
nullptr), std::forward<Args>(args)...);
119 std::lock_guard<std::mutex> lock(
mutex_);
130 std::lock_guard<std::mutex> lock(
mutex_);
138 std::lock_guard<std::mutex> lock(
mutex_);
144 std::lock_guard<std::mutex> lock(
mutex_);
149 using RawPtr = std::unique_ptr<T, detail::RawDelete<T>>;
152 for (std::size_t i = 0; i < count; ++i) {
153 RawPtr block(
static_cast<T*
>(::operator
new(
sizeof(T))));
155 storage_.push_back(std::move(block));
Thread-safe object pool that reuses raw storage for expensive objects.
void release(T *ptr) noexcept
Return raw storage to the pool (destructor already run).
void allocate_block_unlocked(std::size_t count)
std::vector< RawPtr > storage_
pointer_type acquire(bool *reused, Args &&... args)
Acquire an object constructed with the provided arguments.
ObjectPool(std::size_t growth=32)
Construct an object pool with the specified growth factor.
void reserve(std::size_t count)
Add count additional blocks to the pool.
std::unique_ptr< T, detail::RawDelete< T > > RawPtr
std::unique_ptr< T, deleter_type > pointer_type
std::size_t available() const
pointer_type acquire(Args &&... args)
std::stack< T * > free_list_
void clear()
Destroy all cached instances and release memory.
Zero-overhead deleter for ObjectPool-managed objects.
void operator()(T *ptr) const noexcept
void operator()(T *ptr) const noexcept