2026-03-26 16:45:24 +08:00
|
|
|
|
# LinearAllocator
|
|
|
|
|
|
|
|
|
|
|
|
**命名空间**: `XCEngine::Memory`
|
|
|
|
|
|
|
|
|
|
|
|
**类型**: `class`
|
|
|
|
|
|
|
|
|
|
|
|
**头文件**: `XCEngine/Memory/LinearAllocator.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
|
|
|
|
`LinearAllocator` 是典型的 bump / arena allocator:每次分配只推进一个偏移量,不跟踪单个块,也不支持逐块释放。它适合:
|
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
|
|
|
|
## 生命周期与所有权
|
2026-03-26 16:45:24 +08:00
|
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
|
- [Constructor](Constructor.md) 创建一块固定容量缓冲区。
|
|
|
|
|
|
- 如果传入父分配器,底层缓冲由父分配器提供并在析构时归还给父分配器。
|
|
|
|
|
|
- 如果没有父分配器,当前实现会直接在构造函数中申请原生内存。
|
|
|
|
|
|
- [Clear](Clear.md) 会把偏移量重置为零,相当于一次性回收所有线性分配结果。
|
2026-03-26 16:45:24 +08:00
|
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
|
## 当前实现限制
|
|
|
|
|
|
|
|
|
|
|
|
- [Free](Free.md) 目前是空实现,不会释放单个块。
|
|
|
|
|
|
- [Reallocate](Reallocate.md) 目前始终返回 `nullptr`。
|
|
|
|
|
|
- `GetMarker` / `SetMarker` 使用的是“偏移量令牌”,不是缓冲区中的真实指针。
|
|
|
|
|
|
- `GetPeakAllocated` 当前直接返回总容量,而不是历史峰值使用量。
|
|
|
|
|
|
- `GetAllocationCount` 当前始终返回 `0`。
|
|
|
|
|
|
- `Allocate` 会尝试处理对齐,但当前实现通过“增加消耗大小”而不是“返回对齐后的指针”来处理 padding,因此不要过度依赖高阶自定义对齐保证。
|
|
|
|
|
|
|
|
|
|
|
|
## 线程语义
|
|
|
|
|
|
|
|
|
|
|
|
- 当前没有锁;应由调用方在单线程或外部同步前提下使用。
|
|
|
|
|
|
|
|
|
|
|
|
## 公开方法
|
|
|
|
|
|
|
|
|
|
|
|
| 方法 | 说明 |
|
2026-03-26 16:45:24 +08:00
|
|
|
|
|------|------|
|
2026-03-26 18:02:29 +08:00
|
|
|
|
| [Constructor](Constructor.md) | 构造线性分配器。 |
|
|
|
|
|
|
| [Destructor](Destructor.md) | 销毁分配器并释放底层缓冲。 |
|
|
|
|
|
|
| [Allocate](Allocate.md) | 从当前偏移量分配一块内存。 |
|
|
|
|
|
|
| [Free](Free.md) | 释放单个块;当前为空实现。 |
|
|
|
|
|
|
| [Reallocate](Reallocate.md) | 重分配块;当前返回 `nullptr`。 |
|
|
|
|
|
|
| [Clear](Clear.md) | 清空所有线性分配结果。 |
|
|
|
|
|
|
| [GetMarker](GetMarker.md) | 获取当前偏移标记。 |
|
|
|
|
|
|
| [SetMarker](SetMarker.md) | 回退或推进到指定偏移标记。 |
|
|
|
|
|
|
| [GetUsedSize](GetUsedSize.md) | 查询当前已用大小。 |
|
|
|
|
|
|
| [GetCapacity](GetCapacity.md) | 查询总容量。 |
|
|
|
|
|
|
| [GetName](GetName.md) | 查询分配器名称。 |
|
|
|
|
|
|
| [GetTotalAllocated](GetTotalAllocated.md) | 查询当前已用字节数。 |
|
|
|
|
|
|
| [GetTotalFreed](GetTotalFreed.md) | 查询累计释放量;当前固定为 `0`。 |
|
|
|
|
|
|
| [GetPeakAllocated](GetPeakAllocated.md) | 查询峰值分配量;当前返回容量。 |
|
|
|
|
|
|
| [GetAllocationCount](GetAllocationCount.md) | 查询分配次数;当前固定为 `0`。 |
|
2026-03-26 16:45:24 +08:00
|
|
|
|
|
|
|
|
|
|
## 相关文档
|
|
|
|
|
|
|
2026-03-26 18:02:29 +08:00
|
|
|
|
- [当前模块](../Memory.md)
|
|
|
|
|
|
- [IAllocator](../Allocator/Allocator.md)
|
|
|
|
|
|
- [Allocator Selection And Current Limits](../../../_guides/Memory/Allocator-Selection-And-Current-Limits.md)
|