42 lines
910 B
Markdown
42 lines
910 B
Markdown
|
|
# LinearAllocator::GetMarker
|
||
|
|
|
||
|
|
```cpp
|
||
|
|
void* GetMarker() const;
|
||
|
|
```
|
||
|
|
|
||
|
|
获取当前分配位置的标记。标记是一个指向当前偏移量的指针,可用于 `SetMarker` 恢复到该位置。此方法用于实现临时分配的撤销功能。
|
||
|
|
|
||
|
|
**参数:** 无
|
||
|
|
|
||
|
|
**返回:** 当前分配位置的指针标记
|
||
|
|
|
||
|
|
**复杂度:** O(1)
|
||
|
|
|
||
|
|
**示例:**
|
||
|
|
|
||
|
|
```cpp
|
||
|
|
#include <XCEngine/Memory/LinearAllocator.h>
|
||
|
|
|
||
|
|
LinearAllocator allocator(1024);
|
||
|
|
|
||
|
|
// 分配一些数据
|
||
|
|
void* ptr1 = allocator.Allocate(128);
|
||
|
|
|
||
|
|
// 保存标记(用于回滚点)
|
||
|
|
void* marker = allocator.GetMarker();
|
||
|
|
|
||
|
|
// 分配临时数据
|
||
|
|
void* temp = allocator.Allocate(64);
|
||
|
|
void* temp2 = allocator.Allocate(32);
|
||
|
|
|
||
|
|
// 临时数据用完了,恢复到标记位置
|
||
|
|
allocator.SetMarker(marker);
|
||
|
|
|
||
|
|
// 此时 temp 和 temp2 的内存已被回收
|
||
|
|
// ptr1 仍然有效
|
||
|
|
```
|
||
|
|
|
||
|
|
## 相关文档
|
||
|
|
|
||
|
|
- [LinearAllocator 总览](linear-allocator.md) - 返回类总览
|