mirror of
https://github.com/ToyB-Chan/minimal-heap.git
synced 2026-07-13 21:51:16 +02:00
added max heap size
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user