cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
memory_pool.hpp
Go to the documentation of this file.
1#include <cstddef>
2#include <limits>
3#include <mutex>
4#include <queue>
5#include <stdexcept>
6
8
9namespace toolbox::base
10{
11
29{
30public:
54 explicit memory_pool_t(
55 std::size_t block_size,
56 std::size_t initial_blocks = 0,
57 std::size_t max_cached_blocks = std::numeric_limits<std::size_t>::max(),
58 std::size_t growth = 1)
59 : block_size_(block_size)
60 , growth_(growth == 0 ? 1 : growth)
61 , max_cached_blocks_(max_cached_blocks)
62 {
63 if (block_size_ == 0) {
64 throw std::invalid_argument("block size must be > 0");
65 }
66
67 // 预分配指定数量的内存块/Preallocate the specified number of blocks
68 for (std::size_t i = 0; i < initial_blocks; ++i) {
69 pool_.push(::operator new(block_size_));
70 }
71 }
72
77
86
87
96 {
97 std::lock_guard<std::mutex> lock(mutex_);
98 if (pool_.empty()) {
99 expand_blocks(growth_);
100 }
101 void* ptr = pool_.front();
102 pool_.pop();
103 return ptr;
104 }
105
116 void deallocate(void* ptr)
117 {
118 if (!ptr) {
119 return;
120 }
121 std::lock_guard<std::mutex> lock(mutex_);
122 if (pool_.size() >= max_cached_blocks_) {
123 ::operator delete(ptr);
124 } else {
125 pool_.push(ptr);
126 }
127 }
128
133 std::size_t block_size() const noexcept { return block_size_; }
134
140 std::size_t free_blocks() const
141 {
142 std::lock_guard<std::mutex> lock(mutex_);
143 return pool_.size();
144 }
145
155 {
156 std::lock_guard<std::mutex> lock(mutex_);
157 // 释放所有缓存块/Release all cached blocks
158 while (!pool_.empty()) {
159 ::operator delete(pool_.front());
160 pool_.pop();
161 }
162 }
163
164private:
168 std::size_t block_size_ {};
172 std::size_t growth_ {1};
176 std::size_t max_cached_blocks_ {std::numeric_limits<std::size_t>::max()};
180 mutable std::mutex mutex_;
184 std::queue<void*> pool_;
185
191 void expand_blocks(std::size_t count)
192 {
193 for (std::size_t i = 0; i < count; ++i) {
194 pool_.push(::operator new(block_size_));
195 }
196 }
197};
198
199} // namespace toolbox::base
#define CPP_TOOLBOX_DISABLE_COPY(ClassType)
禁用类的拷贝操作 / Disable copy operations for a class
Definition class.hpp:15
#define CPP_TOOLBOX_DISABLE_MOVE(ClassType)
禁用类的移动操作 / Disable move operations for a class
Definition class.hpp:31
固定大小内存池类/Fixed-size memory pool class
Definition memory_pool.hpp:29
void release_unused()
释放所有缓存的内存块到系统/Release all cached blocks back to the system
Definition memory_pool.hpp:154
~memory_pool_t()
析构函数,释放所有缓存内存块/Destructor, frees all cached blocks
Definition memory_pool.hpp:76
std::size_t block_size() const noexcept
获取每个内存块的大小/Get the size of each memory block
Definition memory_pool.hpp:133
void * allocate()
禁用拷贝构造和赋值/Disable copy constructor and assignment
Definition memory_pool.hpp:95
void deallocate(void *ptr)
归还内存块到池中/Return a block to the pool
Definition memory_pool.hpp:116
std::size_t free_blocks() const
获取当前池中空闲块数量/Get the number of free blocks currently stored in the pool
Definition memory_pool.hpp:140
memory_pool_t(std::size_t block_size, std::size_t initial_blocks=0, std::size_t max_cached_blocks=std::numeric_limits< std::size_t >::max(), std::size_t growth=1)
构造内存池/Construct a memory pool
Definition memory_pool.hpp:54
Definition object_pool.hpp:100
通用的编译器、平台、架构检测和实用宏定义 / Common macros for compiler, platform, architecture detection and utility macro...
提供基础 C++ 工具和组件。 Provides fundamental C++ utilities and components.