42 lines
957 B
Markdown
42 lines
957 B
Markdown
|
|
# LinearAllocator::SetMarker
|
||
|
|
|
||
|
|
```cpp
|
||
|
|
void SetMarker(void* marker);
|
||
|
|
```
|
||
|
|
|
||
|
|
恢复到之前通过 `GetMarker` 获取的标记位置。所有在标记之后的分配都将被丢弃,内部偏移量重置为该标记位置。此方法不会释放内存,只是移动偏移量指针。
|
||
|
|
|
||
|
|
**参数:**
|
||
|
|
- `marker` - 通过 `GetMarker` 获取的标记指针
|
||
|
|
|
||
|
|
**返回:** 无
|
||
|
|
|
||
|
|
**复杂度:** O(1)
|
||
|
|
|
||
|
|
**示例:**
|
||
|
|
|
||
|
|
```cpp
|
||
|
|
#include <XCEngine/Memory/LinearAllocator.h>
|
||
|
|
|
||
|
|
LinearAllocator allocator(1024);
|
||
|
|
|
||
|
|
// 基础数据
|
||
|
|
void* base = allocator.Allocate(256);
|
||
|
|
void* marker = allocator.GetMarker();
|
||
|
|
|
||
|
|
// 可选的扩展数据
|
||
|
|
void* ext1 = allocator.Allocate(64);
|
||
|
|
void* ext2 = allocator.Allocate(128);
|
||
|
|
|
||
|
|
// 决定不使用扩展数据,回滚
|
||
|
|
allocator.SetMarker(marker);
|
||
|
|
|
||
|
|
// 扩展数据内存已被回收
|
||
|
|
// 可以重新分配其他数据
|
||
|
|
void* new_data = allocator.Allocate(128);
|
||
|
|
```
|
||
|
|
|
||
|
|
## 相关文档
|
||
|
|
|
||
|
|
- [LinearAllocator 总览](linear-allocator.md) - 返回类总览
|