mirror of
https://github.com/ToyB-Chan/minimal-heap.git
synced 2026-07-13 21:51:16 +02:00
performance speedup
This commit is contained in:
@@ -24,6 +24,7 @@ MIHP_HeapInfo* MIHP_CreateHeap(MIHP_HeapConfig config)
|
||||
heap->Config = config;
|
||||
|
||||
heap->FirstArea = MIHP_CreateHeapMemoryArea(heap, config.MinimalMemoryAreaSize);
|
||||
heap->LastAllocationArea = heap->FirstArea;
|
||||
if (heap->FirstArea == NULL)
|
||||
{
|
||||
MIHP_PlatformFreeMemory(heap, sizeof(MIHP_HeapInfo));
|
||||
@@ -70,12 +71,24 @@ void* MIHP_Allocate(MIHP_HeapInfo* heap, size_t size)
|
||||
|
||||
MIHP_LOCK_HEAP(&heap->HeapLock);
|
||||
|
||||
bool checkedLastAllocationArea = false;
|
||||
MIHP_HeapMemoryAreaHeader* nextArea = heap->FirstArea;
|
||||
while (nextArea)
|
||||
{
|
||||
MIHP_HeapMemoryAreaHeader* currentArea = nextArea;
|
||||
MIHP_ValidateHeapMemoryAreaHeader(heap, currentArea);
|
||||
MIHP_HeapMemoryAreaHeader* currentArea = NULL;
|
||||
|
||||
if (!checkedLastAllocationArea)
|
||||
{
|
||||
currentArea = heap->LastAllocationArea;
|
||||
checkedLastAllocationArea = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentArea = nextArea;
|
||||
nextArea = currentArea->NextArea;
|
||||
}
|
||||
|
||||
MIHP_ValidateHeapMemoryAreaHeader(heap, currentArea);
|
||||
|
||||
if (currentArea->NumOccupiedSegments == currentArea->NumSegments)
|
||||
continue;
|
||||
@@ -108,6 +121,8 @@ void* MIHP_Allocate(MIHP_HeapInfo* heap, size_t size)
|
||||
currentArea->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(currentArea);
|
||||
|
||||
heap->Stats.NumTotalOccupiedSegments++;
|
||||
heap->Stats.TotalOccupiedSize += targetSegment->OccupiedSize;
|
||||
heap->LastAllocationArea = currentArea;
|
||||
|
||||
MIHP_UNLOCK_HEAP(&heap->HeapLock);
|
||||
|
||||
@@ -163,6 +178,8 @@ bool MIHP_Free(MIHP_HeapInfo* heap, void* ptr)
|
||||
MIHP_HeapMemoryAreaHeader* area = segment->OwningMemoryArea;
|
||||
MIHP_ValidateHeapMemoryAreaHeader(heap, area);
|
||||
|
||||
size_t occupiedSize = segment->OccupiedSize;
|
||||
|
||||
segment->OccupiedSize = 0;
|
||||
segment->Checksum = MIHP_GenerateHeapSegmentChecksum(segment);
|
||||
|
||||
@@ -170,6 +187,7 @@ bool MIHP_Free(MIHP_HeapInfo* heap, void* ptr)
|
||||
area->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(area);
|
||||
|
||||
heap->Stats.NumTotalOccupiedSegments--;
|
||||
heap->Stats.TotalOccupiedSize -= occupiedSize;
|
||||
|
||||
if (area->NumOccupiedSegments == 0 && heap->Stats.NumMemoryAreas > 1)
|
||||
{
|
||||
|
||||
@@ -28,6 +28,7 @@ typedef struct _MIHP_HeapStats
|
||||
size_t NumTotalSegments;
|
||||
size_t NumTotalOccupiedSegments;
|
||||
size_t TotalSize;
|
||||
size_t TotalOccupiedSize;
|
||||
} MIHP_HeapStats;
|
||||
|
||||
// This structs size must be multiple of 8
|
||||
@@ -73,7 +74,7 @@ typedef struct _MIHP_HeapCorruptionInfo
|
||||
} MIHP_HeapCorruptionInfo;
|
||||
|
||||
/* Hooks to be implemented by the library user */
|
||||
void* MIHP_PlatformRequestMemory(size_t size); // Alignment must at least be alingment of size_t
|
||||
void* MIHP_PlatformRequestMemory(size_t size); // Alignment must at least be alignment of size_t
|
||||
bool MIHP_PlatformFreeMemory(void* ptr, size_t size);
|
||||
void MIHP_PlatformOnHeapCorruptionDetected(MIHP_HeapCorruptionInfo info);
|
||||
/* === */
|
||||
@@ -93,6 +94,7 @@ typedef struct _MIHP_HeapInfo
|
||||
MIHP_HeapConfig Config;
|
||||
MIHP_HeapStats Stats;
|
||||
MIHP_HeapMemoryAreaHeader* FirstArea;
|
||||
MIHP_HeapMemoryAreaHeader* LastSuccessfulAllocationArea;
|
||||
|
||||
MIHP_HEAP_LOCK_TYPE HeapLock;
|
||||
} MIHP_HeapInfo;
|
||||
|
||||
@@ -21,7 +21,7 @@ void MIHP_PlatformOnHeapCorruptionDetected(MIHP_HeapCorruptionInfo info)
|
||||
__debugbreak();
|
||||
}
|
||||
|
||||
#define NUM_ALLOCATIONS (4096 * 512)
|
||||
#define NUM_ALLOCATIONS (4096 * 128)
|
||||
|
||||
void* myptrs[NUM_ALLOCATIONS] = { 0 };
|
||||
|
||||
@@ -31,7 +31,7 @@ int main()
|
||||
config.AllocationInitialValue = 0xbe;
|
||||
config.AllocationAlignment = 16;
|
||||
config.MinimalMemoryAreaSize = 4096 * 32;
|
||||
config.MaximalMemoryAreaSize = 4096ull * 4096ull * 4096ull;
|
||||
config.MaximalMemoryAreaSize = 4096ull * 4096ull;
|
||||
MIHP_HeapInfo* myHeap = MIHP_CreateHeap(config);
|
||||
|
||||
for (int i = 0; i < NUM_ALLOCATIONS; i++)
|
||||
|
||||
Reference in New Issue
Block a user