From 46fb0e20c60ff457757c2463150cc0c2d1e2fdbb Mon Sep 17 00:00:00 2001 From: ToyB-Chan Date: Wed, 10 Jun 2026 07:32:37 +0200 Subject: [PATCH] added totalsizeoccupied stat --- minimal_heap.h | 5 +++++ minimal_heap_implementation.inl | 14 +++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/minimal_heap.h b/minimal_heap.h index 3d2dd0e..8ed0dda 100644 --- a/minimal_heap.h +++ b/minimal_heap.h @@ -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; diff --git a/minimal_heap_implementation.inl b/minimal_heap_implementation.inl index ba5873f..c80308b 100644 --- a/minimal_heap_implementation.inl +++ b/minimal_heap_implementation.inl @@ -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; }