mirror of
https://github.com/ToyB-Chan/minimal-heap.git
synced 2026-11-09 07:02:06 +01:00
Compare commits
63 Commits
free-list
...
77630e2c47
| Author | SHA1 | Date | |
|---|---|---|---|
| 77630e2c47 | |||
| 2289ec8644 | |||
| 088a3648dd | |||
| a78ec329b4 | |||
| 394e42aae8 | |||
| 9f42297bf2 | |||
| 92eb9456bc | |||
| 947ea6698d | |||
| 164493cdac | |||
| cda05cd84b | |||
| d000447369 | |||
| 5776f4166e | |||
| a59fffba0a | |||
| 623fca197c | |||
| c56962aa5c | |||
| 58fef4fc9e | |||
| 38bbd53e7e | |||
| 1d68f6665e | |||
| bed4193d78 | |||
| 3db14274f9 | |||
| aaa9a82167 | |||
| d7b3cc4088 | |||
| 9a65abf8bf | |||
| 29ddfe9991 | |||
| efea439a56 | |||
| 128d0e6a8d | |||
| df3e59cd1f | |||
| b3e366b1f8 | |||
| 82e759e393 | |||
| 8b033bb7d3 | |||
| 717966dea3 | |||
| e46565edbf | |||
| 0a582ffebc | |||
| ef9ec5de2a | |||
| 4dc806403f | |||
| 273868ee63 | |||
| e75b551159 | |||
| 17df4f1f85 | |||
| 57cbacda15 | |||
| e8b919f576 | |||
| 46fb0e20c6 | |||
| e507521bf8 | |||
| 8a148cf489 | |||
| da572cc718 | |||
| b68c56580e | |||
| fa3fbbc68d | |||
| 84232e399a | |||
| 9ea984f2d4 | |||
| e17def2790 | |||
| 9dd2ebaa5a | |||
| a5fe68867f | |||
| d2cd90fbd7 | |||
| 9d890e794b | |||
| 847dfe1484 | |||
| 41b6dfa9f1 | |||
| 8dfbe26ebc | |||
| 36159288d5 | |||
| 2150fcb0a2 | |||
| 3582e21a84 | |||
| b6bc80cf22 | |||
| eaddbd31fd | |||
| 1501e2e65d | |||
| 202938ca07 |
175
minimal_heap.h
175
minimal_heap.h
@@ -1,8 +1,9 @@
|
|||||||
/* Version 1.3b */
|
|
||||||
|
|
||||||
#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 12
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@@ -11,44 +12,82 @@
|
|||||||
#define MIHP_IMPLEMENTATION 0
|
#define MIHP_IMPLEMENTATION 0
|
||||||
#endif
|
#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(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
|
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 AllocationAlignment
|
size_t MinMemoryAreaSize; // Must be a multiple of AllocationAlignment
|
||||||
size_t MaximalMemoryAreaSize; // Must be a multiple of AllocationAlignment
|
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 +98,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,31 +107,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
|
||||||
{
|
{
|
||||||
uint8_t Type;
|
MIHP_HeapCorruptionError Error;
|
||||||
const void* Ptr;
|
const void* Ptr;
|
||||||
uint32_t ExpectedValue;
|
uint32_t ExpectedValue;
|
||||||
uint32_t ActualValue;
|
uint32_t ActualValue;
|
||||||
@@ -105,51 +157,64 @@ typedef struct _MIHP_Heap
|
|||||||
MIHP_HeapStats Stats;
|
MIHP_HeapStats Stats;
|
||||||
MIHP_HeapMemoryAreaHeader* FirstArea;
|
MIHP_HeapMemoryAreaHeader* FirstArea;
|
||||||
MIHP_HeapMemoryAreaHeader* LastSuccessfulAllocationArea;
|
MIHP_HeapMemoryAreaHeader* LastSuccessfulAllocationArea;
|
||||||
|
|
||||||
MIHP_HEAP_LOCK_TYPE HeapLock;
|
|
||||||
} MIHP_Heap;
|
} MIHP_Heap;
|
||||||
|
|
||||||
/* API Interface */
|
/* API Interface */
|
||||||
bool MIHP_InitializeHeap(MIHP_Heap* heap, MIHP_HeapConfig config);
|
MIHP_HeapConfig MIHP_MakeDefaultConfigPreset();
|
||||||
|
|
||||||
|
MIHP_HeapError MIHP_InitializeHeap(MIHP_Heap* heap, MIHP_HeapConfig config);
|
||||||
bool MIHP_IsHeapInitialized(const MIHP_Heap* heap);
|
bool MIHP_IsHeapInitialized(const MIHP_Heap* heap);
|
||||||
bool MIHP_UninitializeHeap(MIHP_Heap* heap, bool force);
|
MIHP_HeapError MIHP_UninitializeHeap(MIHP_Heap* heap, bool force);
|
||||||
|
|
||||||
void* MIHP_Allocate(MIHP_Heap* heap, size_t size);
|
MIHP_HeapResult MIHP_Allocate(MIHP_Heap* heap, size_t size);
|
||||||
void* MIHP_Realloc(MIHP_Heap* heap, void* ptr, size_t newSize);
|
MIHP_HeapResult MIHP_Reallocate(MIHP_Heap* heap, void* ptr, size_t newSize);
|
||||||
bool MIHP_Free(MIHP_Heap* heap, void* ptr);
|
MIHP_HeapError MIHP_Free(MIHP_Heap* heap, void* ptr);
|
||||||
|
|
||||||
bool MIHP_IsPointerInHeap(MIHP_Heap* heap, void* ptr);
|
MIHP_HeapError MIHP_SetPointerCustomMetadata(MIHP_Heap* heap, void* ptr, MIHP_AllocationCustomMetadata metadata);
|
||||||
size_t MIHP_GetPtrAllocationSize(MIHP_Heap* heap, void* ptr);
|
MIHP_HeapError MIHP_GetPointerCustomMetadata(MIHP_Heap* heap, void* ptr, MIHP_AllocationCustomMetadata* outMetadata);
|
||||||
void MIHP_ValidateHeap(MIHP_Heap* heap);
|
|
||||||
|
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** outNewSegment);
|
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_GetSegmentPayloadPtr(MIHP_Heap* heap, MIHP_HeapSegmentHeader* segment);
|
void* MIHP_GetHeapSegmentPayloadPtr(MIHP_Heap* heap, MIHP_HeapSegmentHeader* segment);
|
||||||
MIHP_HeapSegmentHeader* MIHP_GetSegmentHeaderPtr(MIHP_Heap* heap, void* payloadPtr);
|
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
@@ -1,111 +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(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 * 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 = { 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);
|
|
||||||
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
185
minimal_heap_test.cpp
Normal 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();
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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" />
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user