Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::utils::buffer_pool Class Reference

Thread-safe memory pool for reusable byte buffers. More...

#include <buffer_pool.h>

Inheritance diagram for kcenon::network::utils::buffer_pool:
Inheritance graph
Collaboration diagram for kcenon::network::utils::buffer_pool:
Collaboration graph

Classes

class  impl
 

Public Member Functions

 buffer_pool (size_t pool_size=32, size_t default_capacity=8192)
 Constructs a buffer pool.
 
 ~buffer_pool () noexcept
 Destructor.
 
auto acquire (size_t min_capacity=0) -> std::shared_ptr< std::vector< uint8_t > >
 Acquires a buffer from the pool.
 
auto get_stats () const -> std::pair< size_t, size_t >
 Gets current pool statistics.
 
auto clear () -> void
 Clears the pool and releases all cached buffers.
 

Private Member Functions

auto release (std::vector< uint8_t > *buffer) -> void
 Returns a buffer to the pool.
 

Private Attributes

std::unique_ptr< implpimpl_
 

Detailed Description

Thread-safe memory pool for reusable byte buffers.

Thread Safety

  • All public methods are thread-safe
  • Uses mutex protection for pool access
  • Safe for concurrent acquire/release operations

Key Features

  • Pre-allocated buffer pool reduces allocations
  • Automatic buffer size adjustment
  • Configurable pool size and buffer capacity
  • RAII-based buffer management with custom deleter

Usage Example

auto pool = std::make_shared<buffer_pool>(
32, // pool size: 32 buffers
8192 // default buffer capacity: 8KB
);
// Acquire a buffer
auto buffer = pool->acquire(4096); // Get buffer with at least 4KB
// Use buffer...
buffer->resize(data_size);
std::copy(data, data + data_size, buffer->begin());
// Buffer automatically returns to pool when shared_ptr is destroyed

Definition at line 46 of file buffer_pool.h.

Constructor & Destructor Documentation

◆ buffer_pool()

kcenon::network::utils::buffer_pool::buffer_pool ( size_t pool_size = 32,
size_t default_capacity = 8192 )
explicit

Constructs a buffer pool.

Parameters
pool_sizeMaximum number of buffers to cache
default_capacityDefault capacity for new buffers

Definition at line 134 of file buffer_pool.cpp.

135 : pimpl_(std::make_unique<impl>(pool_size, default_capacity))
136 {
137 }
std::unique_ptr< impl > pimpl_
Definition buffer_pool.h:93

◆ ~buffer_pool()

kcenon::network::utils::buffer_pool::~buffer_pool ( )
noexcept

Destructor.

Definition at line 139 of file buffer_pool.cpp.

140 {
141 try
142 {
143 pimpl_->clear();
144 }
145 catch (...)
146 {
147 // Destructor must not throw
148 }
149 }

References kcenon::network::utils::buffer_pool::impl::clear(), and pimpl_.

Here is the call graph for this function:

Member Function Documentation

◆ acquire()

auto kcenon::network::utils::buffer_pool::acquire ( size_t min_capacity = 0) -> std::shared_ptr<std::vector<uint8_t>>

Acquires a buffer from the pool.

Parameters
min_capacityMinimum required capacity
Returns
Shared pointer to buffer (automatically returned to pool when destroyed)

If no suitable buffer is available in the pool, creates a new one. Buffer is returned to pool automatically when the shared_ptr is destroyed.

Definition at line 151 of file buffer_pool.cpp.

152 {
153 return pimpl_->acquire(min_capacity, this);
154 }
auto acquire(size_t min_capacity, buffer_pool *parent) -> std::shared_ptr< std::vector< uint8_t > >

◆ clear()

auto kcenon::network::utils::buffer_pool::clear ( ) -> void

Clears the pool and releases all cached buffers.

Definition at line 166 of file buffer_pool.cpp.

166{ pimpl_->clear(); }

◆ get_stats()

auto kcenon::network::utils::buffer_pool::get_stats ( ) const -> std::pair<size_t, size_t>

Gets current pool statistics.

Returns
Pair of (available buffers, total allocated)

Definition at line 161 of file buffer_pool.cpp.

162 {
163 return pimpl_->get_stats();
164 }
auto get_stats() const -> std::pair< size_t, size_t >

References kcenon::network::utils::buffer_pool::impl::get_stats(), and pimpl_.

Here is the call graph for this function:

◆ release()

auto kcenon::network::utils::buffer_pool::release ( std::vector< uint8_t > * buffer) -> void
private

Returns a buffer to the pool.

Parameters
bufferBuffer to return

Called automatically by custom deleter when shared_ptr is destroyed.

Definition at line 156 of file buffer_pool.cpp.

157 {
158 pimpl_->release(buffer);
159 }
auto release(std::vector< uint8_t > *buffer) -> void

Member Data Documentation

◆ pimpl_

std::unique_ptr<impl> kcenon::network::utils::buffer_pool::pimpl_
private

Definition at line 93 of file buffer_pool.h.

Referenced by get_stats(), and ~buffer_pool().


The documentation for this class was generated from the following files: