51 lines
1.3 KiB
Markdown
51 lines
1.3 KiB
Markdown
|
|
# IAllocator::GetTotalFreed
|
|||
|
|
|
|||
|
|
```cpp
|
|||
|
|
virtual size_t GetTotalFreed() const = 0;
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
返回此分配器自创建以来累计释放的字节总数。部分分配器(如 LinearAllocator)可能始终返回 0,因为它们不跟踪单个释放操作。
|
|||
|
|
|
|||
|
|
**参数:** 无
|
|||
|
|
|
|||
|
|
**返回:** 累计已释放的字节数
|
|||
|
|
|
|||
|
|
**复杂度:** O(1)
|
|||
|
|
|
|||
|
|
**示例:**
|
|||
|
|
|
|||
|
|
```cpp
|
|||
|
|
#include <XCEngine/Memory/IAllocator.h>
|
|||
|
|
|
|||
|
|
class MyAllocator : public IAllocator {
|
|||
|
|
size_t m_freed = 0;
|
|||
|
|
public:
|
|||
|
|
void* Allocate(size_t size, size_t alignment = 0) override { return ::operator new(size); }
|
|||
|
|
|
|||
|
|
void Free(void* ptr) override {
|
|||
|
|
if (ptr) {
|
|||
|
|
size_t size = 256; // 需要外部记录
|
|||
|
|
::operator delete(ptr);
|
|||
|
|
m_freed += size;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void* Reallocate(void* ptr, size_t newSize) override { /* ... */ }
|
|||
|
|
|
|||
|
|
size_t GetTotalAllocated() const override { return 0; }
|
|||
|
|
size_t GetTotalFreed() const override { return m_freed; }
|
|||
|
|
size_t GetPeakAllocated() const override { return 0; }
|
|||
|
|
size_t GetAllocationCount() const override { return 0; }
|
|||
|
|
const char* GetName() const override { return "MyAllocator"; }
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
MyAllocator alloc;
|
|||
|
|
void* ptr = alloc.Allocate(128);
|
|||
|
|
alloc.Free(ptr);
|
|||
|
|
size_t freed = alloc.GetTotalFreed(); // 返回 128
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## 相关文档
|
|||
|
|
|
|||
|
|
- [IAllocator 总览](allocator.md) - 返回类总览
|