diff --git a/minimal_heap.h b/minimal_heap.h index 1e950fa..cf87117 100644 --- a/minimal_heap.h +++ b/minimal_heap.h @@ -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 diff --git a/minimal_heap_implementation.inl b/minimal_heap_implementation.inl index 6c85019..1372f42 100644 --- a/minimal_heap_implementation.inl +++ b/minimal_heap_implementation.inl @@ -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)