Files
XCSDD/docs/api/memory/proxy-allocator/free.md
ssdfasd 58a83f445a fix: improve doc link navigation and tree display
- Fix link resolution with proper relative/absolute path handling
- Improve link styling with underline decoration
- Hide leaf nodes from tree, only show directories
- Fix log file path for packaged app
2026-03-19 12:44:08 +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) - 返回类总览