Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
kcenon::container::internal::pool_allocator Class Reference

Thread-local pool manager for value_container allocations. More...

#include <pool_allocator.h>

Collaboration diagram for kcenon::container::internal::pool_allocator:
Collaboration graph

Public Member Functions

void * allocate (std::size_t size) noexcept
 Allocate memory from the appropriate pool.
 
void deallocate (void *ptr, std::size_t size) noexcept
 Deallocate memory to the appropriate pool.
 
pool_allocator_stats get_stats () const noexcept
 Get allocation statistics.
 
fixed_block_pool::statistics get_small_pool_stats () const
 Get statistics from the small pool.
 
fixed_block_pool::statistics get_medium_pool_stats () const
 Get statistics from the medium pool.
 
void reset_stats () noexcept
 Reset statistics (for benchmarking)
 

Static Public Member Functions

static pool_allocatorinstance () noexcept
 Get the singleton instance.
 

Private Member Functions

 pool_allocator ()
 
 pool_allocator (const pool_allocator &)=delete
 
pool_allocatoroperator= (const pool_allocator &)=delete
 
 pool_allocator (pool_allocator &&)=delete
 
pool_allocatoroperator= (pool_allocator &&)=delete
 

Private Attributes

fixed_block_pool small_pool_
 
fixed_block_pool medium_pool_
 
pool_allocator_stats stats_
 

Detailed Description

Thread-local pool manager for value_container allocations.

Manages two size-class pools (small and medium) with thread-local instances for lock-free allocation on the fast path.

Definition at line 61 of file pool_allocator.h.

Constructor & Destructor Documentation

◆ pool_allocator() [1/3]

kcenon::container::internal::pool_allocator::pool_allocator ( )
inlineprivate

◆ pool_allocator() [2/3]

kcenon::container::internal::pool_allocator::pool_allocator ( const pool_allocator & )
privatedelete

◆ pool_allocator() [3/3]

kcenon::container::internal::pool_allocator::pool_allocator ( pool_allocator && )
privatedelete

Member Function Documentation

◆ allocate()

void * kcenon::container::internal::pool_allocator::allocate ( std::size_t size)
inlinenoexcept

Allocate memory from the appropriate pool.

Parameters
sizeNumber of bytes to allocate
Returns
Pointer to allocated memory, or nullptr on failure

Definition at line 77 of file pool_allocator.h.

77 {
78 if (size == 0) {
79 return nullptr;
80 }
81
82#if CONTAINER_USE_MEMORY_POOL
83 try {
85 void* ptr = small_pool_.allocate();
88 return ptr;
89 } else if (size <= pool_size_class::medium_threshold) {
90 void* ptr = medium_pool_.allocate();
93 return ptr;
94 }
95 } catch (...) {
96 // Pool allocation failed, fall through to heap
97 }
98#endif
99 // Large allocation or pool disabled: use heap
101 return ::operator new(size, std::nothrow);
102 }

References kcenon::container::internal::fixed_block_pool::allocate(), medium_pool_, kcenon::container::internal::pool_allocator_stats::medium_pool_allocs, kcenon::container::internal::pool_size_class::medium_threshold, kcenon::container::internal::pool_allocator_stats::pool_hits, kcenon::container::internal::pool_allocator_stats::pool_misses, small_pool_, kcenon::container::internal::pool_allocator_stats::small_pool_allocs, kcenon::container::internal::pool_size_class::small_threshold, and stats_.

Referenced by kcenon::container::internal::pool_allocate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ deallocate()

void kcenon::container::internal::pool_allocator::deallocate ( void * ptr,
std::size_t size )
inlinenoexcept

Deallocate memory to the appropriate pool.

Parameters
ptrPointer to deallocate
sizeSize of the allocation

Definition at line 109 of file pool_allocator.h.

109 {
110 if (!ptr) {
111 return;
112 }
113
115
116#if CONTAINER_USE_MEMORY_POOL
119 return;
120 } else if (size <= pool_size_class::medium_threshold) {
122 return;
123 }
124#endif
125 // Large allocation: use heap
126 ::operator delete(ptr);
127 }

References kcenon::container::internal::fixed_block_pool::deallocate(), kcenon::container::internal::pool_allocator_stats::deallocations, medium_pool_, kcenon::container::internal::pool_size_class::medium_threshold, small_pool_, kcenon::container::internal::pool_size_class::small_threshold, and stats_.

Referenced by kcenon::container::internal::pool_allocate(), and kcenon::container::internal::pool_deallocate().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_medium_pool_stats()

fixed_block_pool::statistics kcenon::container::internal::pool_allocator::get_medium_pool_stats ( ) const
inline

Get statistics from the medium pool.

Returns
Medium pool statistics

Definition at line 149 of file pool_allocator.h.

149 {
151 }
statistics get_statistics() const
Get current pool statistics.

References kcenon::container::internal::fixed_block_pool::get_statistics(), and medium_pool_.

Here is the call graph for this function:

◆ get_small_pool_stats()

fixed_block_pool::statistics kcenon::container::internal::pool_allocator::get_small_pool_stats ( ) const
inline

Get statistics from the small pool.

Returns
Small pool statistics

Definition at line 141 of file pool_allocator.h.

141 {
143 }

References kcenon::container::internal::fixed_block_pool::get_statistics(), and small_pool_.

Here is the call graph for this function:

◆ get_stats()

pool_allocator_stats kcenon::container::internal::pool_allocator::get_stats ( ) const
inlinenoexcept

Get allocation statistics.

Returns
Current statistics

Definition at line 133 of file pool_allocator.h.

133 {
134 return stats_;
135 }

References stats_.

◆ instance()

static pool_allocator & kcenon::container::internal::pool_allocator::instance ( )
inlinestaticnoexcept

Get the singleton instance.

Returns
Reference to thread-local pool allocator

Definition at line 67 of file pool_allocator.h.

67 {
68 thread_local pool_allocator allocator;
69 return allocator;
70 }

Referenced by kcenon::container::internal::pool_allocate(), and kcenon::container::internal::pool_deallocate().

Here is the caller graph for this function:

◆ operator=() [1/2]

pool_allocator & kcenon::container::internal::pool_allocator::operator= ( const pool_allocator & )
privatedelete

◆ operator=() [2/2]

pool_allocator & kcenon::container::internal::pool_allocator::operator= ( pool_allocator && )
privatedelete

◆ reset_stats()

void kcenon::container::internal::pool_allocator::reset_stats ( )
inlinenoexcept

Reset statistics (for benchmarking)

Definition at line 156 of file pool_allocator.h.

156 {
157 stats_ = pool_allocator_stats{};
158 }

References stats_.

Member Data Documentation

◆ medium_pool_

fixed_block_pool kcenon::container::internal::pool_allocator::medium_pool_
private

Definition at line 173 of file pool_allocator.h.

Referenced by allocate(), deallocate(), and get_medium_pool_stats().

◆ small_pool_

fixed_block_pool kcenon::container::internal::pool_allocator::small_pool_
private

Definition at line 172 of file pool_allocator.h.

Referenced by allocate(), deallocate(), and get_small_pool_stats().

◆ stats_

pool_allocator_stats kcenon::container::internal::pool_allocator::stats_
private

Definition at line 174 of file pool_allocator.h.

Referenced by allocate(), deallocate(), get_stats(), and reset_stats().


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