78 Commits

Author SHA1 Message Date
a632291f07 bump version 2026-11-05 18:09:27 +01:00
dcd6c58394 fixed some bugs (tm) 2026-11-05 17:57:07 +01:00
77630e2c47 fixed bad struct size on 64 bit for custom metadata 2026-07-11 05:04:03 +02:00
2289ec8644 more asserts 2026-06-14 04:17:03 +02:00
088a3648dd forward declaration 2026-06-14 04:05:45 +02:00
a78ec329b4 asdx 2026-06-14 03:24:40 +02:00
394e42aae8 ifdef 2026-06-14 03:21:15 +02:00
9f42297bf2 Update minimal_heap_implementation.inl 2026-06-14 03:04:02 +02:00
92eb9456bc a 2026-06-13 23:43:20 +02:00
947ea6698d changed memset signature and added freefillvalue 2026-06-13 23:33:41 +02:00
164493cdac Update minimal_heap_implementation.inl 2026-06-13 23:21:18 +02:00
cda05cd84b bump version 2026-06-13 23:18:00 +02:00
d000447369 Merge pull request #3 from ToyB-Chan/error-results
added error results
2026-06-13 23:16:55 +02:00
5776f4166e added error results 2026-06-13 23:15:58 +02:00
a59fffba0a removed inline and typo 2026-06-13 20:37:20 +02:00
623fca197c typo 2026-06-13 20:34:23 +02:00
c56962aa5c version bump 2026-06-13 20:24:14 +02:00
58fef4fc9e fixed allocations with sizes smaller than the max area limited but bigger than the max payload size never return 2026-06-13 20:23:52 +02:00
38bbd53e7e Update minimal_heap_implementation.inl 2026-06-13 07:38:53 +02:00
1d68f6665e shadowing 2026-06-13 07:23:28 +02:00
bed4193d78 Update minimal_heap_test.cpp 2026-06-13 07:22:48 +02:00
3db14274f9 bootstarpping example 2026-06-13 07:08:15 +02:00
aaa9a82167 Update minimal_heap_implementation.inl 2026-06-13 00:52:32 +02:00
d7b3cc4088 Update minimal_heap.h 2026-06-13 00:50:44 +02:00
9a65abf8bf 32 bit integration 2026-06-13 00:23:38 +02:00
29ddfe9991 metadata union 2026-06-12 23:45:45 +02:00
efea439a56 Update minimal_heap.h 2026-06-11 23:44:27 +02:00
128d0e6a8d Update minimal_heap_implementation.inl 2026-06-11 16:30:04 +02:00
df3e59cd1f Update minimal_heap.h 2026-06-11 16:27:34 +02:00
b3e366b1f8 renaming 2026-06-11 16:03:51 +02:00
82e759e393 version update 2026-06-11 16:00:37 +02:00
8b033bb7d3 made passed heap to platform functions opaque as its not always safe to dereference 2026-06-11 15:59:28 +02:00
717966dea3 added max heap size 2026-06-11 15:52:01 +02:00
e46565edbf fixed reallocate not preserving custom metadata 2026-06-11 15:49:33 +02:00
0a582ffebc renaming 2026-06-11 05:38:45 +02:00
ef9ec5de2a fixed some bugs 2026-06-11 05:27:06 +02:00
4dc806403f fixed wrong return type 2026-06-11 01:54:43 +02:00
273868ee63 added custom metadata and heapwalk function 2026-06-11 01:51:58 +02:00
e75b551159 uintptr_t 2026-06-10 22:41:41 +02:00
17df4f1f85 changed the way areas are inserted and deleted, making heap compact itself automatically after massive frees 2026-06-10 08:15:53 +02:00
57cbacda15 fixed warnings 2026-06-10 07:59:16 +02:00
e8b919f576 Revert "added totalsizeoccupied stat"
This reverts commit 46fb0e20c6.
2026-06-10 07:55:08 +02:00
46fb0e20c6 added totalsizeoccupied stat 2026-06-10 07:32:37 +02:00
e507521bf8 tweaked growth rate 2026-06-10 07:19:33 +02:00
8a148cf489 wrong return type 2026-06-10 04:57:28 +02:00
da572cc718 changed the way allocation sizes are calculated (☠) 2026-06-10 04:10:12 +02:00
b68c56580e comments 2026-06-10 03:47:24 +02:00
fa3fbbc68d insane performance improvement(??) 2026-06-10 03:33:48 +02:00
84232e399a updated test 2026-06-10 03:14:50 +02:00
9ea984f2d4 Update minimal_heap.h 2026-06-10 01:21:29 +02:00
e17def2790 made all define configs configurable in runtime per heap 2026-06-10 01:09:36 +02:00
9dd2ebaa5a added ability to merge heaps 2026-06-09 23:48:56 +02:00
a5fe68867f version inc 2026-06-09 21:49:19 +02:00
d2cd90fbd7 changed all things to function ptr 2026-06-09 21:47:40 +02:00
9d890e794b Update minimal_heap_implementation.inl 2026-06-09 20:41:08 +02:00
847dfe1484 rename and fixed missing unlock 2026-06-09 20:37:30 +02:00
41b6dfa9f1 arbitrary version inc 2026-06-09 20:23:14 +02:00
8dfbe26ebc removed schädel lock mechanism 2026-06-09 20:21:39 +02:00
36159288d5 tweaked heuristics 2026-06-09 20:10:45 +02:00
2150fcb0a2 changed the order areas are traversed 2026-06-09 19:41:06 +02:00
3582e21a84 tweaked heuristics 2026-06-09 19:25:07 +02:00
b6bc80cf22 fixed last successful allocation area not being reset when destroying the area 2026-06-09 04:09:01 +02:00
eaddbd31fd added first heuristic 2026-06-09 03:30:38 +02:00
1501e2e65d changend minimal payload size 2026-06-09 01:42:21 +02:00
202938ca07 Update minimal_heap_implementation.inl 2026-06-09 01:37:53 +02:00
667b359eec Update minimal_heap_test.c 2026-06-09 00:40:57 +02:00
8f737acc1a Update minimal_heap.h 2026-06-09 00:39:29 +02:00
63a447d1c4 moved segment/payload hopping to seperate functions 2026-06-09 00:39:10 +02:00
fa1eb41719 Update minimal_heap.h 2026-06-08 21:55:27 +02:00
54b61085a8 Update minimal_heap_implementation.inl 2026-06-08 21:54:59 +02:00
d3aa81fcea Update minimal_heap_implementation.inl 2026-06-08 21:54:19 +02:00
ccce067d13 version inc 2026-06-08 21:52:34 +02:00
dd92501753 outsourced lifetime of MIHP_Heap, gave heap ptr as identifier to platform functions 2026-06-08 21:52:22 +02:00
ad14fa7c74 merge 2026-06-08 20:35:01 +02:00
3c46dfa855 Merge branch 'main' of https://github.com/ToyB-Chan/minimal-heap 2026-06-08 20:34:34 +02:00
468378e88e changed param to be non optional 2026-06-08 20:33:48 +02:00
05343eb3cd Merge pull request #2 from ToyB-Chan/v2
V2 2
2026-06-08 20:30:42 +02:00
af88bcac93 Merge pull request #1 from ToyB-Chan/v2
V2
2026-06-08 19:38:29 +02:00
6 changed files with 803 additions and 418 deletions

