fixed allocations with sizes smaller than the max area limited but bigger than the max payload size never return

This commit is contained in:
2026-06-13 20:23:52 +02:00
parent 38bbd53e7e
commit 58fef4fc9e
2 changed files with 11 additions and 1 deletions

View File

@@ -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

View File

@@ -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;
}