mirror of
https://github.com/ToyB-Chan/minimal-heap.git
synced 2026-07-13 21:51:16 +02:00
32 bit integration
This commit is contained in:
@@ -102,6 +102,7 @@ typedef struct _MIHP_AllocationCustomMetadata
|
|||||||
uint8_t AsU8 [sizeof(size_t) * 2];
|
uint8_t AsU8 [sizeof(size_t) * 2];
|
||||||
uint16_t AsU16 [sizeof(size_t) * 1];
|
uint16_t AsU16 [sizeof(size_t) * 1];
|
||||||
uint32_t AsU32 [sizeof(size_t) / 2];
|
uint32_t AsU32 [sizeof(size_t) / 2];
|
||||||
|
void* AsPtr [sizeof(size_t) / 2];
|
||||||
uint64_t AsU64 [sizeof(size_t) / 4];
|
uint64_t AsU64 [sizeof(size_t) / 4];
|
||||||
};
|
};
|
||||||
} MIHP_AllocationCustomMetadata;
|
} MIHP_AllocationCustomMetadata;
|
||||||
@@ -111,11 +112,12 @@ 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;
|
||||||
|
|
||||||
struct _MIHP_AllocationCustomMetadata CustomMetadata;
|
|
||||||
|
|
||||||
uint32_t Checksum; // Checksum must be the last member !
|
uint32_t Checksum; // Checksum must be the last member !
|
||||||
} MIHP_HeapSegmentHeader;
|
} MIHP_HeapSegmentHeader;
|
||||||
|
|||||||
@@ -194,9 +194,8 @@ void* MIHP_Allocate(MIHP_Heap* heap, size_t size)
|
|||||||
///
|
///
|
||||||
|
|
||||||
size_t newAreaSize = heap->Stats.TotalSize / 4; // growth-rate of 1.25
|
size_t newAreaSize = heap->Stats.TotalSize / 4; // growth-rate of 1.25
|
||||||
|
|
||||||
while (newAreaSize < size)
|
while (newAreaSize < size)
|
||||||
newAreaSize = MIHP_MIN(heap->Config.MaxMemoryAreaSize, newAreaSize + newAreaSize / 4);
|
newAreaSize = newAreaSize + newAreaSize / 4;
|
||||||
|
|
||||||
MIHP_HeapMemoryAreaHeader* newArea = MIHP_CreateHeapMemoryArea(heap, newAreaSize);
|
MIHP_HeapMemoryAreaHeader* newArea = MIHP_CreateHeapMemoryArea(heap, newAreaSize);
|
||||||
if (newArea == NULL)
|
if (newArea == NULL)
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ int main()
|
|||||||
config.PlatformRequestMemoryFn = RequestMemory;
|
config.PlatformRequestMemoryFn = RequestMemory;
|
||||||
config.PlatformFreeMemoryFn = FreeMemory;
|
config.PlatformFreeMemoryFn = FreeMemory;
|
||||||
config.OnHeapCorruptionDetectedFn = OnHeapCorruptionDetectedCallback;
|
config.OnHeapCorruptionDetectedFn = OnHeapCorruptionDetectedCallback;
|
||||||
config.MaxMemoryAreaSize *= 16;
|
|
||||||
|
|
||||||
config.HeapValidationMode = MIHP_HVM_MagicNumber;
|
config.HeapValidationMode = MIHP_HVM_MagicNumber;
|
||||||
|
|
||||||
@@ -59,19 +58,18 @@ int main()
|
|||||||
myptrs[i] = malloc(size);
|
myptrs[i] = malloc(size);
|
||||||
#else
|
#else
|
||||||
myptrs[i] = MIHP_Allocate(&myHeap, size);
|
myptrs[i] = MIHP_Allocate(&myHeap, size);
|
||||||
|
MIHP_ASSERT(myptrs[i]);
|
||||||
|
|
||||||
MIHP_AllocationCustomMetadata metadata = { 0 };
|
MIHP_AllocationCustomMetadata metadata = { 0 };
|
||||||
void* ptr = GetThisAddress();
|
void* ptr = GetThisAddress();
|
||||||
memcpy(&metadata.AsU8, &ptr, sizeof(void*));
|
metadata.AsPtr[0] = ptr;
|
||||||
|
|
||||||
MIHP_SetPointerCustomMetadata(&myHeap, myptrs[i], metadata);
|
MIHP_SetPointerCustomMetadata(&myHeap, myptrs[i], metadata);
|
||||||
MIHP_ValidateHeap(&myHeap);
|
MIHP_ValidateHeap(&myHeap);
|
||||||
|
|
||||||
MIHP_AllocationCustomMetadata outMetadata = { 0 };
|
MIHP_AllocationCustomMetadata outMetadata = { 0 };
|
||||||
MIHP_GetPointerCustomMetadata(&myHeap, myptrs[i], &outMetadata);
|
MIHP_GetPointerCustomMetadata(&myHeap, myptrs[i], &outMetadata);
|
||||||
void* returnPtr;
|
void* returnPtr = outMetadata.AsPtr[0];
|
||||||
|
|
||||||
memcpy(&returnPtr, &outMetadata.AsU8, sizeof(void*));
|
|
||||||
|
|
||||||
MIHP_ASSERT(ptr == returnPtr);
|
MIHP_ASSERT(ptr == returnPtr);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user