From 80349be4c009601f179d0149db488f8d62c78950 Mon Sep 17 00:00:00 2001 From: Iliyas Jorio Date: Sat, 21 Jan 2023 14:16:12 +0100 Subject: [PATCH] GetPtrSize; Heap stats --- src/Memory/Memory.cpp | 33 +++++++++++++++++++++++++-------- src/Pomme.h | 13 +++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Memory/Memory.cpp b/src/Memory/Memory.cpp index 294ebf2..5128616 100644 --- a/src/Memory/Memory.cpp +++ b/src/Memory/Memory.cpp @@ -19,6 +19,9 @@ static std::set gLivePtrNums; static constexpr int kBlockDescriptorPadding = 32; static_assert(sizeof(BlockDescriptor) <= kBlockDescriptorPadding); +static size_t gTotalHeapSize = 0; +static size_t gNumBlocksAllocated = 0; + //----------------------------------------------------------------------------- // Implementation-specific stuff @@ -33,6 +36,9 @@ BlockDescriptor* BlockDescriptor::Allocate(uint32_t size) block->ptrToData = buf + kBlockDescriptorPadding; block->rezMeta = nullptr; + gTotalHeapSize += kBlockDescriptorPadding + size; + gNumBlocksAllocated++; + #if POMME_PTR_TRACKING block->ptrBatch = gCurrentPtrBatch; block->ptrNumInBatch = gCurrentNumPtrsInBatch++; @@ -47,6 +53,9 @@ void BlockDescriptor::Free(BlockDescriptor* block) if (!block) return; + gTotalHeapSize -= kBlockDescriptorPadding + block->size; + gNumBlocksAllocated--; + block->magic = 'DEAD'; block->size = 0; block->ptrToData = nullptr; @@ -172,12 +181,8 @@ Ptr NewPtr(Size byteCount) if (byteCount > 0x7FFFFFFF) throw std::invalid_argument("trying to alloc massive ptr"); -#if !POMME_PTR_TRACKING - return new char[byteCount]; -#else BlockDescriptor* bd = BlockDescriptor::Allocate((UInt32) byteCount); return bd->ptrToData; -#endif } Ptr NewPtrSys(Size byteCount) @@ -192,18 +197,30 @@ Ptr NewPtrClear(Size byteCount) return ptr; } +Size GetPtrSize(Ptr p) +{ + const BlockDescriptor* block = BlockDescriptor::PtrToBlock(p); + return block->size; +} + void DisposePtr(Ptr p) { -#if !POMME_PTR_TRACKING - delete[] p; -#else BlockDescriptor::Free(BlockDescriptor::PtrToBlock(p)); -#endif } //----------------------------------------------------------------------------- // Memory: pointer tracking +long Pomme_GetNumAllocs() +{ + return (long) gNumBlocksAllocated; +} + +Size Pomme_GetHeapSize() +{ + return (Size) gTotalHeapSize; +} + void Pomme_FlushPtrTracking(bool issueWarnings) { #if POMME_PTR_TRACKING diff --git a/src/Pomme.h b/src/Pomme.h index aecdd16..ac1d211 100644 --- a/src/Pomme.h +++ b/src/Pomme.h @@ -430,8 +430,21 @@ Ptr NewPtrSys(Size); Ptr NewPtrClear(Size); +Size GetPtrSize(Ptr p); + void DisposePtr(Ptr p); +//----------------------------------------------------------------------------- +// Memory: heap statistics + +// Pomme extension: +// Returns amount of Ptrs and Handles currently live +long Pomme_GetNumAllocs(void); + +// Pomme extension: +// Returns lower bound of total heap allocated by application +Size Pomme_GetHeapSize(void); + //----------------------------------------------------------------------------- // Memory: pointer tracking