made passed heap to platform functions opaque as its not always safe to dereference

This commit is contained in:
2026-06-11 15:59:28 +02:00
parent 717966dea3
commit 8b033bb7d3
5 changed files with 15 additions and 13 deletions

View File

@@ -21,8 +21,10 @@
#endif #endif
/* */ /* */
typedef void*(MIHP_PlatformRequestMemoryFn)(const struct _MIHP_Heap* heap, size_t minRequestedSize, size_t* outActualSize); typedef uintptr_t MIHP_HeapOpaque;
typedef bool (MIHP_PlatformFreeMemoryFn)(const struct _MIHP_Heap* heap, void* ptr, size_t actualSize);
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 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_OnHeapCorruptionDetectedFn)(const struct _MIHP_Heap* heap, struct _MIHP_HeapCorruptionInfo corruptionInfo);
typedef void(MIHP_LockHeapFn)(const struct _MIHP_Heap* heap, void* heapLock); typedef void(MIHP_LockHeapFn)(MIHP_HeapOpaque heap, void* heapLock);
typedef void(MIHP_UnlockHeapFn)(const struct _MIHP_Heap* heap, void* heapLock); typedef void(MIHP_UnlockHeapFn)(MIHP_HeapOpaque heap, void* heapLock);
typedef enum _MIHP_HeapValidationMode typedef enum _MIHP_HeapValidationMode
{ {
@@ -87,7 +89,7 @@ typedef struct _MIHP_HeapMemoryAreaHeader
struct _MIHP_HeapSegmentHeader* LastSuccessfulAllocationSegment; struct _MIHP_HeapSegmentHeader* LastSuccessfulAllocationSegment;
// TODO: Change this to an InstigatingHeapUuid (opaque ptr) // 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 ! uint32_t Checksum; // Checksum must be the last member !
} MIHP_HeapMemoryAreaHeader; } MIHP_HeapMemoryAreaHeader;

View File

@@ -571,13 +571,13 @@ bool MIHP_WalkHeap(MIHP_Heap* heap, MIHP_WalkHeapCallbackFn* callback)
void MIHP_LockHeap(MIHP_Heap* heap) void MIHP_LockHeap(MIHP_Heap* heap)
{ {
if (heap->Config.HeapLock) 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) void MIHP_UnlockHeap(MIHP_Heap* heap)
{ {
if (heap->Config.HeapLock) 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) 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; return NULL;
size_t actualSize = 0; 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) if (area == NULL)
return NULL; return NULL;
MIHP_ASSERT(actualSize >= effectiveSize); MIHP_ASSERT(actualSize >= effectiveSize);
MIHP_MEMSET(area, sizeof(MIHP_HeapMemoryAreaHeader), 0); MIHP_MEMSET(area, sizeof(MIHP_HeapMemoryAreaHeader), 0);
area->InstigatingHeap = heap; area->InstigatingHeap = (MIHP_HeapOpaque)heap;
area->AreaSize = actualSize; area->AreaSize = actualSize;
area->Checksum = ~0; area->Checksum = ~0;

View File

@@ -12,13 +12,13 @@
#define MIHP_ValidateHeap(x) #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; *outActualSize = minRequestedSize;
return malloc(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); free(ptr);
return true; return true;

View File

@@ -124,7 +124,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="minimal_heap_test.c" /> <ClCompile Include="minimal_heap_test.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="minimal_heap.h" /> <ClInclude Include="minimal_heap.h" />

View File

@@ -15,7 +15,7 @@
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="minimal_heap_test.c"> <ClCompile Include="minimal_heap_test.cpp">
<Filter>Quelldateien</Filter> <Filter>Quelldateien</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>