From 58fef4fc9e9b0ac87d8fc2054e4142b83af541ef Mon Sep 17 00:00:00 2001 From: ToyB-Chan Date: Sat, 13 Jun 2026 20:23:52 +0200 Subject: [PATCH] fixed allocations with sizes smaller than the max area limited but bigger than the max payload size never return --- minimal_heap.h | 1 + minimal_heap_implementation.inl | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/minimal_heap.h b/minimal_heap.h index 5c19746..4715e91 100644 --- a/minimal_heap.h +++ b/minimal_heap.h @@ -189,6 +189,7 @@ void MIHP_ValidateHeapMemoryAreaHeader(const MIHP_Heap* heap, const MIHP_HeapMem void MIHP_ValidateHeapSegmentHeader(const MIHP_Heap* heap, const MIHP_HeapSegmentHeader* segment); size_t MIHP_GetMinimalPayloadSize(const MIHP_Heap* heap); +size_t MIHP_GetMaximalPayloadSize(const MIHP_Heap* heap); /* */ #if MIHP_IMPLEMENTATION diff --git a/minimal_heap_implementation.inl b/minimal_heap_implementation.inl index 0881bec..e77cfc5 100644 --- a/minimal_heap_implementation.inl +++ b/minimal_heap_implementation.inl @@ -1,3 +1,4 @@ +#include "minimal_heap.h" #define MIHP_MIN(A, B) ((A) < (B) ? (A) : (B)) #define MIHP_MAX(A, B) ((A) > (B) ? (A) : (B)) @@ -117,7 +118,7 @@ void* MIHP_Allocate(MIHP_Heap* heap, size_t size) if (size == 0) return NULL; - if (size > heap->Config.MaxMemoryAreaSize) + if (size > MIHP_GetMaximalPayloadSize(heap)) return NULL; MIHP_LockHeap(heap); @@ -871,3 +872,11 @@ size_t MIHP_GetMinimalPayloadSize(const MIHP_Heap* heap) { return MIHP_MAX(sizeof(MIHP_HeapSegmentHeader), heap->Config.AllocationAlignment); } + +inline size_t MIHP_GetMaximalPayloadSize(const MIHP_Heap* heap) +{ + size_t maxPayloadSize = heap->Config.MaxMemoryAreaSize; + maxPayloadSize -= MIHP_GetHeapAlignedSize(heap, sizeof(_MIHP_HeapMemoryAreaHeader)); + maxPayloadSize -= MIHP_GetHeapAlignedSize(heap, sizeof(MIHP_HeapSegmentHeader)); + return maxPayloadSize; +}