mirror of
https://github.com/jorio/Pomme.git
synced 2024-11-26 13:49:22 +00:00
GetPtrSize; Heap stats
This commit is contained in:
parent
dae4b61b5b
commit
80349be4c0
@ -19,6 +19,9 @@ static std::set<uint32_t> 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
|
||||
|
13
src/Pomme.h
13
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user