Tracking allocator, buffer tests
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user