more tests

This commit is contained in:
2026-06-08 07:24:17 +02:00
parent 0da008fec7
commit b50b2548ab
3 changed files with 82 additions and 32 deletions

View File

@@ -5,8 +5,16 @@
#define MIHP_MEMSET(Ptr, Size, Val) for(size_t i = 0; i < Size; ++i) *((char*)Ptr + i) = Val;
#define MIHP_IS_PO2(Val) (Val != 0 && (Val & (Val - 1)) == 0)
MIHP_HeapInfo* MIHP_CreateHeap(MIHP_HeapConfig config)
{
if (config.AllocationAlignment < sizeof(size_t))
return NULL;
if (!MIHP_IS_PO2(config.AllocationAlignment))
return NULL;
MIHP_HeapInfo* heap = (MIHP_HeapInfo*)MIHP_PlatformRequestMemory(sizeof(MIHP_HeapInfo));
if (heap == NULL)
return NULL;
@@ -238,6 +246,20 @@ bool MIHP_IsPointerInHeap(MIHP_HeapInfo* heap, void* ptr)
return false;
}
size_t MIHP_GetPtrAllocationSize(MIHP_HeapInfo* heap, void* ptr)
{
if (!MIHP_IsPointerInHeap(heap, ptr))
return 0;
MIHP_LOCK_HEAP(&heap->HeapLock);
size_t segmentHeaderSize = MIHP_GetHeapAlignedSize(heap, sizeof(MIHP_HeapSegmentHeader));
MIHP_HeapSegmentHeader* segment = (MIHP_HeapSegmentHeader*)((char*)ptr - segmentHeaderSize);
MIHP_ValidateHeapSegmentHeader(heap, segment);
return segment->OccupiedSize;
}
void MIHP_ValidateHeap(MIHP_HeapInfo* heap)
{
MIHP_HeapMemoryAreaHeader* nextArea = heap->FirstArea;

View File

@@ -4,7 +4,7 @@
#include <stdint.h>
#include <stdbool.h>
#define MIHP_USE_HEAP_STRUCTURE_CHECKSUM_VALIDATION 1
#define MIHP_USE_HEAP_STRUCTURE_CHECKSUM_VALIDATION 0
#define MIHP_USE_CANARY_VALIDATION 1
#define MIHP_HEAP_CORRUPTION_TYPE_MEMORY_AREA_CHECKSUM_MISMATCH 0
@@ -38,7 +38,7 @@ typedef struct _MIHP_HeapMemoryAreaHeader
size_t NumOccupiedSegments;
struct _MIHP_HeapMemoryAreaHeader* NextArea;
struct _MIHP_HeapMemoryAreaHeader* PreviousArea;
//struct _MIHP_HeapMemoryAreaHeader* PreviousArea;
struct _MIHP_HeapInfo* OwningHeap;
struct _MIHP_HeapSegmentHeader* FirstSegment;
@@ -105,6 +105,7 @@ void* MIHP_Realloc(MIHP_HeapInfo* heap, void* ptr, size_t newSize);
bool MIHP_Free(MIHP_HeapInfo* heap, void* ptr);
bool MIHP_IsPointerInHeap(MIHP_HeapInfo* heap, void* ptr);
size_t MIHP_GetPtrAllocationSize(MIHP_HeapInfo* heap, void* ptr);
void MIHP_ValidateHeap(MIHP_HeapInfo* heap);
/* */

View File

@@ -3,6 +3,8 @@
#include <time.h>
#include <stdio.h>
#define USE_MALLOC 0
void* MIHP_PlatformRequestMemory(size_t size)
{
return malloc(size);
@@ -19,56 +21,81 @@ void MIHP_PlatformOnHeapCorruptionDetected(MIHP_HeapCorruptionInfo info)
__debugbreak();
}
#define NUM_ALLOCATIONS (4096 * 1)
#define NUM_ALLOCATIONS (4096 * 512)
void* myptrs[NUM_ALLOCATIONS] = { 0 };
int main()
{
MIHP_HeapConfig config = {0};
config.AllocationAlignment = 128;
config.AllocationInitialValue = 0xbe;
config.AllocationAlignment = 16;
config.MinimalMemoryAreaSize = 4096 * 32;
config.MaximalMemoryAreaSize = 4096 * 4096;
config.MaximalMemoryAreaSize = 4096ull * 4096ull * 4096ull;
MIHP_HeapInfo* myHeap = MIHP_CreateHeap(config);
for (int i = 0; i < NUM_ALLOCATIONS; i++)
{
clock_t start = clock();
for (int i = 0; i < NUM_ALLOCATIONS; i++)
{
size_t size = 473 + (i % 5 == 0 ? 854 : 0);
//myptrs[i] = malloc(size);
myptrs[i] = MIHP_Allocate(myHeap, size);
MIHP_ValidateHeap(myHeap);
}
clock_t end = clock();
printf("Allocating took %f seconds\n", ((double)(end - start)) / CLOCKS_PER_SEC);
size_t size = 473 + (i % 5 == 0 ? 854 : 0);
#if USE_MALLOC
myptrs[i] = malloc(size);
#else
myptrs[i] = MIHP_Allocate(myHeap, size);
MIHP_ValidateHeap(myHeap);
#endif
}
__debugbreak();
for (int i = 0; i < NUM_ALLOCATIONS; i++)
{
clock_t start = clock();
for (int z = 15; z >= 0; z--)
if (rand() % 3 == 0)
{
for (int y = 0; y < NUM_ALLOCATIONS / 16; y++)
{
int i = z + y * 16;
#if USE_MALLOC
free(myptrs[i]);
#else
MIHP_Free(myHeap, myptrs[i]);
MIHP_ValidateHeap(myHeap);
#endif
//free(myptrs[i]);
MIHP_Free(myHeap, myptrs[i]);
MIHP_ValidateHeap(myHeap);
myptrs[i] = NULL;
}
myptrs[i] = NULL;
}
clock_t end = clock();
printf("Freeing took %f seconds\n", ((double)(end - start)) / CLOCKS_PER_SEC);
}
__debugbreak();
for (int i = 0; i < NUM_ALLOCATIONS; i++)
{
volatile int n = i;
if (!myptrs[i])
{
size_t size = 541 + rand() % 2564;
#if USE_MALLOC
myptrs[i] = malloc(size);
#else
myptrs[i] = MIHP_Allocate(myHeap, size);
MIHP_ValidateHeap(myHeap);
#endif
}
}
__debugbreak();
for (int z = 15; z >= 0; z--)
for (int y = 0; y < NUM_ALLOCATIONS / 16; y++)
{
int i = z + y * 16;
#if USE_MALLOC
free(myptrs[i]);
#else
MIHP_Free(myHeap, myptrs[i]);
MIHP_ValidateHeap(myHeap);
#endif
myptrs[i] = NULL;
}
__debugbreak();
}