Files
XCSDD/docs/api/memory/allocator/get-allocation-count.md
ssdfasd 58a83f445a fix: improve doc link navigation and tree display
- Fix link resolution with proper relative/absolute path handling
- Improve link styling with underline decoration
- Hide leaf nodes from tree, only show directories
- Fix log file path for packaged app
2026-03-19 12:44:08 +08:00

1.2 KiB

IAllocator::GetAllocationCount

virtual size_t GetAllocationCount() const = 0;

返回当前处于已分配状态(未释放)的内存块数量。此方法用于监控活跃分配的数量。

参数:

返回: 当前已分配块的数量

复杂度: O(1)

示例:

#include <XCEngine/Memory/Allocator.h>

class MyAllocator : public IAllocator {
    size_t m_count = 0;
public:
    void* Allocate(size_t size, size_t alignment = 0) override {
        ++m_count;
        return ::operator new(size);
    }
    
    void Free(void* ptr) override {
        if (ptr) {
            --m_count;
            ::operator delete(ptr);
        }
    }
    
    void* Reallocate(void* ptr, size_t newSize) override { /* ... */ }
    
    size_t GetTotalAllocated() const override { return 0; }
    size_t GetTotalFreed() const override { return 0; }
    size_t GetPeakAllocated() const override { return 0; }
    size_t GetAllocationCount() const override { return m_count; }
    const char* GetName() const override { return "MyAllocator"; }
};

MyAllocator alloc;
alloc.Allocate(64);
alloc.Allocate(128);
size_t count = alloc.GetAllocationCount(); // 返回 2

相关文档