17 Commits

3 changed files with 151 additions and 75 deletions

View File

@@ -1,4 +1,4 @@
/* Version 1.2 */
/* Version 1.XX */
#ifndef __MINIMAL_HEAP_H__
#define __MINIMAL_HEAP_H__
@@ -32,9 +32,9 @@
/* */
/* Hooks to be implemented by the library user */
void* MIHP_PlatformRequestMemory(size_t minRequestedSize, size_t* outActualSize); // Alignment must at least be alignment of size_t, outActualSize must at least be minRequestedSize
bool MIHP_PlatformFreeMemory(void* ptr, size_t actualSize);
void MIHP_PlatformOnHeapCorruptionDetected(struct _MIHP_HeapCorruptionInfo info);
void* MIHP_PlatformRequestMemory(const struct _MIHP_Heap* heap, size_t minRequestedSize, size_t* outActualSize); // Alignment must at least be alignment of size_t, outActualSize must at least be minRequestedSize
bool MIHP_PlatformFreeMemory(const struct _MIHP_Heap* heap, void* ptr, size_t actualSize);
void MIHP_PlatformOnHeapCorruptionDetected(const struct _MIHP_Heap* heap, struct _MIHP_HeapCorruptionInfo info);
/* */
#define MIHP_HEAP_CORRUPTION_TYPE_MEMORY_AREA_CHECKSUM_MISMATCH 0
@@ -45,8 +45,8 @@ typedef struct _MIHP_HeapConfig
{
size_t AllocationAlignment; // Must be power of two and at least sizeof(size_t)
size_t MinimalMemoryAreaSize; // Must be a multiple of AllocationAlingment
size_t MaximalMemoryAreaSize; // Must be a multiple of AllocationAlingment
size_t MinimalMemoryAreaSize; // Must be a multiple of AllocationAlignment
size_t MaximalMemoryAreaSize; // Must be a multiple of AllocationAlignment
char AllocationInitialValue;
} MIHP_HeapConfig;
@@ -69,8 +69,9 @@ typedef struct _MIHP_HeapMemoryAreaHeader
struct _MIHP_HeapMemoryAreaHeader* NextArea;
struct _MIHP_HeapMemoryAreaHeader* PreviousArea;
struct _MIHP_Heap* OwningHeap;
struct _MIHP_HeapSegmentHeader* FirstSegment;
struct _MIHP_Heap* OwningHeap;
struct _MIHP_HeapSegmentHeader* FirstSegment;
struct _MIHP_HeapSegmentHeaderFreePayload* FirstFreeSegment;
uint32_t Checksum; // Checksum must be the last member !
@@ -92,9 +93,14 @@ typedef struct _MIHP_HeapSegmentHeader
} MIHP_HeapSegmentHeader;
typedef struct _MIHP_HeapSegmentHeaderFreePayload
{
struct _MIHP_HeapSegmentHeaderFreePayload* PreviousFreeSegment;
struct _MIHP_HeapSegmentHeaderFreePayload* NextFreeSegment;
} MIHP_HeapSegmentHeaderFreePayload;
typedef struct _MIHP_HeapCorruptionInfo
{
const struct _MIHP_Heap* Heap;
uint8_t Type;
const void* Ptr;
uint32_t ExpectedValue;
@@ -108,14 +114,14 @@ typedef struct _MIHP_Heap
MIHP_HeapMemoryAreaHeader* FirstArea;
MIHP_HeapMemoryAreaHeader* LastSuccessfulAllocationArea;
size_t HeapStructureAllocationSize;
MIHP_HEAP_LOCK_TYPE HeapLock;
} MIHP_Heap;
/* API Interface */
MIHP_Heap* MIHP_CreateHeap(MIHP_HeapConfig config);
bool MIHP_DestroyHeap(MIHP_Heap* heap, bool force);
bool MIHP_InitializeHeap(MIHP_Heap* heap, MIHP_HeapConfig config);
bool MIHP_IsHeapInitialized(const MIHP_Heap* heap);
bool MIHP_UninitializeHeap(MIHP_Heap* heap, bool force);
void* MIHP_Allocate(MIHP_Heap* heap, size_t size);
void* MIHP_Realloc(MIHP_Heap* heap, void* ptr, size_t newSize);
bool MIHP_Free(MIHP_Heap* heap, void* ptr);
@@ -132,9 +138,15 @@ bool MIHP_DestroyHeapMemoryArea(MIHP_Heap* heap, MIHP_HeapMemoryAreaHeader* area
MIHP_HeapSegmentHeader* MIHP_InitializeHeapSegment(MIHP_HeapMemoryAreaHeader* area, void* segmentStart, size_t size);
bool MIHP_UninitializeHeapSegment(MIHP_HeapSegmentHeader* segment);
bool MIHP_SplitHeapSegment(MIHP_HeapSegmentHeader* sourceSegment, size_t newSegmentSize, MIHP_HeapSegmentHeader** optOutNewSegment);
bool MIHP_SplitHeapSegment(MIHP_HeapSegmentHeader* sourceSegment, size_t newSegmentSize, MIHP_HeapSegmentHeader** outNewSegment);
bool MIHP_MergeHeapSegments(MIHP_HeapSegmentHeader* sourceSegment, MIHP_HeapSegmentHeader* segmentToAbsorb);
void MIHP_AddSegmentToFreeList(MIHP_Heap* heap, MIHP_HeapSegmentHeader* segment);
void MIHP_RemoveSegmentFromFreeList(MIHP_Heap* heap, MIHP_HeapSegmentHeader* segment);
void* MIHP_GetSegmentPayloadPtr(MIHP_Heap* heap, MIHP_HeapSegmentHeader* segment);
MIHP_HeapSegmentHeader* MIHP_GetSegmentHeaderPtr(MIHP_Heap* heap, void* payloadPtr);
size_t MIHP_GetHeapAlignedSize(const MIHP_Heap* heap, size_t size);
uint32_t MIHP_HashMemoryRegion(const void* data, size_t size);

View File

@@ -5,49 +5,52 @@
#define MIHP_IS_PO2(Val) (Val != 0 && (Val & (Val - 1)) == 0)
MIHP_Heap* MIHP_CreateHeap(MIHP_HeapConfig config)
bool MIHP_InitializeHeap(MIHP_Heap* heap, MIHP_HeapConfig config)
{
if (heap == NULL)
return false;
if (MIHP_IsHeapInitialized(heap))
return false;
if (config.AllocationAlignment < sizeof(size_t))
return NULL;
return false;
if (!MIHP_IS_PO2(config.AllocationAlignment))
return NULL;
return false;
if (config.MinimalMemoryAreaSize % config.AllocationAlignment != 0)
return NULL;
return false;
if (config.MaximalMemoryAreaSize % config.AllocationAlignment != 0)
return NULL;
return false;
size_t actualHeapStructAllocSize = 0;
MIHP_Heap* heap = (MIHP_Heap*)MIHP_PlatformRequestMemory(sizeof(MIHP_Heap), &actualHeapStructAllocSize);
if (heap == NULL)
return NULL;
MIHP_ASSERT(actualHeapStructAllocSize >= sizeof(MIHP_Heap));
MIHP_MEMSET(heap, sizeof(MIHP_Heap), 0);
heap->Config = config;
heap->HeapStructureAllocationSize = actualHeapStructAllocSize;
heap->FirstArea = MIHP_CreateHeapMemoryArea(heap, actualHeapStructAllocSize);
heap->FirstArea = MIHP_CreateHeapMemoryArea(heap, config.MinimalMemoryAreaSize);
heap->LastSuccessfulAllocationArea = heap->FirstArea;
if (heap->FirstArea == NULL)
{
MIHP_PlatformFreeMemory(heap, heap->HeapStructureAllocationSize);
return NULL;
}
heap->FirstArea->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(heap->FirstArea);
return heap;
}
bool MIHP_DestroyHeap(MIHP_Heap* heap, bool force)
bool MIHP_IsHeapInitialized(const MIHP_Heap* heap)
{
if (heap == NULL)
return false;
return heap->FirstArea != NULL;
}
bool MIHP_UninitializeHeap(MIHP_Heap* heap, bool force)
{
if (heap == NULL)
return false;
if (!MIHP_IsHeapInitialized(heap))
return false;
if (!force && heap->Stats.NumTotalOccupiedSegments > 0)
return false;
@@ -61,7 +64,7 @@ bool MIHP_DestroyHeap(MIHP_Heap* heap, bool force)
MIHP_DestroyHeapMemoryArea(heap, currentArea);
}
MIHP_PlatformFreeMemory(heap, heap->HeapStructureAllocationSize);
MIHP_MEMSET(heap, sizeof(MIHP_Heap), 0);
return true;
}
@@ -100,12 +103,12 @@ void* MIHP_Allocate(MIHP_Heap* heap, size_t size)
if (currentArea->NumOccupiedSegments == currentArea->NumSegments)
continue;
MIHP_HeapSegmentHeader* nextSegment = currentArea->FirstSegment;
while (nextSegment)
MIHP_HeapSegmentHeaderFreePayload* nextFreeSegmentPayload = currentArea->FirstFreeSegment;
while (nextFreeSegmentPayload)
{
MIHP_HeapSegmentHeader* currentSegment = nextSegment;
MIHP_HeapSegmentHeader* currentSegment = MIHP_GetSegmentHeaderPtr(heap, nextFreeSegmentPayload);
MIHP_ValidateHeapSegmentHeader(heap, currentSegment);
nextSegment = currentSegment->NextSegment;
nextFreeSegmentPayload = nextFreeSegmentPayload->NextFreeSegment;
if (currentSegment->OccupiedSize != 0)
continue;
@@ -120,6 +123,7 @@ void* MIHP_Allocate(MIHP_Heap* heap, size_t size)
else
continue;
MIHP_RemoveSegmentFromFreeList(heap, targetSegment);
targetSegment->OccupiedSize = size;
targetSegment->Checksum = MIHP_GenerateHeapSegmentChecksum(targetSegment);
@@ -131,7 +135,7 @@ void* MIHP_Allocate(MIHP_Heap* heap, size_t size)
MIHP_UNLOCK_HEAP(&heap->HeapLock);
void* data = (char*)targetSegment + segmentHeaderSize;
void* data = MIHP_GetSegmentPayloadPtr(heap, targetSegment);
MIHP_MEMSET(data, size, heap->Config.AllocationInitialValue);
return data;
@@ -178,10 +182,10 @@ void* MIHP_Realloc(MIHP_Heap* heap, void* ptr, size_t newSize)
MIHP_LOCK_HEAP(&heap->HeapLock);
size_t segmentHeaderSize = MIHP_GetHeapAlignedSize(heap, sizeof(MIHP_HeapSegmentHeader));
MIHP_HeapSegmentHeader* segment = (MIHP_HeapSegmentHeader*)((char*)ptr - segmentHeaderSize);
MIHP_HeapSegmentHeader* segment = MIHP_GetSegmentHeaderPtr(heap, ptr);
MIHP_ValidateHeapSegmentHeader(heap, segment);
size_t segmentHeaderSize = MIHP_GetHeapAlignedSize(heap, sizeof(MIHP_HeapSegmentHeader));
if (newSize > segment->OccupiedSize)
{
size_t maxPayloadSize = segment->SegmentSize - segmentHeaderSize;
@@ -250,8 +254,7 @@ bool MIHP_Free(MIHP_Heap* heap, void* ptr)
MIHP_LOCK_HEAP(&heap->HeapLock);
size_t segmentHeaderSize = MIHP_GetHeapAlignedSize(heap, sizeof(MIHP_HeapSegmentHeader));
MIHP_HeapSegmentHeader* segment = (MIHP_HeapSegmentHeader*)((char*)ptr - segmentHeaderSize);
MIHP_HeapSegmentHeader* segment = MIHP_GetSegmentHeaderPtr(heap, ptr);
MIHP_ValidateHeapSegmentHeader(heap, segment);
MIHP_HeapMemoryAreaHeader* area = segment->OwningMemoryArea;
@@ -260,11 +263,10 @@ bool MIHP_Free(MIHP_Heap* heap, void* ptr)
if (segment->OccupiedSize == 0)
{
MIHP_HeapCorruptionInfo corruptionInfo = {0};
corruptionInfo.Heap = heap;
corruptionInfo.Type = MIHP_HEAP_CORRUPTION_TYPE_DOUBLE_FREE;
corruptionInfo.Ptr = ptr;
MIHP_PlatformOnHeapCorruptionDetected(corruptionInfo);
MIHP_PlatformOnHeapCorruptionDetected(heap, corruptionInfo);
return false;
}
@@ -276,6 +278,8 @@ bool MIHP_Free(MIHP_Heap* heap, void* ptr)
heap->Stats.NumTotalOccupiedSegments--;
MIHP_AddSegmentToFreeList(heap, segment);
if (area->NumOccupiedSegments == 0 && heap->Stats.NumMemoryAreas > 1)
{
MIHP_HeapMemoryAreaHeader* prevArea = area->PreviousArea;
@@ -393,7 +397,7 @@ MIHP_HeapMemoryAreaHeader* MIHP_CreateHeapMemoryArea(MIHP_Heap* heap, size_t req
effectiveSize = MIHP_MIN(effectiveSize, heap->Config.MaximalMemoryAreaSize);
size_t actualSize = 0;
MIHP_HeapMemoryAreaHeader* area = (MIHP_HeapMemoryAreaHeader*)MIHP_PlatformRequestMemory(effectiveSize, &actualSize);
MIHP_HeapMemoryAreaHeader* area = (MIHP_HeapMemoryAreaHeader*)MIHP_PlatformRequestMemory(heap, effectiveSize, &actualSize);
if (area == NULL)
return NULL;
@@ -423,7 +427,7 @@ bool MIHP_DestroyHeapMemoryArea(MIHP_Heap* heap, MIHP_HeapMemoryAreaHeader* memo
size_t areaNumOccupiedSegments = memoryArea->NumOccupiedSegments;
size_t areaSize = memoryArea->AreaSize;
if (!MIHP_PlatformFreeMemory(memoryArea, memoryArea->AreaSize))
if (!MIHP_PlatformFreeMemory(heap, memoryArea, memoryArea->AreaSize))
return false;
heap->Stats.NumTotalSegments -= areaNumSegments;
@@ -452,6 +456,8 @@ MIHP_HeapSegmentHeader* MIHP_InitializeHeapSegment(MIHP_HeapMemoryAreaHeader* ar
area->OwningHeap->Stats.NumTotalSegments++;
MIHP_AddSegmentToFreeList(area->OwningHeap, segment);
return segment;
}
@@ -462,6 +468,8 @@ bool MIHP_UninitializeHeapSegment(MIHP_HeapSegmentHeader* segment)
if (segment->OccupiedSize > 0)
return false;
MIHP_RemoveSegmentFromFreeList(segment->OwningMemoryArea->OwningHeap, segment);
segment->OwningMemoryArea->NumSegments--;
segment->OwningMemoryArea->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(segment->OwningMemoryArea);
segment->OwningMemoryArea->OwningHeap->Stats.NumTotalSegments--;
@@ -471,7 +479,7 @@ bool MIHP_UninitializeHeapSegment(MIHP_HeapSegmentHeader* segment)
return true;
}
bool MIHP_SplitHeapSegment(MIHP_HeapSegmentHeader* sourceSegment, size_t newSegmentMinSize, MIHP_HeapSegmentHeader** optOutNewSegment)
bool MIHP_SplitHeapSegment(MIHP_HeapSegmentHeader* sourceSegment, size_t newSegmentMinSize, MIHP_HeapSegmentHeader** outNewSegment)
{
MIHP_ASSERT(sourceSegment);
@@ -501,12 +509,10 @@ bool MIHP_SplitHeapSegment(MIHP_HeapSegmentHeader* sourceSegment, size_t newSegm
sourceSegment->NextSegment = newSegment;
if (optOutNewSegment)
*optOutNewSegment = newSegment;
newSegment->Checksum = MIHP_GenerateHeapSegmentChecksum(newSegment);
sourceSegment->Checksum = MIHP_GenerateHeapSegmentChecksum(sourceSegment);
*outNewSegment = newSegment;
return true;
}
@@ -536,6 +542,61 @@ bool MIHP_MergeHeapSegments(MIHP_HeapSegmentHeader* sourceSegment, MIHP_HeapSegm
return true;
}
void MIHP_AddSegmentToFreeList(MIHP_Heap* heap, MIHP_HeapSegmentHeader* segment)
{
MIHP_ASSERT(segment->OccupiedSize == 0);
MIHP_HeapMemoryAreaHeader* area = segment->OwningMemoryArea;
MIHP_HeapSegmentHeaderFreePayload* list = (MIHP_HeapSegmentHeaderFreePayload*)MIHP_GetSegmentPayloadPtr(heap, segment);
list->PreviousFreeSegment = NULL;
list->NextFreeSegment = area->FirstFreeSegment;
if (area->FirstFreeSegment)
area->FirstFreeSegment->PreviousFreeSegment = list;
area->FirstFreeSegment = list;
area->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(area);
}
void MIHP_RemoveSegmentFromFreeList(MIHP_Heap* heap, MIHP_HeapSegmentHeader* segment)
{
MIHP_ASSERT(segment->OccupiedSize == 0);
MIHP_HeapMemoryAreaHeader* area = segment->OwningMemoryArea;
MIHP_HeapSegmentHeaderFreePayload* list = (MIHP_HeapSegmentHeaderFreePayload*)MIHP_GetSegmentPayloadPtr(heap, segment);
if (list->PreviousFreeSegment)
list->PreviousFreeSegment->NextFreeSegment = list->NextFreeSegment;
if (list->NextFreeSegment)
list->NextFreeSegment->PreviousFreeSegment = list->PreviousFreeSegment;
if (area->FirstFreeSegment == list)
{
area->FirstFreeSegment = list->NextFreeSegment;
area->Checksum = MIHP_GenerateHeapMemoryAreaChecksum(area);
}
list->NextFreeSegment = NULL;
list->PreviousFreeSegment = NULL;
}
void* MIHP_GetSegmentPayloadPtr(MIHP_Heap* heap, MIHP_HeapSegmentHeader* segment)
{
MIHP_ASSERT(heap);
MIHP_ASSERT(segment);
return (char*)segment + MIHP_GetHeapAlignedSize(heap, sizeof(MIHP_HeapSegmentHeader));
}
MIHP_HeapSegmentHeader* MIHP_GetSegmentHeaderPtr(MIHP_Heap* heap, void* payloadPtr)
{
MIHP_ASSERT(heap);
MIHP_ASSERT(payloadPtr);
return (MIHP_HeapSegmentHeader*)((char*)payloadPtr - MIHP_GetHeapAlignedSize(heap, sizeof(MIHP_HeapSegmentHeader)));
}
size_t MIHP_GetHeapAlignedSize(const MIHP_Heap* heap, size_t size)
{
return (size + (heap->Config.AllocationAlignment - 1)) & ~(heap->Config.AllocationAlignment - 1);
@@ -610,13 +671,12 @@ void MIHP_ValidateHeapMemoryAreaHeader(const MIHP_Heap* heap, const MIHP_HeapMem
if (area->Checksum != newChecksum)
{
MIHP_HeapCorruptionInfo corruptionInfo;
corruptionInfo.Heap = heap;
corruptionInfo.Type = MIHP_HEAP_CORRUPTION_TYPE_MEMORY_AREA_CHECKSUM_MISMATCH;
corruptionInfo.Ptr = area;
corruptionInfo.ExpectedValue = area->Checksum;
corruptionInfo.ActualValue = newChecksum;
MIHP_PlatformOnHeapCorruptionDetected(corruptionInfo);
MIHP_PlatformOnHeapCorruptionDetected(heap, corruptionInfo);
}
}
@@ -637,17 +697,16 @@ void MIHP_ValidateHeapSegmentHeader(const MIHP_Heap* heap, const MIHP_HeapSegmen
if (segment->Checksum != newChecksum)
{
MIHP_HeapCorruptionInfo corruptionInfo;
corruptionInfo.Heap = heap;
corruptionInfo.Type = MIHP_HEAP_CORRUPTION_TYPE_SEGMENT_CHECKSUM_MISMATCH;
corruptionInfo.Ptr = segment;
corruptionInfo.ExpectedValue = segment->Checksum;
corruptionInfo.ActualValue = newChecksum;
MIHP_PlatformOnHeapCorruptionDetected(corruptionInfo);
MIHP_PlatformOnHeapCorruptionDetected(heap, corruptionInfo);
}
}
size_t MIHP_GetMinimalPayloadSize(const MIHP_Heap* heap)
{
return heap->Config.AllocationAlignment;
return MIHP_MAX(heap->Config.AllocationAlignment, sizeof(MIHP_HeapSegmentHeaderFreePayload));
}

View File

@@ -7,37 +7,39 @@
#define USE_MALLOC 0
#define MIHP_ValidateHeap(x)
//#define MIHP_ValidateHeap(x)
void* MIHP_PlatformRequestMemory(size_t minRequestedSize, size_t* outActualSize)
void* MIHP_PlatformRequestMemory(const MIHP_Heap* heap, size_t minRequestedSize, size_t* outActualSize)
{
*outActualSize = minRequestedSize;
return malloc(minRequestedSize);
}
bool MIHP_PlatformFreeMemory(void* ptr, size_t actualSize)
bool MIHP_PlatformFreeMemory(const MIHP_Heap* heap, void* ptr, size_t actualSize)
{
free(ptr);
return true;
}
void MIHP_PlatformOnHeapCorruptionDetected(MIHP_HeapCorruptionInfo info)
void MIHP_PlatformOnHeapCorruptionDetected(const MIHP_Heap* heap, MIHP_HeapCorruptionInfo info)
{
__debugbreak();
}
#define NUM_ALLOCATIONS (4096 * 4)
#define NUM_ALLOCATIONS (4096 * 128)
void* myptrs[NUM_ALLOCATIONS] = { 0 };
char* myptrs[NUM_ALLOCATIONS] = { 0 };
int main()
{
MIHP_HeapConfig config = {0};
config.AllocationInitialValue = 0xbe;
config.AllocationInitialValue = 'f'; //0xbe;
config.AllocationAlignment = 16;
config.MinimalMemoryAreaSize = 4096 * 32;
config.MaximalMemoryAreaSize = 4096ull * 4096ull * 4096ull;
MIHP_Heap* myHeap = MIHP_CreateHeap(config);
MIHP_Heap myHeap = { 0 };
MIHP_InitializeHeap(&myHeap, config);
for (int i = 0; i < NUM_ALLOCATIONS; i++)
{
@@ -45,8 +47,9 @@ int main()
#if USE_MALLOC
myptrs[i] = malloc(size);
#else
myptrs[i] = MIHP_Allocate(myHeap, size);
MIHP_ValidateHeap(myHeap);
myptrs[i] = MIHP_Allocate(&myHeap, size);
myptrs[i][size - 1] = '\0';
MIHP_ValidateHeap(&myHeap);
#endif
}
@@ -59,8 +62,8 @@ int main()
#if USE_MALLOC
free(myptrs[i]);
#else
MIHP_Free(myHeap, myptrs[i]);
MIHP_ValidateHeap(myHeap);
MIHP_Free(&myHeap, myptrs[i]);
MIHP_ValidateHeap(&myHeap);
#endif
myptrs[i] = NULL;
@@ -72,15 +75,17 @@ int main()
for (int i = 0; i < NUM_ALLOCATIONS; i++)
{
volatile int n = i;
if (myptrs[i])
if (!myptrs[i])
{
size_t size = 300 + rand() % 500;
#if USE_MALLOC
myptrs[i] = realloc(myptrs[i], size);
#else
myptrs[i] = MIHP_Realloc(myHeap, myptrs[i], size);
MIHP_ValidateHeap(myHeap);
//myptrs[i] = MIHP_Realloc(&myHeap, myptrs[i], size);
myptrs[i] = MIHP_Allocate(&myHeap, size);
myptrs[i][size - 1] = '\0';
MIHP_ValidateHeap(&myHeap);
#endif
@@ -96,8 +101,8 @@ int main()
#if USE_MALLOC
free(myptrs[i]);
#else
MIHP_Free(myHeap, myptrs[i]);
MIHP_ValidateHeap(myHeap);
MIHP_Free(&myHeap, myptrs[i]);
MIHP_ValidateHeap(&myHeap);
#endif
myptrs[i] = NULL;