View File

@@ -1,54 +1,89 @@
/* Version 1.2 */
#ifndef __MINIMAL_HEAP_H__ #ifndef __MINIMAL_HEAP_H__
#define __MINIMAL_HEAP_H__ #define __MINIMAL_HEAP_H__
#define MIHP_VERSION_MAJOR 1
#define MIHP_VERSION_MINOR 13
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#ifndef MIHP_IMPLEMENTATION
#define MIHP_IMPLEMENTATION 0
#endif
#define MIHP_HEAP_STRUCTURE_VALIDATION_TYPE_NONE 0
#define MIHP_HEAP_STRUCTURE_VALIDATION_TYPE_MAGIC_NUMBER 1
#define MIHP_HEAP_STRUCTURE_VALIDATION_TYPE_CHECKSUM 2
/* Defines to be configured by the library user */ /* Defines to be configured by the library user */
#ifndef MIHP_HEAP_STRUCTURE_VALIDATION_TYPE
#define MIHP_HEAP_STRUCTURE_VALIDATION_TYPE MIHP_HEAP_STRUCTURE_VALIDATION_TYPE_CHECKSUM
#endif
#ifndef MIHP_ASSERT #ifndef MIHP_ASSERT
#define MIHP_ASSERT(Condition) while(1) { if (Condition) break; *(volatile char*)(0x0) = 0; } #define MIHP_ASSERT(Condition) while(1) { if (Condition) break; *(volatile char*)(0x0) = 0; }
#endif #endif
#ifndef MIHP_HEAP_LOCK_TYPE #ifndef MIHP_MEMSET
#define MIHP_HEAP_LOCK_TYPE volatile bool // Does NOT have to be reentred safe #define MIHP_MEMSET(Ptr, Val, Size) do { for(size_t i = 0; i < Size; ++i) *((char*)Ptr + i) = Val; } while(0);
#define MIHP_LOCK_HEAP(LockVarPtr) { while (*(LockVarPtr)) {}; *(LockVarPtr) = true; }
#define MIHP_UNLOCK_HEAP(LockVarPtr) { *(LockVarPtr) = false; }
#endif #endif
/* */ /* */
/* Hooks to be implemented by the library user */ typedef uintptr_t MIHP_HeapOpaque;
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);
/* */
#define MIHP_HEAP_CORRUPTION_TYPE_MEMORY_AREA_CHECKSUM_MISMATCH 0 typedef void*(MIHP_PlatformRequestMemoryFn)(MIHP_HeapOpaque heap, size_t minRequestedSize, size_t* outActualSize);
#define MIHP_HEAP_CORRUPTION_TYPE_SEGMENT_CHECKSUM_MISMATCH 1 typedef bool (MIHP_PlatformFreeMemoryFn)(MIHP_HeapOpaque heap, void* ptr, size_t actualSize);
#define MIHP_HEAP_CORRUPTION_TYPE_DOUBLE_FREE 2
typedef enum _MIHP_HeapCorruptionError
{
MIHP_HCE_MemoryAreaChecksumMismatch = 0,
MIHP_HCE_SegmentChecksumMismatch = 1,
MIHP_HCE_DoubleFree = 2
} MIHP_HeapCorruptionError;
struct _MIHP_Heap;
struct _MIHP_HeapCorruptionInfo;
typedef void (MIHP_OnHeapCorruptionDetectedFn)(const struct _MIHP_Heap* heap, struct _MIHP_HeapCorruptionInfo corruptionInfo);
typedef void(MIHP_LockHeapFn)(MIHP_HeapOpaque heap, void* heapLock);
typedef void(MIHP_UnlockHeapFn)(MIHP_HeapOpaque heap, void* heapLock);
typedef enum _MIHP_HeapValidationMode
{
MIHP_HVM_None = 0,
MIHP_HVM_MagicNumber = 1,
MIHP_HVM_Checksum = 2
} MIHP_HeapValidationMode;
typedef enum _MIHP_HeapError
{
MIHP_HE_Success = 0,
MIHP_HE_InvalidHeap = 1,
MIHP_HE_BadConfig = 2,
MIHP_HE_OutOfMemory = 3,
MIHP_HE_InvalidOperation = 4,
MIHP_HE_InvalidParameter = 5
} MIHP_HeapError;
typedef struct _MIHP_HeapResult
{
void* Ptr;
MIHP_HeapError Error;
} MIHP_HeapResult;
typedef struct _MIHP_HeapConfig typedef struct _MIHP_HeapConfig
{ {
size_t AllocationAlignment; // Must be power of two and at least sizeof(size_t) MIHP_PlatformRequestMemoryFn* PlatformRequestMemoryFn; // Alignment must at least be alignment of size_t, outActualSize must at least be minRequestedSize
MIHP_PlatformFreeMemoryFn* PlatformFreeMemoryFn; // Can return false if the memory cannot be freed
MIHP_OnHeapCorruptionDetectedFn* OnHeapCorruptionDetectedFn; // Can be NULL
size_t MinimalMemoryAreaSize; // Must be a multiple of AllocationAlingment size_t MinMemoryAreaSize; // Must be a multiple of AllocationAlignment
size_t MaximalMemoryAreaSize; // Must be a multiple of AllocationAlingment size_t MaxMemoryAreaSize; // Must be a multiple of AllocationAlignment
size_t MaxHeapSize; // Maximum size the heap is allowed to grow to. May exceed slightly if PlatformRequestMemory returns a larger area than requested
char AllocationInitialValue; size_t MinTotalTraversalsBeforeHeapExpansion; // Minimal amounts of traversals during allocation before considering expanding the heap
uint8_t MinHeapTraversalPercentToExpand; // Minimal integer percent of the heap traversed before expanding the heap
size_t AllocationAlignment; // Must be power of two and at least sizeof(size_t)
uint8_t AllocateFillValue; // Initial value the returned blocks are to be filled with
uint8_t FreeFillValue; // Value freed blocks are to be filled with
MIHP_HeapValidationMode HeapValidationMode;
void* HeapLock; // Does not need to be reentred safe, can be NULL to disable locking
MIHP_LockHeapFn* LockHeapFn; // Can be NULL if HeapLock is NULL
MIHP_UnlockHeapFn* UnlockHeapFn; // Can be NULL if HeapLock is NULL
} MIHP_HeapConfig; } MIHP_HeapConfig;
typedef struct _MIHP_HeapStats typedef struct _MIHP_HeapStats
@@ -59,7 +94,6 @@ typedef struct _MIHP_HeapStats
size_t TotalSize; size_t TotalSize;
} MIHP_HeapStats; } MIHP_HeapStats;
// This structs size must be multiple of 8
typedef struct _MIHP_HeapMemoryAreaHeader typedef struct _MIHP_HeapMemoryAreaHeader
{ {
size_t AreaSize; size_t AreaSize;
@@ -69,33 +103,45 @@ typedef struct _MIHP_HeapMemoryAreaHeader
struct _MIHP_HeapMemoryAreaHeader* NextArea; struct _MIHP_HeapMemoryAreaHeader* NextArea;
struct _MIHP_HeapMemoryAreaHeader* PreviousArea; struct _MIHP_HeapMemoryAreaHeader* PreviousArea;
struct _MIHP_Heap* OwningHeap;
struct _MIHP_HeapSegmentHeader* FirstSegment; struct _MIHP_HeapSegmentHeader* FirstSegment;
struct _MIHP_HeapSegmentHeader* LastSuccessfulAllocationSegment;
// TODO: Change this to an InstigatingHeapUuid (opaque ptr)
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;
// This structs size must be multiple of 8 typedef struct _MIHP_AllocationCustomMetadata
{
union
{
uint8_t AsU8 [sizeof(size_t) * 2];
uint16_t AsU16 [(sizeof(size_t) * 2) / sizeof(uint16_t)];
uint32_t AsU32 [(sizeof(size_t) * 2) / sizeof(uint32_t)];
void* AsPtr [(sizeof(size_t) * 2) / sizeof(void*)];
uint64_t AsU64 [(sizeof(size_t) * 2) / sizeof(uint64_t)];
};
} MIHP_AllocationCustomMetadata;
typedef struct _MIHP_HeapSegmentHeader typedef struct _MIHP_HeapSegmentHeader
{ {
size_t SegmentSize; size_t SegmentSize;
size_t OccupiedSize; size_t OccupiedSize;
struct _MIHP_AllocationCustomMetadata CustomMetadata;
struct _MIHP_HeapSegmentHeader* NextSegment; struct _MIHP_HeapSegmentHeader* NextSegment;
struct _MIHP_HeapSegmentHeader* PreviousSegment; struct _MIHP_HeapSegmentHeader* PreviousSegment;
struct _MIHP_HeapMemoryAreaHeader* OwningMemoryArea; struct _MIHP_HeapMemoryAreaHeader* OwningMemoryArea;
char _padding0[16];
uint32_t Checksum; // Checksum must be the last member !
uint32_t Checksum; // Checksum must be the last member !
} MIHP_HeapSegmentHeader; } MIHP_HeapSegmentHeader;
typedef struct _MIHP_HeapCorruptionInfo typedef struct _MIHP_HeapCorruptionInfo
{ {
const struct _MIHP_Heap* Heap; MIHP_HeapCorruptionError Error;
uint8_t Type;
const void* Ptr; const void* Ptr;
uint32_t ExpectedValue; uint32_t ExpectedValue;
uint32_t ActualValue; uint32_t ActualValue;
@@ -107,48 +153,64 @@ typedef struct _MIHP_Heap
MIHP_HeapStats Stats; MIHP_HeapStats Stats;
MIHP_HeapMemoryAreaHeader* FirstArea; MIHP_HeapMemoryAreaHeader* FirstArea;
MIHP_HeapMemoryAreaHeader* LastSuccessfulAllocationArea; MIHP_HeapMemoryAreaHeader* LastSuccessfulAllocationArea;
size_t HeapStructureAllocationSize;
MIHP_HEAP_LOCK_TYPE HeapLock;
} MIHP_Heap; } MIHP_Heap;
/* API Interface */ /* API Interface */
MIHP_Heap* MIHP_CreateHeap(MIHP_HeapConfig config); MIHP_HeapConfig MIHP_MakeDefaultConfigPreset();
bool MIHP_DestroyHeap(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);
bool MIHP_IsPointerInHeap(MIHP_Heap* heap, void* ptr); MIHP_HeapError MIHP_InitializeHeap(MIHP_Heap* heap, MIHP_HeapConfig config);
size_t MIHP_GetPtrAllocationSize(MIHP_Heap* heap, void* ptr); bool MIHP_IsHeapInitialized(const MIHP_Heap* heap);
void MIHP_ValidateHeap(MIHP_Heap* heap); MIHP_HeapError MIHP_UninitializeHeap(MIHP_Heap* heap, bool force);
MIHP_HeapResult MIHP_Allocate(MIHP_Heap* heap, size_t size);
MIHP_HeapResult MIHP_Reallocate(MIHP_Heap* heap, void* ptr, size_t newSize);
MIHP_HeapError MIHP_Free(MIHP_Heap* heap, void* ptr);
MIHP_HeapError MIHP_SetPointerCustomMetadata(MIHP_Heap* heap, void* ptr, MIHP_AllocationCustomMetadata metadata);
MIHP_HeapError MIHP_GetPointerCustomMetadata(MIHP_Heap* heap, void* ptr, MIHP_AllocationCustomMetadata* outMetadata);
MIHP_HeapError MIHP_MergeHeaps(MIHP_Heap* sourceHeap, MIHP_Heap* heapToAbsorb);
bool MIHP_IsAddressInHeap(MIHP_Heap* heap, void* ptr);
MIHP_HeapError MIHP_GetPointerAllocationSize(MIHP_Heap* heap, void* ptr, size_t* outSize);
MIHP_HeapError MIHP_ValidateHeap(MIHP_Heap* heap);
typedef void(MIHP_WalkHeapCallbackFn)(const MIHP_Heap* heap, const MIHP_HeapMemoryAreaHeader* area, const MIHP_HeapSegmentHeader* segment);
MIHP_HeapError MIHP_WalkHeap(MIHP_Heap* heap, MIHP_WalkHeapCallbackFn* callback);
/* */ /* */
/* Internals */ /* Internals */
void MIHP_LockHeap(MIHP_Heap* heap);
void MIHP_UnlockHeap(MIHP_Heap* heap);
MIHP_HeapMemoryAreaHeader* MIHP_CreateHeapMemoryArea(MIHP_Heap* heap, size_t requestedSize); MIHP_HeapMemoryAreaHeader* MIHP_CreateHeapMemoryArea(MIHP_Heap* heap, size_t requestedSize);
bool MIHP_DestroyHeapMemoryArea(MIHP_Heap* heap, MIHP_HeapMemoryAreaHeader* area); bool MIHP_DestroyHeapMemoryArea(MIHP_Heap* heap, MIHP_HeapMemoryAreaHeader* area);
MIHP_HeapSegmentHeader* MIHP_InitializeHeapSegment(MIHP_HeapMemoryAreaHeader* area, void* segmentStart, size_t size); MIHP_HeapSegmentHeader* MIHP_InitializeHeapSegment(MIHP_Heap* heap, MIHP_HeapMemoryAreaHeader* area, void* segmentStart, size_t size);
bool MIHP_UninitializeHeapSegment(MIHP_HeapSegmentHeader* segment); bool MIHP_UninitializeHeapSegment(MIHP_Heap* heap, MIHP_HeapSegmentHeader* segment);
bool MIHP_SplitHeapSegment(MIHP_HeapSegmentHeader* sourceSegment, size_t newSegmentSize, MIHP_HeapSegmentHeader** optOutNewSegment); bool MIHP_SplitHeapSegment(MIHP_Heap* heap, MIHP_HeapSegmentHeader* sourceSegment, size_t newSegmentSize, MIHP_HeapSegmentHeader** outNewSegment);
bool MIHP_MergeHeapSegments(MIHP_HeapSegmentHeader* sourceSegment, MIHP_HeapSegmentHeader* segmentToAbsorb); bool MIHP_MergeHeapSegments(MIHP_Heap* heap, MIHP_HeapSegmentHeader* sourceSegment, MIHP_HeapSegmentHeader* segmentToAbsorb);
void* MIHP_GetHeapSegmentPayloadPtr(MIHP_Heap* heap, MIHP_HeapSegmentHeader* segment);
MIHP_HeapSegmentHeader* MIHP_GetHeapSegmentHeaderPtr(MIHP_Heap* heap, void* payloadPtr);
size_t MIHP_GetHeapAlignedSize(const MIHP_Heap* heap, size_t size); size_t MIHP_GetHeapAlignedSize(const MIHP_Heap* heap, size_t size);
uint32_t MIHP_HashMemoryRegion(const void* data, size_t size); uint32_t MIHP_HashMemoryRegion(const void* data, size_t size);
uint32_t MIHP_GenerateHeapMemoryAreaChecksum(const MIHP_HeapMemoryAreaHeader* area); uint32_t MIHP_GenerateHeapMemoryAreaChecksum(const MIHP_Heap* heap, const MIHP_HeapMemoryAreaHeader* area);
uint32_t MIHP_GenerateHeapSegmentChecksum(const MIHP_HeapSegmentHeader* segment); uint32_t MIHP_GenerateHeapSegmentChecksum(const MIHP_Heap* heap, const MIHP_HeapSegmentHeader* segment);
void MIHP_ValidateHeapMemoryAreaHeader(const MIHP_Heap* heap, const MIHP_HeapMemoryAreaHeader* area); void MIHP_ValidateHeapMemoryAreaHeader(const MIHP_Heap* heap, const MIHP_HeapMemoryAreaHeader* area);
void MIHP_ValidateHeapSegmentHeader(const MIHP_Heap* heap, const MIHP_HeapSegmentHeader* segment); void MIHP_ValidateHeapSegmentHeader(const MIHP_Heap* heap, const MIHP_HeapSegmentHeader* segment);
size_t MIHP_GetMinimalPayloadSize(const MIHP_Heap* heap); size_t MIHP_GetMinimalPayloadSize(const MIHP_Heap* heap);
size_t MIHP_GetMaximalPayloadSize(const MIHP_Heap* heap);
/* */ /* */
#endif #ifdef MIHP_IMPLEMENTATION
#if MIHP_IMPLEMENTATION
#include "minimal_heap_implementation.inl" #include "minimal_heap_implementation.inl"
#endif #endif
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,109 +0,0 @@
#define MIHP_IMPLEMENTATION 1
#include "minimal_heap.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#define USE_MALLOC 0
#define MIHP_ValidateHeap(x)
void* MIHP_PlatformRequestMemory(size_t minRequestedSize, size_t* outActualSize)
{
*outActualSize = minRequestedSize;
return malloc(minRequestedSize);
}
bool MIHP_PlatformFreeMemory(void* ptr, size_t actualSize)
{
free(ptr);
return true;
}
void MIHP_PlatformOnHeapCorruptionDetected(MIHP_HeapCorruptionInfo info)
{
__debugbreak();
}
#define NUM_ALLOCATIONS (4096 * 4)
void* myptrs[NUM_ALLOCATIONS] = { 0 };
int main()
{
MIHP_HeapConfig config = {0};
config.AllocationInitialValue = 0xbe;
config.AllocationAlignment = 16;
config.MinimalMemoryAreaSize = 4096 * 32;
config.MaximalMemoryAreaSize = 4096ull * 4096ull * 4096ull;
MIHP_Heap* myHeap = MIHP_CreateHeap(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);
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);
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();
}

185
minimal_heap_test.cpp Normal file
View File

@@ -0,0 +1,185 @@
#define MIHP_IMPLEMENTATION
#include "minimal_heap.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <intrin.h>
#include <string.h>
#define USE_MALLOC 0
#define MIHP_ValidateHeap(x)
void* RequestMemory(MIHP_HeapOpaque heap, size_t minRequestedSize, size_t* outActualSize)
{
*outActualSize = minRequestedSize;
return malloc(minRequestedSize);
}
bool FreeMemory(MIHP_HeapOpaque heap, void* ptr, size_t actualSize)
{
free(ptr);
return true;
}
void OnHeapCorruptionDetectedCallback(const MIHP_Heap* heap, MIHP_HeapCorruptionInfo info)
{
__debugbreak();
}
#define NUM_ALLOCATIONS (4096 * 4096)
void* myptrs[NUM_ALLOCATIONS] = { 0 };
void* GetThisAddress()
{
return _ReturnAddress();
}
void BootstrapHeap()
{
MIHP_HeapConfig config = MIHP_MakeDefaultConfigPreset();
config.PlatformRequestMemoryFn = RequestMemory;
config.PlatformFreeMemoryFn = FreeMemory;
config.OnHeapCorruptionDetectedFn = OnHeapCorruptionDetectedCallback;
config.HeapValidationMode = MIHP_HVM_MagicNumber;
MIHP_Heap* bootstrappedHeap = NULL;
{
MIHP_Heap tmpHeap = { 0 };
MIHP_InitializeHeap(&tmpHeap, config);
bootstrappedHeap = (MIHP_Heap*)MIHP_Allocate(&tmpHeap, sizeof(MIHP_Heap)).Ptr;
memset(bootstrappedHeap, 0, sizeof(MIHP_Heap));
MIHP_InitializeHeap(bootstrappedHeap, config);
MIHP_MergeHeaps(bootstrappedHeap, &tmpHeap);
}
// bootstrappedHeap is ready to use
__debugbreak();
}
int main()
{
BootstrapHeap();
MIHP_HeapConfig config = MIHP_MakeDefaultConfigPreset();
config.PlatformRequestMemoryFn = RequestMemory;
config.PlatformFreeMemoryFn = FreeMemory;
config.OnHeapCorruptionDetectedFn = OnHeapCorruptionDetectedCallback;
config.HeapValidationMode = MIHP_HVM_MagicNumber;
MIHP_Heap myHeap = { 0 };
MIHP_InitializeHeap(&myHeap, config);
for (int i = 0; i < NUM_ALLOCATIONS; i++)
{
size_t size = 565 + (i % 5 == 0 ? 854 : 0);
#if USE_MALLOC
myptrs[i] = malloc(size);
#else
myptrs[i] = MIHP_Allocate(&myHeap, size).Ptr;
MIHP_ASSERT(myptrs[i]);
MIHP_AllocationCustomMetadata metadata = { 0 };
void* ptr = GetThisAddress();
metadata.AsPtr[0] = ptr;
MIHP_SetPointerCustomMetadata(&myHeap, myptrs[i], metadata);
MIHP_ValidateHeap(&myHeap);
MIHP_AllocationCustomMetadata outMetadata = { 0 };
MIHP_GetPointerCustomMetadata(&myHeap, myptrs[i], &outMetadata);
void* returnPtr = outMetadata.AsPtr[0];
MIHP_ASSERT(ptr == returnPtr);
#endif
}
__debugbreak();
//MIHP_MEMSET(myptrs[5], 5000, 0x75);
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_Reallocate(&myHeap, myptrs[i], size).Ptr;
MIHP_ValidateHeap(&myHeap);
#endif
}
}
__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] = malloc(size);
#else
myptrs[i] = MIHP_Allocate(&myHeap, size).Ptr;
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();
}

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>