From 083cb52f5420135af50b69b0691d64ab6224f11a Mon Sep 17 00:00:00 2001 From: ToyB-Chan Date: Thu, 5 Nov 2026 23:45:08 +0100 Subject: [PATCH] fixed some more small things --- minimal_heap.h | 10 +++++++++- minimal_heap_implementation.inl | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/minimal_heap.h b/minimal_heap.h index 7e56b99..1c79be3 100644 --- a/minimal_heap.h +++ b/minimal_heap.h @@ -2,7 +2,7 @@ #define __MINIMAL_HEAP_H__ #define MIHP_VERSION_MAJOR 1 -#define MIHP_VERSION_MINOR 13 +#define MIHP_VERSION_MINOR 14 #include #include @@ -18,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); @@ -213,4 +217,8 @@ size_t MIHP_GetMaximalPayloadSize(const MIHP_Heap* heap); #include "minimal_heap_implementation.inl" #endif +#ifdef __cplusplus + } +#endif + #endif diff --git a/minimal_heap_implementation.inl b/minimal_heap_implementation.inl index 391557b..9c30c0a 100644 --- a/minimal_heap_implementation.inl +++ b/minimal_heap_implementation.inl @@ -405,10 +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; } @@ -420,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; @@ -500,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; @@ -568,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) { @@ -586,6 +600,8 @@ MIHP_HeapError MIHP_WalkHeap(MIHP_Heap* heap, MIHP_WalkHeapCallbackFn* callback) } } + MIHP_UnlockHeap(heap); + return MIHP_HE_Success; }