2026-03-18 17:49:22 +08:00
|
|
|
# MemoryManager
|
|
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Memory`
|
|
|
|
|
|
|
|
|
|
**类型**: `class` (singleton)
|
|
|
|
|
|
2026-03-20 02:35:24 +08:00
|
|
|
**头文件**: `XCEngine/Memory/MemoryManager.h`
|
|
|
|
|
|
2026-03-18 17:49:22 +08:00
|
|
|
**描述**: 全局内存管理器单例,提供系统分配器和各种专用分配器的创建。
|
|
|
|
|
|
|
|
|
|
## 概述
|
|
|
|
|
|
|
|
|
|
`MemoryManager` 是 XCEngine 内存管理系统的核心单例。它负责维护系统分配器,提供分配器工厂方法,并支持内存泄漏检测和报告。
|
|
|
|
|
|
|
|
|
|
## 单例访问
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 |
|
|
|
|
|
|------|------|
|
2026-03-20 02:35:24 +08:00
|
|
|
| [`Get`](get.md) | 获取单例实例 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
## 公共方法
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 |
|
|
|
|
|
|------|------|
|
2026-03-20 02:35:24 +08:00
|
|
|
| [`Initialize`](initialize.md) | 初始化内存管理器 |
|
|
|
|
|
| [`Shutdown`](shutdown.md) | 关闭内存管理器 |
|
|
|
|
|
| [`GetSystemAllocator`](get-system-allocator.md) | 获取系统默认分配器 |
|
|
|
|
|
| [`CreateLinearAllocator`](create-linear-allocator.md) | 创建线性分配器 |
|
|
|
|
|
| [`CreatePoolAllocator`](create-pool-allocator.md) | 创建内存池分配器 |
|
|
|
|
|
| [`CreateProxyAllocator`](create-proxy-allocator.md) | 创建代理分配器 |
|
|
|
|
|
| [`SetTrackAllocations`](set-track-allocations.md) | 设置是否跟踪分配 |
|
|
|
|
|
| [`DumpMemoryLeaks`](dump-memory-leaks.md) | 输出内存泄漏报告 |
|
|
|
|
|
| [`GenerateMemoryReport`](generate-memory-report.md) | 生成内存使用报告 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
## 宏定义
|
|
|
|
|
|
|
|
|
|
### XE_ALLOC
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#define XE_ALLOC(allocator, size, ...) allocator->Allocate(size, ##__VA_ARGS__)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
内存分配宏。
|
|
|
|
|
|
|
|
|
|
### XE_FREE
|
|
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
|
#define XE_FREE(allocator, ptr) allocator->Free(ptr)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
内存释放宏。
|
|
|
|
|
|
|
|
|
|
## 使用示例
|
|
|
|
|
|
|
|
|
|
```cpp
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
#include <XCEngine/Memory/MemoryManager.h>
|
|
|
|
|
|
2026-03-18 17:49:22 +08:00
|
|
|
// 初始化
|
|
|
|
|
MemoryManager::Get().Initialize();
|
|
|
|
|
|
|
|
|
|
// 获取系统分配器
|
|
|
|
|
IAllocator* sysAlloc = MemoryManager::Get().GetSystemAllocator();
|
|
|
|
|
|
|
|
|
|
// 创建专用分配器
|
|
|
|
|
auto linearAlloc = MemoryManager::Get().CreateLinearAllocator(1024 * 1024);
|
|
|
|
|
auto poolAlloc = MemoryManager::Get().CreatePoolAllocator(sizeof(MyObject), 1000);
|
|
|
|
|
auto proxyAlloc = MemoryManager::Get().CreateProxyAllocator("GameFrame");
|
|
|
|
|
|
|
|
|
|
// 使用宏分配
|
|
|
|
|
void* ptr = XE_ALLOC(proxyAlloc, 256);
|
|
|
|
|
XE_FREE(proxyAlloc, ptr);
|
|
|
|
|
|
|
|
|
|
// 跟踪内存
|
|
|
|
|
MemoryManager::Get().SetTrackAllocations(true);
|
|
|
|
|
MemoryManager::Get().GenerateMemoryReport();
|
|
|
|
|
|
|
|
|
|
// 关闭
|
|
|
|
|
MemoryManager::Get().Shutdown();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
docs: 重构 API 文档结构并修正源码准确性
- 重组文档目录结构: 每个模块的概述页移动到模块子目录
- 重命名 index.md 为 main.md
- 修正所有模块文档中的错误:
- math: FromEuler→FromEulerAngles, TransformDirection 包含缩放, Box 是 OBB, Color::ToRGBA 格式
- containers: 新增 operator==/!= 文档, 补充 std::hash DJB 算法细节
- core: 修复 types 链接错误
- debug: LogLevelToString 返回大写, timestamp 是秒, Profiler 空实现标注, Windows API vs ANSI
- memory: 修复头文件路径, malloc vs operator new, 新增方法文档
- resources: 修复 Shader/Texture 链接错误
- threading: TaskSystem::Wait 空实现标注, ReadWriteLock 重入描述, LambdaTask 链接
- 验证: fix_links.py 确认 0 个断裂引用
2026-03-19 00:22:30 +08:00
|
|
|
- [Memory 模块总览](../memory.md) - 返回模块总览
|
|
|
|
|
- [IAllocator](../allocator/allocator.md) - 分配器接口
|
|
|
|
|
- [LinearAllocator](../linear-allocator/linear-allocator.md) - 线性分配器
|
|
|
|
|
- [PoolAllocator](../pool-allocator/pool-allocator.md) - 内存池分配器
|
|
|
|
|
- [ProxyAllocator](../proxy-allocator/proxy-allocator.md) - 代理分配器
|