40 lines
1.0 KiB
Markdown
40 lines
1.0 KiB
Markdown
|
|
# ProxyAllocator::ProxyAllocator
|
||
|
|
|
||
|
|
```cpp
|
||
|
|
ProxyAllocator(IAllocator* underlying, const char* name);
|
||
|
|
```
|
||
|
|
|
||
|
|
构造一个代理分配器,包装底层分配器并记录分配统计。所有 `Allocate`、`Free`、`Reallocate` 调用都会被转发到底层分配器,同时记录统计信息。名称用于日志和报告。
|
||
|
|
|
||
|
|
**参数:**
|
||
|
|
- `underlying` - 被包装的底层分配器,不能为 `nullptr`
|
||
|
|
- `name` - 代理分配器的名称字符串
|
||
|
|
|
||
|
|
**返回:** 无
|
||
|
|
|
||
|
|
**复杂度:** O(1)
|
||
|
|
|
||
|
|
**线程安全:** ✅ (内部使用 mutex 保护统计数据)
|
||
|
|
|
||
|
|
**示例:**
|
||
|
|
|
||
|
|
```cpp
|
||
|
|
#include <XCEngine/Memory/MemoryManager.h>
|
||
|
|
#include <XCEngine/Memory/ProxyAllocator.h>
|
||
|
|
|
||
|
|
MemoryManager::Get().Initialize();
|
||
|
|
|
||
|
|
// 使用系统分配器作为底层
|
||
|
|
IAllocator* sysAlloc = MemoryManager::Get().GetSystemAllocator();
|
||
|
|
ProxyAllocator proxy(sysAlloc, "TempAllocations");
|
||
|
|
|
||
|
|
// 通过代理分配
|
||
|
|
void* ptr = proxy.Allocate(1024);
|
||
|
|
proxy.Free(ptr);
|
||
|
|
```
|
||
|
|
|
||
|
|
## 相关文档
|
||
|
|
|
||
|
|
- [ProxyAllocator 总览](proxy-allocator.md) - 返回类总览
|
||
|
|
- [`~ProxyAllocator`](~proxy-allocator.md) - 析构函数
|