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

#include <memory_pool.h>

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

Classes

struct  statistics
 Statistics for monitoring memory pool usage. More...
 

Public Member Functions

 fixed_block_pool (std::size_t block_size, std::size_t blocks_per_chunk=1024)
 
 ~fixed_block_pool ()=default
 
void * allocate ()
 
void deallocate (void *p)
 
std::size_t block_size () const noexcept
 
statistics get_statistics () const
 Get current pool statistics.
 

Private Member Functions

void allocate_chunk_unlocked ()
 

Private Attributes

std::size_t block_size_
 
std::size_t blocks_per_chunk_
 
std::vector< std::unique_ptr< std::uint8_t[]> > chunks_
 
void * free_list_ {nullptr}
 
std::mutex mutex_
 
std::size_t allocated_count_ {0}
 

Detailed Description

Definition at line 21 of file memory_pool.h.

Constructor & Destructor Documentation

◆ fixed_block_pool()

kcenon::container::internal::fixed_block_pool::fixed_block_pool ( std::size_t block_size,
std::size_t blocks_per_chunk = 1024 )
inlineexplicit

Definition at line 23 of file memory_pool.h.

25 : block_size_(block_size < sizeof(void*) ? sizeof(void*) : block_size)
26 , blocks_per_chunk_(blocks_per_chunk) {}
std::size_t block_size() const noexcept
Definition memory_pool.h:76

◆ ~fixed_block_pool()

kcenon::container::internal::fixed_block_pool::~fixed_block_pool ( )
default

Member Function Documentation

◆ allocate()

void * kcenon::container::internal::fixed_block_pool::allocate ( )
inline

Definition at line 30 of file memory_pool.h.

30 {
31 std::lock_guard<std::mutex> lock(mutex_);
32 if (!free_list_) {
33 try {
35 } catch (const std::bad_alloc&) {
36 // Chunk allocation failed, propagate the exception
37 throw;
38 }
39 // Verify that allocate_chunk_unlocked() actually created free blocks
40 if (!free_list_) {
41 throw std::runtime_error("Memory pool chunk allocation failed to create free list");
42 }
43 }
44 void* p = free_list_;
45 free_list_ = *reinterpret_cast<void**>(free_list_);
47 return p;
48 }

References allocate_chunk_unlocked(), allocated_count_, free_list_, and mutex_.

Referenced by kcenon::container::internal::pool_allocator::allocate().

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

◆ allocate_chunk_unlocked()

void kcenon::container::internal::fixed_block_pool::allocate_chunk_unlocked ( )
inlineprivate

Definition at line 127 of file memory_pool.h.

127 {
128 std::unique_ptr<std::uint8_t[]> chunk(new std::uint8_t[block_size_ * blocks_per_chunk_]);
129 std::uint8_t* base = chunk.get();
130 chunks_.push_back(std::move(chunk));
131 // Build free list
132 for (std::size_t i = 0; i < blocks_per_chunk_; ++i) {
133 void* p = base + i * block_size_;
134 *reinterpret_cast<void**>(p) = free_list_;
135 free_list_ = p;
136 }
137 }
std::vector< std::unique_ptr< std::uint8_t[]> > chunks_

References block_size_, blocks_per_chunk_, chunks_, and free_list_.

Referenced by allocate().

Here is the caller graph for this function:

◆ block_size()

std::size_t kcenon::container::internal::fixed_block_pool::block_size ( ) const
inlinenoexcept

Definition at line 76 of file memory_pool.h.

76{ return block_size_; }

References block_size_.

◆ deallocate()

void kcenon::container::internal::fixed_block_pool::deallocate ( void * p)
inline

Definition at line 50 of file memory_pool.h.

50 {
51 if (!p) return;
52 std::lock_guard<std::mutex> lock(mutex_);
53
54 // Validate pointer belongs to one of our pool chunks.
55 // This lightweight range check runs in all build modes to
56 // prevent free-list corruption from invalid deallocations.
57 bool found = false;
58 for (const auto& chunk : chunks_) {
59 std::uint8_t* chunk_start = chunk.get();
60 std::uint8_t* chunk_end = chunk_start + (block_size_ * blocks_per_chunk_);
61 if (reinterpret_cast<std::uint8_t*>(p) >= chunk_start &&
62 reinterpret_cast<std::uint8_t*>(p) < chunk_end) {
63 found = true;
64 break;
65 }
66 }
67 if (!found) {
68 return;
69 }
70
71 *reinterpret_cast<void**>(p) = free_list_;
72 free_list_ = p;
74 }

References allocated_count_, block_size_, blocks_per_chunk_, chunks_, free_list_, and mutex_.

Referenced by kcenon::container::internal::pool_allocator::deallocate().

Here is the caller graph for this function:

◆ get_statistics()

statistics kcenon::container::internal::fixed_block_pool::get_statistics ( ) const
inline

Get current pool statistics.

Note
This method is thread-safe but may impact performance if called frequently

Definition at line 105 of file memory_pool.h.

105 {
106 std::lock_guard<std::mutex> lock(mutex_);
107
108 // Count free blocks in the free list
109 std::size_t free_count = 0;
110 void* current = free_list_;
111 while (current) {
112 ++free_count;
113 current = *reinterpret_cast<void**>(current);
114 }
115
116 std::size_t total_capacity = chunks_.size() * blocks_per_chunk_;
117
118 return statistics{
119 chunks_.size(),
121 total_capacity,
122 free_count
123 };
124 }

References allocated_count_, blocks_per_chunk_, chunks_, free_list_, and mutex_.

Referenced by kcenon::container::internal::pool_allocator::get_medium_pool_stats(), and kcenon::container::internal::pool_allocator::get_small_pool_stats().

Here is the caller graph for this function:

Member Data Documentation

◆ allocated_count_

std::size_t kcenon::container::internal::fixed_block_pool::allocated_count_ {0}
private

Definition at line 145 of file memory_pool.h.

145{0}; // Track number of currently allocated blocks

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

◆ block_size_

std::size_t kcenon::container::internal::fixed_block_pool::block_size_
private

Definition at line 140 of file memory_pool.h.

Referenced by allocate_chunk_unlocked(), block_size(), and deallocate().

◆ blocks_per_chunk_

std::size_t kcenon::container::internal::fixed_block_pool::blocks_per_chunk_
private

Definition at line 141 of file memory_pool.h.

Referenced by allocate_chunk_unlocked(), deallocate(), and get_statistics().

◆ chunks_

std::vector<std::unique_ptr<std::uint8_t[]> > kcenon::container::internal::fixed_block_pool::chunks_
private

Definition at line 142 of file memory_pool.h.

Referenced by allocate_chunk_unlocked(), deallocate(), and get_statistics().

◆ free_list_

void* kcenon::container::internal::fixed_block_pool::free_list_ {nullptr}
private

Definition at line 143 of file memory_pool.h.

143{nullptr};

Referenced by allocate(), allocate_chunk_unlocked(), deallocate(), and get_statistics().

◆ mutex_

std::mutex kcenon::container::internal::fixed_block_pool::mutex_
mutableprivate

Definition at line 144 of file memory_pool.h.

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


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