#define MIHP_IMPLEMENTATION 1 #include "minimal_heap.h" #include #include #include #define USE_MALLOC 0 #define MIHP_ValidateHeap(x) void* MIHP_PlatformRequestMemory(const MIHP_Heap* heap, size_t minRequestedSize, size_t* outActualSize) { *outActualSize = minRequestedSize; return malloc(minRequestedSize); } bool MIHP_PlatformFreeMemory(const MIHP_Heap* heap, void* ptr, size_t actualSize) { free(ptr); return true; } void MIHP_PlatformOnHeapCorruptionDetected(const MIHP_Heap* heap, MIHP_HeapCorruptionInfo info) { __debugbreak(); } #define NUM_ALLOCATIONS (4096 * 128) char* myptrs[NUM_ALLOCATIONS] = { 0 }; int main() { MIHP_HeapConfig config = {0}; config.AllocationInitialValue = 'f'; //0xbe; config.AllocationAlignment = 16; config.MinimalMemoryAreaSize = 4096 * 32; config.MaximalMemoryAreaSize = 4096ull * 4096ull * 4096ull; MIHP_Heap myHeap = { 0 }; MIHP_InitializeHeap(&myHeap, config); for (int i = 0; i < NUM_ALLOCATIONS; i++) { size_t size = 473 + (i % 5 == 0 ? 854 : 0); #if USE_MALLOC myptrs[i] = malloc(size); #else myptrs[i] = MIHP_Allocate(&myHeap, size); myptrs[i][size - 1] = '\0'; MIHP_ValidateHeap(&myHeap); #endif } __debugbreak(); for (int i = 0; i < NUM_ALLOCATIONS; i++) { if (rand() % 3 == 0) { #if USE_MALLOC free(myptrs[i]); #else MIHP_Free(&myHeap, myptrs[i]); MIHP_ValidateHeap(&myHeap); #endif myptrs[i] = NULL; } } __debugbreak(); for (int i = 0; i < NUM_ALLOCATIONS; i++) { volatile int n = 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); myptrs[i] = MIHP_Allocate(&myHeap, size); myptrs[i][size - 1] = '\0'; 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(); }