added totalsizeoccupied stat

This commit is contained in:
2026-06-10 07:32:37 +02:00
parent e507521bf8
commit 46fb0e20c6
2 changed files with 16 additions and 3 deletions

View File

@@ -68,15 +68,20 @@ typedef struct _MIHP_HeapConfig
typedef struct _MIHP_HeapStats
{
size_t NumMemoryAreas;
size_t NumTotalSegments;
size_t NumTotalOccupiedSegments;
size_t TotalSize;
size_t TotalSizeOccupied;
} MIHP_HeapStats;
// This structs size must be multiple of 8
typedef struct _MIHP_HeapMemoryAreaHeader
{
size_t AreaSize;
size_t AreaSizeOccupied;
size_t NumSegments;
size_t NumOccupiedSegments;

View File

@@ -170,6 +170,7 @@ void* MIHP_Allocate(MIHP_Heap* heap, size_t size)
// Set it to currentSegment as it may be a large segment we've split targetSegment of
currentArea->LastSuccessfulAllocationSegment = currentSegment;
currentArea->AreaSizeOccupied += targetSegment->SegmentSize;
currentArea->NumOccupiedSegments++;
currentArea->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(heap, currentArea);
@@ -325,6 +326,7 @@ bool MIHP_Free(MIHP_Heap* heap, void* ptr)
segment->OccupiedSize = 0;
segment->Checksum = MIHP_GenerateHeapSegmentChecksum(heap, segment);
area->AreaSizeOccupied -= segment->SegmentSize;
area->NumOccupiedSegments--;
area->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(heap, area);
@@ -431,6 +433,7 @@ bool MIHP_MergeHeaps(MIHP_Heap* sourceHeap, MIHP_Heap* heapToAbsorb)
sourceHeap->Stats.NumTotalSegments += heapToAbsorb->Stats.NumTotalSegments;
sourceHeap->Stats.NumTotalOccupiedSegments += heapToAbsorb->Stats.NumTotalOccupiedSegments;
sourceHeap->Stats.TotalSize += heapToAbsorb->Stats.TotalSize;
sourceHeap->Stats.TotalSizeOccupied += heapToAbsorb->Stats.TotalSizeOccupied;
MIHP_UnlockHeap(heapToAbsorb);
MIHP_MEMSET(heapToAbsorb, sizeof(MIHP_Heap), 0);
@@ -552,17 +555,22 @@ bool MIHP_DestroyHeapMemoryArea(MIHP_Heap* heap, MIHP_HeapMemoryAreaHeader* memo
MIHP_ASSERT(heap);
MIHP_ASSERT(memoryArea);
size_t areaSize = memoryArea->AreaSize;
size_t areaSizeOccupied = memoryArea->AreaSizeOccupied;
size_t areaNumSegments = memoryArea->NumSegments;
size_t areaNumOccupiedSegments = memoryArea->NumOccupiedSegments;
size_t areaSize = memoryArea->AreaSize;
if (!heap->Config.PlatformFreeMemoryFn(memoryArea->InstigatingHeap, memoryArea, memoryArea->AreaSize))
return false;
heap->Stats.NumMemoryAreas--;
heap->Stats.TotalSize -= areaSize;
heap->Stats.TotalSizeOccupied -= areaSizeOccupied;
heap->Stats.NumTotalSegments -= areaNumSegments;
heap->Stats.NumTotalOccupiedSegments -= areaNumOccupiedSegments;
heap->Stats.NumMemoryAreas--;
heap->Stats.TotalSize -= areaSize;
return true;
}