mirror of
https://github.com/ToyB-Chan/minimal-heap.git
synced 2026-07-13 21:51:16 +02:00
@@ -68,20 +68,15 @@ typedef struct _MIHP_HeapConfig
|
|||||||
typedef struct _MIHP_HeapStats
|
typedef struct _MIHP_HeapStats
|
||||||
{
|
{
|
||||||
size_t NumMemoryAreas;
|
size_t NumMemoryAreas;
|
||||||
|
|
||||||
size_t NumTotalSegments;
|
size_t NumTotalSegments;
|
||||||
size_t NumTotalOccupiedSegments;
|
size_t NumTotalOccupiedSegments;
|
||||||
|
|
||||||
size_t TotalSize;
|
size_t TotalSize;
|
||||||
size_t TotalSizeOccupied;
|
|
||||||
} MIHP_HeapStats;
|
} MIHP_HeapStats;
|
||||||
|
|
||||||
// This structs size must be multiple of 8
|
// This structs size must be multiple of 8
|
||||||
typedef struct _MIHP_HeapMemoryAreaHeader
|
typedef struct _MIHP_HeapMemoryAreaHeader
|
||||||
{
|
{
|
||||||
size_t AreaSize;
|
size_t AreaSize;
|
||||||
size_t AreaSizeOccupied;
|
|
||||||
|
|
||||||
size_t NumSegments;
|
size_t NumSegments;
|
||||||
size_t NumOccupiedSegments;
|
size_t NumOccupiedSegments;
|
||||||
|
|
||||||
|
|||||||
@@ -170,7 +170,6 @@ 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
|
// Set it to currentSegment as it may be a large segment we've split targetSegment of
|
||||||
currentArea->LastSuccessfulAllocationSegment = currentSegment;
|
currentArea->LastSuccessfulAllocationSegment = currentSegment;
|
||||||
|
|
||||||
currentArea->AreaSizeOccupied += targetSegment->SegmentSize;
|
|
||||||
currentArea->NumOccupiedSegments++;
|
currentArea->NumOccupiedSegments++;
|
||||||
currentArea->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(heap, currentArea);
|
currentArea->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(heap, currentArea);
|
||||||
|
|
||||||
@@ -326,7 +325,6 @@ bool MIHP_Free(MIHP_Heap* heap, void* ptr)
|
|||||||
segment->OccupiedSize = 0;
|
segment->OccupiedSize = 0;
|
||||||
segment->Checksum = MIHP_GenerateHeapSegmentChecksum(heap, segment);
|
segment->Checksum = MIHP_GenerateHeapSegmentChecksum(heap, segment);
|
||||||
|
|
||||||
area->AreaSizeOccupied -= segment->SegmentSize;
|
|
||||||
area->NumOccupiedSegments--;
|
area->NumOccupiedSegments--;
|
||||||
area->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(heap, area);
|
area->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(heap, area);
|
||||||
|
|
||||||
@@ -433,7 +431,6 @@ bool MIHP_MergeHeaps(MIHP_Heap* sourceHeap, MIHP_Heap* heapToAbsorb)
|
|||||||
sourceHeap->Stats.NumTotalSegments += heapToAbsorb->Stats.NumTotalSegments;
|
sourceHeap->Stats.NumTotalSegments += heapToAbsorb->Stats.NumTotalSegments;
|
||||||
sourceHeap->Stats.NumTotalOccupiedSegments += heapToAbsorb->Stats.NumTotalOccupiedSegments;
|
sourceHeap->Stats.NumTotalOccupiedSegments += heapToAbsorb->Stats.NumTotalOccupiedSegments;
|
||||||
sourceHeap->Stats.TotalSize += heapToAbsorb->Stats.TotalSize;
|
sourceHeap->Stats.TotalSize += heapToAbsorb->Stats.TotalSize;
|
||||||
sourceHeap->Stats.TotalSizeOccupied += heapToAbsorb->Stats.TotalSizeOccupied;
|
|
||||||
|
|
||||||
MIHP_UnlockHeap(heapToAbsorb);
|
MIHP_UnlockHeap(heapToAbsorb);
|
||||||
MIHP_MEMSET(heapToAbsorb, sizeof(MIHP_Heap), 0);
|
MIHP_MEMSET(heapToAbsorb, sizeof(MIHP_Heap), 0);
|
||||||
@@ -555,22 +552,17 @@ bool MIHP_DestroyHeapMemoryArea(MIHP_Heap* heap, MIHP_HeapMemoryAreaHeader* memo
|
|||||||
MIHP_ASSERT(heap);
|
MIHP_ASSERT(heap);
|
||||||
MIHP_ASSERT(memoryArea);
|
MIHP_ASSERT(memoryArea);
|
||||||
|
|
||||||
size_t areaSize = memoryArea->AreaSize;
|
|
||||||
size_t areaSizeOccupied = memoryArea->AreaSizeOccupied;
|
|
||||||
|
|
||||||
size_t areaNumSegments = memoryArea->NumSegments;
|
size_t areaNumSegments = memoryArea->NumSegments;
|
||||||
size_t areaNumOccupiedSegments = memoryArea->NumOccupiedSegments;
|
size_t areaNumOccupiedSegments = memoryArea->NumOccupiedSegments;
|
||||||
|
size_t areaSize = memoryArea->AreaSize;
|
||||||
|
|
||||||
if (!heap->Config.PlatformFreeMemoryFn(memoryArea->InstigatingHeap, memoryArea, memoryArea->AreaSize))
|
if (!heap->Config.PlatformFreeMemoryFn(memoryArea->InstigatingHeap, memoryArea, memoryArea->AreaSize))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
heap->Stats.NumMemoryAreas--;
|
|
||||||
|
|
||||||
heap->Stats.TotalSize -= areaSize;
|
|
||||||
heap->Stats.TotalSizeOccupied -= areaSizeOccupied;
|
|
||||||
|
|
||||||
heap->Stats.NumTotalSegments -= areaNumSegments;
|
heap->Stats.NumTotalSegments -= areaNumSegments;
|
||||||
heap->Stats.NumTotalOccupiedSegments -= areaNumOccupiedSegments;
|
heap->Stats.NumTotalOccupiedSegments -= areaNumOccupiedSegments;
|
||||||
|
heap->Stats.NumMemoryAreas--;
|
||||||
|
heap->Stats.TotalSize -= areaSize;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user