added max heap size

This commit is contained in:
2026-06-11 15:52:01 +02:00
parent e46565edbf
commit 717966dea3
2 changed files with 8 additions and 0 deletions

View File

@@ -51,6 +51,7 @@ typedef struct _MIHP_HeapConfig
size_t MinMemoryAreaSize; // Must be a multiple of AllocationAlignment
size_t MaxMemoryAreaSize; // Must be a multiple of AllocationAlignment
size_t MaxHeapSize;
size_t MinTotalTraversalsBeforeHeapExpansion; // Minimal amounts of traversals during allocation before considering expanding the heap
uint8_t MinHeapTraversalPercentToExpand; // Minimal integer percent of the heap traversed before expanding the heap

View File

@@ -9,6 +9,7 @@ MIHP_HeapConfig MIHP_MakeDefaultConfigPreset()
cfg.MinMemoryAreaSize = 4096 * 32;
cfg.MaxMemoryAreaSize = 4096 * 4096 * 128ull;
cfg.MaxHeapSize = ~0;
cfg.MinTotalTraversalsBeforeHeapExpansion = 10'000;
cfg.MinHeapTraversalPercentToExpand = 80;
@@ -35,6 +36,9 @@ bool MIHP_InitializeHeap(MIHP_Heap* heap, MIHP_HeapConfig config)
if (config.PlatformFreeMemoryFn == NULL)
return false;
if (config.MaxHeapSize < config.MinMemoryAreaSize)
return false;
if (config.MinTotalTraversalsBeforeHeapExpansion < 10)
return false;
@@ -584,6 +588,9 @@ MIHP_HeapMemoryAreaHeader* MIHP_CreateHeapMemoryArea(MIHP_Heap* heap, size_t req
effectiveSize = MIHP_MAX(effectiveSize, heap->Config.MinMemoryAreaSize);
effectiveSize = MIHP_MIN(effectiveSize, heap->Config.MaxMemoryAreaSize);
if (heap->Stats.TotalSize + requestedSize > heap->Config.MaxHeapSize)
return NULL;
size_t actualSize = 0;
MIHP_HeapMemoryAreaHeader* area = (MIHP_HeapMemoryAreaHeader*)heap->Config.PlatformRequestMemoryFn(heap, effectiveSize, &actualSize);
if (area == NULL)