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