Files
XCEngine/docs/api/memory/proxy-allocator/free.md
ssdfasd 98c764bab9 docs: Fix memory module documentation discrepancies
- Add missing PoolAllocator class overview with methods table
- Add missing LinearAllocator class overview with methods table
- Add missing ProxyAllocator class overview with methods table
- Fix PoolAllocator::Allocate example code and comments
- Clarify ProxyAllocator::Free totalFreed calculation behavior
- Fix CreateLinearAllocator complexity from O(size) to O(1)
- Add note about Reallocate thread safety in ProxyAllocator
2026-03-19 00:48:44 +08:00

43 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ProxyAllocator::Free
```cpp
void Free(void* ptr) override;
```
释放内存并记录统计。调用转发到底层分配器,同时更新统计信息:递增 `totalFreed`(累加当前 `allocationCount`),并递减 `allocationCount`
**注意:** `totalFreed` 累加的是每次释放时的 `allocationCount`(分配计数)而非实际字节大小。这是一个简化的实现,用于追踪释放操作次数。
**参数:**
- `ptr` - 指向要释放的内存块
**返回:**
**复杂度:** O(1)(底层释放 + 统计更新)
**示例:**
```cpp
#include <XCEngine/Memory/MemoryManager.h>
#include <XCEngine/Memory/ProxyAllocator.h>
IAllocator* sysAlloc = MemoryManager::Get().GetSystemAllocator();
ProxyAllocator proxy(sysAlloc, "TrackedAlloc");
void* p1 = proxy.Allocate(512);
void* p2 = proxy.Allocate(256);
proxy.Free(p1);
proxy.Free(p2);
const auto& stats = proxy.GetStats();
// totalFreed 累加了每次释放时的 allocationCount次数非字节
// allocationCount 最终为 0
printf("Total allocated: %zu bytes, Freed count: %zu, Current count: %zu\n",
stats.totalAllocated, stats.totalFreed, stats.allocationCount);
```
## 相关文档
- [ProxyAllocator 总览](proxy-allocator.md) - 返回类总览