3 Commits

Author SHA1 Message Date
083cb52f54 fixed some more small things 2026-11-05 23:45:08 +01:00
a632291f07 bump version 2026-11-05 18:09:27 +01:00
dcd6c58394 fixed some bugs (tm) 2026-11-05 17:57:07 +01:00
2 changed files with 35 additions and 14 deletions

View File

@@ -2,16 +2,12 @@
#define __MINIMAL_HEAP_H__
#define MIHP_VERSION_MAJOR 1
#define MIHP_VERSION_MINOR 12
#define MIHP_VERSION_MINOR 14
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#ifndef MIHP_IMPLEMENTATION
#define MIHP_IMPLEMENTATION 0
#endif
/* Defines to be configured by the library user */
#ifndef MIHP_ASSERT
#define MIHP_ASSERT(Condition) while(1) { if (Condition) break; *(volatile char*)(0x0) = 0; }
@@ -22,6 +18,10 @@
#endif
/* */
#ifdef __cplusplus
extern "C" {
#endif
typedef uintptr_t MIHP_HeapOpaque;
typedef void*(MIHP_PlatformRequestMemoryFn)(MIHP_HeapOpaque heap, size_t minRequestedSize, size_t* outActualSize);
@@ -217,4 +217,8 @@ size_t MIHP_GetMaximalPayloadSize(const MIHP_Heap* heap);
#include "minimal_heap_implementation.inl"
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -24,7 +24,7 @@ MIHP_HeapConfig MIHP_MakeDefaultConfigPreset()
cfg.AllocateFillValue = 0xbe;
cfg.FreeFillValue = 0xee;
cfg.HeapValidationMode = MIHP_HVM_MagicNumber;
cfg.HeapValidationMode = MIHP_HVM_None;
return cfg;
}
@@ -43,6 +43,12 @@ MIHP_HeapError MIHP_InitializeHeap(MIHP_Heap* heap, MIHP_HeapConfig config)
if (config.PlatformFreeMemoryFn == NULL)
return MIHP_HE_BadConfig;
if (config.OnHeapCorruptionDetectedFn == NULL && config.HeapValidationMode != MIHP_HVM_None)
return MIHP_HE_BadConfig;
if (config.MinMemoryAreaSize < MIHP_GetMinimalPayloadSize(heap) * 2)
return MIHP_HE_BadConfig;
if (config.MaxHeapSize < config.MinMemoryAreaSize)
return MIHP_HE_BadConfig;
@@ -260,7 +266,7 @@ MIHP_HeapResult MIHP_Reallocate(MIHP_Heap* heap, void* ptr, size_t newSize)
segment->Checksum = MIHP_GenerateHeapSegmentChecksum(heap, segment);
MIHP_UnlockHeap(heap);
return MIHP_MAKE_RESULT(NULL, MIHP_HE_Success);
return MIHP_MAKE_RESULT(ptr, MIHP_HE_Success);
}
if (segment->NextSegment)
@@ -399,9 +405,14 @@ MIHP_HeapError MIHP_SetPointerCustomMetadata(MIHP_Heap* heap, void* ptr, MIHP_Al
if (!MIHP_IsAddressInHeap(heap, ptr))
return MIHP_HE_InvalidHeap;
MIHP_LockHeap(heap);
MIHP_HeapSegmentHeader* segment = MIHP_GetHeapSegmentHeaderPtr(heap, ptr);
MIHP_ValidateHeapSegmentHeader(heap, segment);
segment->CustomMetadata = metadata;
segment->Checksum = MIHP_GenerateHeapSegmentChecksum(heap, segment);
MIHP_UnlockHeap(heap);
return MIHP_HE_Success;
}
@@ -413,14 +424,22 @@ MIHP_HeapError MIHP_GetPointerCustomMetadata(MIHP_Heap* heap, void* ptr, MIHP_Al
if (!MIHP_IsAddressInHeap(heap, ptr))
return MIHP_HE_InvalidHeap;
MIHP_LockHeap(heap);
MIHP_HeapSegmentHeader* segment = MIHP_GetHeapSegmentHeaderPtr(heap, ptr);
MIHP_ValidateHeapSegmentHeader(heap, segment);
*outMetadata = segment->CustomMetadata;
MIHP_UnlockHeap(heap);
return MIHP_HE_Success;
}
MIHP_HeapError MIHP_MergeHeaps(MIHP_Heap* sourceHeap, MIHP_Heap* heapToAbsorb)
{
if (sourceHeap == heapToAbsorb)
return MIHP_HE_InvalidParameter;
if (!MIHP_IsHeapInitialized(sourceHeap))
return MIHP_HE_InvalidHeap;
@@ -493,7 +512,7 @@ bool MIHP_IsAddressInHeap(MIHP_Heap* heap, void* ptr)
uintptr_t ptrMin = (uintptr_t)currentArea;
uintptr_t ptrMax = (uintptr_t)currentArea + currentArea->AreaSize;
if (searchingPtr >= ptrMin && searchingPtr <= ptrMax)
if (searchingPtr >= ptrMin && searchingPtr < ptrMax)
{
MIHP_UnlockHeap(heap);
return true;
@@ -561,6 +580,8 @@ MIHP_HeapError MIHP_WalkHeap(MIHP_Heap* heap, MIHP_WalkHeapCallbackFn* callback)
if (callback == NULL)
return MIHP_HE_InvalidParameter;
MIHP_LockHeap(heap);
MIHP_HeapMemoryAreaHeader* nextArea = heap->FirstArea;
while (nextArea != NULL)
{
@@ -579,6 +600,8 @@ MIHP_HeapError MIHP_WalkHeap(MIHP_Heap* heap, MIHP_WalkHeapCallbackFn* callback)
}
}
MIHP_UnlockHeap(heap);
return MIHP_HE_Success;
}
@@ -829,9 +852,6 @@ uint32_t MIHP_GenerateHeapSegmentChecksum(const MIHP_Heap* heap, const MIHP_Heap
void MIHP_ValidateHeapMemoryAreaHeader(const MIHP_Heap* heap, const MIHP_HeapMemoryAreaHeader* area)
{
if (heap->Config.OnHeapCorruptionDetectedFn == NULL)
return;
uint32_t newChecksum;
if (heap->Config.HeapValidationMode == MIHP_HVM_None)
@@ -858,9 +878,6 @@ void MIHP_ValidateHeapMemoryAreaHeader(const MIHP_Heap* heap, const MIHP_HeapMem
void MIHP_ValidateHeapSegmentHeader(const MIHP_Heap* heap, const MIHP_HeapSegmentHeader* segment)
{
if (heap->Config.OnHeapCorruptionDetectedFn == NULL)
return;
uint32_t newChecksum;
if (heap->Config.HeapValidationMode == MIHP_HVM_None)