From 8b033bb7d3af9fccce2022f6cd551c578ddd2f57 Mon Sep 17 00:00:00 2001 From: ToyB-Chan Date: Thu, 11 Jun 2026 15:59:28 +0200 Subject: [PATCH] made passed heap to platform functions opaque as its not always safe to dereference --- minimal_heap.h | 12 +++++++----- minimal_heap_implementation.inl | 8 ++++---- minimal_heap_test.c => minimal_heap_test.cpp | 4 ++-- minimal_heap_test.vcxproj | 2 +- minimal_heap_test.vcxproj.filters | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) rename minimal_heap_test.c => minimal_heap_test.cpp (94%) diff --git a/minimal_heap.h b/minimal_heap.h index cf87117..f1d10f6 100644 --- a/minimal_heap.h +++ b/minimal_heap.h @@ -21,8 +21,10 @@ #endif /* */ -typedef void*(MIHP_PlatformRequestMemoryFn)(const struct _MIHP_Heap* heap, size_t minRequestedSize, size_t* outActualSize); -typedef bool (MIHP_PlatformFreeMemoryFn)(const struct _MIHP_Heap* heap, void* ptr, size_t actualSize); +typedef uintptr_t MIHP_HeapOpaque; + +typedef void*(MIHP_PlatformRequestMemoryFn)(MIHP_HeapOpaque heap, size_t minRequestedSize, size_t* outActualSize); +typedef bool (MIHP_PlatformFreeMemoryFn)(MIHP_HeapOpaque heap, void* ptr, size_t actualSize); typedef enum _MIHP_HEAP_CORRUPTION_ERROR { @@ -33,8 +35,8 @@ typedef enum _MIHP_HEAP_CORRUPTION_ERROR typedef void (MIHP_OnHeapCorruptionDetectedFn)(const struct _MIHP_Heap* heap, struct _MIHP_HeapCorruptionInfo corruptionInfo); -typedef void(MIHP_LockHeapFn)(const struct _MIHP_Heap* heap, void* heapLock); -typedef void(MIHP_UnlockHeapFn)(const struct _MIHP_Heap* heap, void* heapLock); +typedef void(MIHP_LockHeapFn)(MIHP_HeapOpaque heap, void* heapLock); +typedef void(MIHP_UnlockHeapFn)(MIHP_HeapOpaque heap, void* heapLock); typedef enum _MIHP_HeapValidationMode { @@ -87,7 +89,7 @@ typedef struct _MIHP_HeapMemoryAreaHeader struct _MIHP_HeapSegmentHeader* LastSuccessfulAllocationSegment; // TODO: Change this to an InstigatingHeapUuid (opaque ptr) - const struct _MIHP_Heap* InstigatingHeap; // Heap that created this area. May not always be the owning heap (in case of a merge) or safe to deference! + MIHP_HeapOpaque InstigatingHeap; // Heap that created this area. uint32_t Checksum; // Checksum must be the last member ! } MIHP_HeapMemoryAreaHeader; diff --git a/minimal_heap_implementation.inl b/minimal_heap_implementation.inl index 1372f42..3b22d59 100644 --- a/minimal_heap_implementation.inl +++ b/minimal_heap_implementation.inl @@ -571,13 +571,13 @@ bool MIHP_WalkHeap(MIHP_Heap* heap, MIHP_WalkHeapCallbackFn* callback) void MIHP_LockHeap(MIHP_Heap* heap) { if (heap->Config.HeapLock) - heap->Config.LockHeapFn(heap, heap->Config.HeapLock); + heap->Config.LockHeapFn((MIHP_HeapOpaque)heap, heap->Config.HeapLock); } void MIHP_UnlockHeap(MIHP_Heap* heap) { if (heap->Config.HeapLock) - heap->Config.UnlockHeapFn(heap, heap->Config.HeapLock); + heap->Config.UnlockHeapFn((MIHP_HeapOpaque)heap, heap->Config.HeapLock); } MIHP_HeapMemoryAreaHeader* MIHP_CreateHeapMemoryArea(MIHP_Heap* heap, size_t requestedSize) @@ -592,14 +592,14 @@ MIHP_HeapMemoryAreaHeader* MIHP_CreateHeapMemoryArea(MIHP_Heap* heap, size_t req return NULL; size_t actualSize = 0; - MIHP_HeapMemoryAreaHeader* area = (MIHP_HeapMemoryAreaHeader*)heap->Config.PlatformRequestMemoryFn(heap, effectiveSize, &actualSize); + MIHP_HeapMemoryAreaHeader* area = (MIHP_HeapMemoryAreaHeader*)heap->Config.PlatformRequestMemoryFn((MIHP_HeapOpaque)heap, effectiveSize, &actualSize); if (area == NULL) return NULL; MIHP_ASSERT(actualSize >= effectiveSize); MIHP_MEMSET(area, sizeof(MIHP_HeapMemoryAreaHeader), 0); - area->InstigatingHeap = heap; + area->InstigatingHeap = (MIHP_HeapOpaque)heap; area->AreaSize = actualSize; area->Checksum = ~0; diff --git a/minimal_heap_test.c b/minimal_heap_test.cpp similarity index 94% rename from minimal_heap_test.c rename to minimal_heap_test.cpp index fb42496..7f57335 100644 --- a/minimal_heap_test.c +++ b/minimal_heap_test.cpp @@ -12,13 +12,13 @@ #define MIHP_ValidateHeap(x) -void* RequestMemory(const MIHP_Heap* heap, size_t minRequestedSize, size_t* outActualSize) +void* RequestMemory(MIHP_HeapOpaque heap, size_t minRequestedSize, size_t* outActualSize) { *outActualSize = minRequestedSize; return malloc(minRequestedSize); } -bool FreeMemory(const MIHP_Heap* heap, void* ptr, size_t actualSize) +bool FreeMemory(MIHP_HeapOpaque heap, void* ptr, size_t actualSize) { free(ptr); return true; diff --git a/minimal_heap_test.vcxproj b/minimal_heap_test.vcxproj index 06693a7..92a73e8 100644 --- a/minimal_heap_test.vcxproj +++ b/minimal_heap_test.vcxproj @@ -124,7 +124,7 @@ - + diff --git a/minimal_heap_test.vcxproj.filters b/minimal_heap_test.vcxproj.filters index aad3cae..b1b7f20 100644 --- a/minimal_heap_test.vcxproj.filters +++ b/minimal_heap_test.vcxproj.filters @@ -15,7 +15,7 @@ - + Quelldateien