From eaddbd31fd3ccfc4df9566ba923a065ed3bc7705 Mon Sep 17 00:00:00 2001 From: ToyB-Chan Date: Tue, 9 Jun 2026 03:30:38 +0200 Subject: [PATCH] added first heuristic --- minimal_heap.h | 2 +- minimal_heap_implementation.inl | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/minimal_heap.h b/minimal_heap.h index d83a04a..3d0c32b 100644 --- a/minimal_heap.h +++ b/minimal_heap.h @@ -1,4 +1,4 @@ -/* Version 1.3b */ +/* Version 1.3c */ #ifndef __MINIMAL_HEAP_H__ #define __MINIMAL_HEAP_H__ diff --git a/minimal_heap_implementation.inl b/minimal_heap_implementation.inl index 9e8f459..3ae2aec 100644 --- a/minimal_heap_implementation.inl +++ b/minimal_heap_implementation.inl @@ -81,6 +81,8 @@ void* MIHP_Allocate(MIHP_Heap* heap, size_t size) MIHP_LOCK_HEAP(&heap->HeapLock); + size_t numTraversedSegments = 0; + bool triedLastAllocationArea = false; MIHP_HeapMemoryAreaHeader* nextArea = heap->FirstArea; while (nextArea) @@ -106,6 +108,11 @@ void* MIHP_Allocate(MIHP_Heap* heap, size_t size) MIHP_HeapSegmentHeader* nextSegment = currentArea->FirstSegment; while (nextSegment) { + if (numTraversedSegments * 100 > heap->Stats.NumTotalSegments * 50) + goto AllocateNewArea; + + numTraversedSegments++; + MIHP_HeapSegmentHeader* currentSegment = nextSegment; MIHP_ValidateHeapSegmentHeader(heap, currentSegment); nextSegment = nextSegment->NextSegment; @@ -141,6 +148,8 @@ void* MIHP_Allocate(MIHP_Heap* heap, size_t size) } } +AllocateNewArea: + size_t newAreaSize = heap->Stats.TotalSize; while (newAreaSize < size)