Files
XCEngine/docs/api/memory/pool-allocator/free.md

39 lines
809 B
Markdown
Raw Normal View History

# PoolAllocator::Free
```cpp
void Free(void* ptr) override;
```
将内存块归还到空闲链表中。如果 `ptr``nullptr` 则无效果。释放操作将块插入空闲链表头部。必须传入从此池分配的指针,传入外部指针会导致未定义行为。
**参数:**
- `ptr` - 指向要释放的内存块
**返回:** 无
**复杂度:** O(1)
**示例:**
```cpp
#include <XCEngine/Memory/PoolAllocator.h>
PoolAllocator pool(sizeof(int), 100);
void* block = pool.Allocate();
if (block) {
int* num = static_cast<int*>(block);
*num = 123;
pool.Free(block); // 归还到空闲链表
block = nullptr;
}
// 检查空闲块数量
size_t freeCount = pool.GetFreeBlockCount();
```
## 相关文档
- [PoolAllocator 总览](pool-allocator.md) - 返回类总览