cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
toolbox::base::memory_pool_t Class Reference

固定大小内存池类/Fixed-size memory pool class More...

#include <memory_pool.hpp>

Public Member Functions

 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
 
 ~memory_pool_t ()
 析构函数,释放所有缓存内存块/Destructor, frees all cached blocks
 
voidallocate ()
 禁用拷贝构造和赋值/Disable copy constructor and assignment
 
void deallocate (void *ptr)
 归还内存块到池中/Return a block to the pool
 
std::size_t block_size () const noexcept
 获取每个内存块的大小/Get the size of each memory block
 
std::size_t free_blocks () const
 获取当前池中空闲块数量/Get the number of free blocks currently stored in the pool
 
void release_unused ()
 释放所有缓存的内存块到系统/Release all cached blocks back to the system
 

Detailed Description

固定大小内存池类/Fixed-size memory pool class

提供高效的固定大小内存块分配与回收,支持线程安全和自动扩容/收缩。 Provides efficient allocation and deallocation of fixed-size memory blocks, supporting thread safety and automatic expansion/shrinking.

pool with 64-byte blocks, 4 preallocated, max 8 cached, growth 2 void* p =
pool.allocate(); // 分配内存块/Allocate a block pool.deallocate(p); //
归还内存块/Deallocate a block pool.release_unused(); //
固定大小内存池类/Fixed-size memory pool class
Definition memory_pool.hpp:29
Definition object_pool.hpp:100

Constructor & Destructor Documentation

◆ memory_pool_t()

toolbox::base::memory_pool_t::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 
)
inlineexplicit

构造内存池/Construct a memory pool

Parameters
block_size每个内存块的字节数,必须大于0/Size of each memory block in bytes, must be > 0
initial_blocks预先分配的内存块数量/Number of blocks to preallocate
max_cached_blocks最大缓存空闲块数,超出则归还系统/Maximum number of freed blocks to keep cached, exceeding this returns memory to the system 使用 std::numeric_limits<std::size_t>::max() 表示无限制/Use std::numeric_limits<std::size_t>::max() for unlimited
growth池耗尽时每次扩容分配的块数/Number of blocks to allocate when the pool runs out

创建一个提供固定大小内存块的池。池耗尽时按 growth 分配新块,归还过多时自动释放多余内存。 Creates a pool that provides fixed-size memory blocks. If the pool is exhausted, additional blocks will be allocated in batches of growth. When too many blocks are returned, excess ones are released so the pool can shrink.

memory_pool_t pool(32, 2); // 创建块大小32字节,预分配2块的内存池/Create a

◆ ~memory_pool_t()

toolbox::base::memory_pool_t::~memory_pool_t ( )
inline

析构函数,释放所有缓存内存块/Destructor, frees all cached blocks

Member Function Documentation

◆ allocate()

void * toolbox::base::memory_pool_t::allocate ( )
inline

禁用拷贝构造和赋值/Disable copy constructor and assignment

禁用移动构造和赋值/Disable move constructor and assignment

分配一个内存块/Allocate a memory block

Returns
指向内存块的指针/Pointer to a block of size block_size()
void* p = pool.allocate(); // 分配内存块/Allocate a block

◆ block_size()

std::size_t toolbox::base::memory_pool_t::block_size ( ) const
inlinenoexcept

获取每个内存块的大小/Get the size of each memory block

Returns
内存块字节数/Size of each memory block in bytes

◆ deallocate()

void toolbox::base::memory_pool_t::deallocate ( void ptr)
inline

归还内存块到池中/Return a block to the pool

Parameters
ptrallocate() 获得的指针/Pointer previously obtained from allocate()
void* p = pool.allocate();
pool.deallocate(p); // 归还内存块/Return the block

◆ free_blocks()

std::size_t toolbox::base::memory_pool_t::free_blocks ( ) const
inline

获取当前池中空闲块数量/Get the number of free blocks currently stored in the pool

Returns
空闲块数量/Number of free blocks

◆ release_unused()

void toolbox::base::memory_pool_t::release_unused ( )
inline

释放所有缓存的内存块到系统/Release all cached blocks back to the system

pool.release_unused(); // 释放所有未用内存/Release all unused memory

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