2026-03-26 16:45:24 +08:00
|
|
|
# MemoryManager
|
|
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Memory`
|
|
|
|
|
|
|
|
|
|
**类型**: `class (singleton)`
|
|
|
|
|
|
|
|
|
|
**头文件**: `XCEngine/Memory/MemoryManager.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
|
|
|
`MemoryManager` 当前是内存模块的全局入口,但它的职责比名字听起来更窄。它主要做三件事:
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
- 创建和保存一个内部 `SystemAllocator`
|
|
|
|
|
- 提供若干分配器工厂方法
|
|
|
|
|
- 预留追踪、泄漏报告和报表接口
|
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
|
|
|
- `XE_ALLOC(allocator, size, ...)`
|
|
|
|
|
- `XE_FREE(allocator, ptr)`
|
2026-03-26 16:45:24 +08:00
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
## 当前实现边界
|
|
|
|
|
|
|
|
|
|
- 只有在 [Initialize](Initialize.md) 之后,`GetSystemAllocator()` 才会返回有效系统分配器。
|
|
|
|
|
- `CreateLinearAllocator()` 即使在未初始化时也能工作,因为 `LinearAllocator` 在父分配器为空时会走自身的直接分配路径。
|
|
|
|
|
- `CreateProxyAllocator()` 在未初始化时会创建一个底层分配器为空的对象,后续实际分配将产生未定义行为,因此应在初始化后使用。
|
|
|
|
|
- 使用系统分配器作为底层或父分配器创建出来的对象,应在 [Shutdown](Shutdown.md) 之前销毁,否则会留下悬空指针。
|
|
|
|
|
- `SetTrackAllocations()` 当前只修改标志位,不驱动统计逻辑。
|
|
|
|
|
- `DumpMemoryLeaks()` 和 `GenerateMemoryReport()` 当前只向 `stdout` 输出标题。
|
|
|
|
|
|
|
|
|
|
## 生命周期
|
|
|
|
|
|
|
|
|
|
- [Get](Get.md) 返回进程级单例。
|
|
|
|
|
- [Initialize](Initialize.md) 创建内部系统分配器。
|
|
|
|
|
- [Shutdown](Shutdown.md) 销毁内部系统分配器。
|
|
|
|
|
|
|
|
|
|
## 线程语义
|
|
|
|
|
|
|
|
|
|
- 当前管理器自身没有锁。
|
|
|
|
|
- 建议在启动和受控退出阶段使用,不要在多个线程中并发初始化或关闭。
|
|
|
|
|
|
|
|
|
|
## 公开方法
|
|
|
|
|
|
|
|
|
|
| 方法 | 说明 |
|
2026-03-26 16:45:24 +08:00
|
|
|
|------|------|
|
2026-03-26 18:02:29 +08:00
|
|
|
| [Get](Get.md) | 获取全局 `MemoryManager` 实例。 |
|
|
|
|
|
| [Initialize](Initialize.md) | 初始化内存管理器。 |
|
|
|
|
|
| [Shutdown](Shutdown.md) | 关闭内存管理器。 |
|
|
|
|
|
| [GetSystemAllocator](GetSystemAllocator.md) | 获取内部系统分配器。 |
|
|
|
|
|
| [CreateLinearAllocator](CreateLinearAllocator.md) | 创建线性分配器。 |
|
|
|
|
|
| [CreatePoolAllocator](CreatePoolAllocator.md) | 创建池分配器。 |
|
|
|
|
|
| [CreateProxyAllocator](CreateProxyAllocator.md) | 创建代理分配器。 |
|
|
|
|
|
| [SetTrackAllocations](SetTrackAllocations.md) | 设置是否跟踪分配;当前仅保存标志位。 |
|
|
|
|
|
| [DumpMemoryLeaks](DumpMemoryLeaks.md) | 输出泄漏报告标题。 |
|
|
|
|
|
| [GenerateMemoryReport](GenerateMemoryReport.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)
|