2026-03-26 18:02:29 +08:00
|
|
|
# IAllocator
|
2026-03-26 16:45:24 +08:00
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Memory`
|
|
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
**类型**: `class (abstract interface)`
|
2026-03-26 16:45:24 +08:00
|
|
|
|
|
|
|
|
**头文件**: `XCEngine/Memory/Allocator.h`
|
|
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
**描述**: 定义统一的分配、释放、重分配和统计查询接口。
|
2026-03-26 16:45:24 +08:00
|
|
|
|
|
|
|
|
## 概述
|
|
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
`IAllocator` 是当前内存模块的基础抽象。它把“如何申请和回收一块内存”统一成一组稳定接口,让上层代码不必直接依赖具体分配器实现。
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
它的职责分成两部分:
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
- 内存操作:`Allocate`、`Free`、`Reallocate`
|
|
|
|
|
- 统计查询:`GetTotalAllocated`、`GetTotalFreed`、`GetPeakAllocated`、`GetAllocationCount`
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
## 设计目的
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
这类接口在引擎里很有价值,因为:
|
|
|
|
|
|
|
|
|
|
- 容器、资源系统和工具系统可以接受任意分配器实现。
|
|
|
|
|
- 不同策略可以共存,例如线性分配器、对象池、系统分配器代理。
|
|
|
|
|
- 调试和统计逻辑可以围绕接口做组合,而不是侵入所有调用点。
|
|
|
|
|
|
|
|
|
|
## 当前实现约束
|
|
|
|
|
|
|
|
|
|
- `IAllocator` 只定义形状,不保证线程安全。
|
|
|
|
|
- 统计方法的语义由具体实现决定;当前不同分配器的统计精度并不一致。
|
|
|
|
|
- 不是每个实现都完整支持 `Free` 和 `Reallocate`。
|
|
|
|
|
|
|
|
|
|
## 已知实现
|
|
|
|
|
|
|
|
|
|
- [LinearAllocator](../LinearAllocator/LinearAllocator.md)
|
|
|
|
|
- [PoolAllocator](../PoolAllocator/PoolAllocator.md)
|
|
|
|
|
- [ProxyAllocator](../ProxyAllocator/ProxyAllocator.md)
|
|
|
|
|
- `SystemAllocator`:当前仅存在于 `engine/src/Memory/Memory.cpp` 内部,不是公共类型。
|
|
|
|
|
|
|
|
|
|
## 公开方法
|
|
|
|
|
|
|
|
|
|
| 方法 | 说明 |
|
2026-03-26 16:45:24 +08:00
|
|
|
|------|------|
|
2026-03-26 18:02:29 +08:00
|
|
|
| [Destructor](Destructor.md) | 虚析构函数。 |
|
|
|
|
|
| [Allocate](Allocate.md) | 申请内存。 |
|
|
|
|
|
| [Free](Free.md) | 释放内存。 |
|
|
|
|
|
| [Reallocate](Reallocate.md) | 调整已分配内存大小。 |
|
|
|
|
|
| [GetTotalAllocated](GetTotalAllocated.md) | 查询累计已分配量。 |
|
|
|
|
|
| [GetTotalFreed](GetTotalFreed.md) | 查询累计已释放量。 |
|
|
|
|
|
| [GetPeakAllocated](GetPeakAllocated.md) | 查询峰值分配量。 |
|
|
|
|
|
| [GetAllocationCount](GetAllocationCount.md) | 查询当前或累计分配次数。 |
|
|
|
|
|
| [GetName](GetName.md) | 查询分配器名称。 |
|
2026-03-26 16:45:24 +08:00
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
- [当前模块](../Memory.md)
|
|
|
|
|
- [Allocator Selection And Current Limits](../../../_guides/Memory/Allocator-Selection-And-Current-Limits.md)
|