2026-03-18 17:49:22 +08:00
|
|
|
|
# IAllocator
|
|
|
|
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Memory`
|
|
|
|
|
|
|
|
|
|
|
|
**类型**: `class` (abstract interface)
|
|
|
|
|
|
|
|
|
|
|
|
**描述**: 内存分配器抽象接口,定义标准分配协议。
|
|
|
|
|
|
|
|
|
|
|
|
## 概述
|
|
|
|
|
|
|
|
|
|
|
|
`IAllocator` 是 XCEngine 内存管理系统的核心抽象接口。它定义了分配、释放和重新分配内存的标准方法,以及内存统计接口。所有专用分配器(LinearAllocator、PoolAllocator、ProxyAllocator)都实现此接口。
|
|
|
|
|
|
|
|
|
|
|
|
## 公共方法
|
|
|
|
|
|
|
|
|
|
|
|
### 内存操作
|
|
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 |
|
|
|
|
|
|
|------|------|
|
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
|
|
|
|
| `Allocate` | 分配内存 |
|
|
|
|
|
|
| `Free` | 释放内存 |
|
|
|
|
|
|
| `Reallocate` | 重新分配内存 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
### 统计信息
|
|
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 |
|
|
|
|
|
|
|------|------|
|
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
|
|
|
|
| `GetTotalAllocated` | 获取已分配总字节数 |
|
|
|
|
|
|
| `GetTotalFreed` | 获取已释放总字节数 |
|
|
|
|
|
|
| `GetPeakAllocated` | 获取峰值分配字节数 |
|
|
|
|
|
|
| `GetAllocationCount` | 获取分配次数 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
### 元信息
|
|
|
|
|
|
|
|
|
|
|
|
| 方法 | 描述 |
|
|
|
|
|
|
|------|------|
|
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
|
|
|
|
| `GetName` | 获取分配器名称 |
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
## 使用示例
|
|
|
|
|
|
|
|
|
|
|
|
```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/Allocator.h>
|
2026-03-18 17:49:22 +08:00
|
|
|
|
|
|
|
|
|
|
class MyAllocator : public IAllocator {
|
|
|
|
|
|
public:
|
|
|
|
|
|
void* Allocate(size_t size, size_t alignment = 0) override {
|
|
|
|
|
|
return ::operator new(size);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Free(void* ptr) override {
|
|
|
|
|
|
::operator delete(ptr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* Reallocate(void* ptr, size_t newSize) override {
|
|
|
|
|
|
void* newPtr = Allocate(newSize);
|
|
|
|
|
|
Free(ptr);
|
|
|
|
|
|
return newPtr;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t GetTotalAllocated() const override { return m_allocated; }
|
|
|
|
|
|
size_t GetTotalFreed() const override { return m_freed; }
|
|
|
|
|
|
size_t GetPeakAllocated() const override { return m_peak; }
|
|
|
|
|
|
size_t GetAllocationCount() const override { return m_count; }
|
|
|
|
|
|
const char* GetName() const override { return "MyAllocator"; }
|
|
|
|
|
|
};
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
|
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) - 返回模块总览
|
|
|
|
|
|
- [MemoryManager](../manager/manager.md) - 内存管理器
|
|
|
|
|
|
- [LinearAllocator](../linear-allocator/linear-allocator.md) - 线性分配器
|
|
|
|
|
|
- [PoolAllocator](../pool-allocator/pool-allocator.md) - 内存池分配器
|