Tracking allocator, buffer tests

This commit is contained in:
Krzosa Karol
2025-11-28 09:59:09 +01:00
parent d72485a137
commit c19c60fe22
10 changed files with 535 additions and 225 deletions

View File

@@ -238,6 +238,8 @@ void *SystemAllocator_Alloc(void *object, int kind, void *p, size_t size) {
Assert(result);
} else if (kind == AllocatorKind_Deallocate) {
free(p);
} else {
InvalidCodepath();
}
return result;
}
@@ -245,4 +247,47 @@ void *SystemAllocator_Alloc(void *object, int kind, void *p, size_t size) {
Allocator GetSystemAllocator() {
Allocator result = {SystemAllocator_Alloc};
return result;
}
struct MemoryRecord {
size_t size;
void *addr;
bool deallocated;
};
Array<MemoryRecord> MemoryTrackingRecord;
void *TrackingAllocatorProc(void *object, int kind, void *p, size_t size) {
void *result = NULL;
if (kind == AllocatorKind_Allocate) {
result = malloc(size);
Add(&MemoryTrackingRecord, {size, result});
Assert(result);
} else if (kind == AllocatorKind_Deallocate) {
free(p);
bool found = false;
For(MemoryTrackingRecord){
if (it.addr == p) {
it.deallocated = true;
found = true;
}
}
Assert(found);
} else {
InvalidCodepath();
}
return result;
}
void TrackingAllocatorCheck() {
For (MemoryTrackingRecord) {
Assert(it.deallocated);
}
}
Allocator GetTrackingAllocator() {
Allocator result = {TrackingAllocatorProc};
return result;
}