-
Notifications
You must be signed in to change notification settings - Fork 8
/
allocator.odin
63 lines (59 loc) · 1.85 KB
/
allocator.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package tracy
import "core:c"
import "core:mem"
TrackedAllocatorData :: struct {
backing_allocator : mem.Allocator,
tracked_allocator : mem.Allocator,
callstack_enable : bool,
callstack_size : i32,
}
TrackedAllocator :: proc(
self : ^TrackedAllocatorData,
callstack_enable : bool = false,
callstack_size : i32 = 5,
backing_allocator := context.allocator) -> mem.Allocator {
self.callstack_enable = callstack_enable;
self.callstack_size = callstack_size;
self.backing_allocator = backing_allocator;
self.tracked_allocator = mem.Allocator{
data = self,
procedure = proc(allocator_data : rawptr, mode : mem.Allocator_Mode, size, alignment : int, old_memory : rawptr, old_size : int, flags : u64, location := #caller_location) -> rawptr {
self := cast(^TrackedAllocatorData) allocator_data;
new_memory := self.backing_allocator.procedure(self.backing_allocator.data, mode, size, alignment, old_memory, old_size, flags, location);
switch mode {
case .Alloc:
if new_memory != nil {
if self.callstack_enable {
___tracy_emit_memory_alloc_callstack(new_memory, c.size_t(size), self.callstack_size, 1);
} else {
___tracy_emit_memory_alloc(new_memory, c.size_t(size), 1);
}
}
case .Free:
if old_memory != nil {
___tracy_emit_memory_free(old_memory, 1);
}
case .Free_All:
// NOTE: Free_All not supported by this allocator
case .Resize:
if old_memory != nil {
___tracy_emit_memory_free(old_memory, 1);
}
if new_memory != nil {
if self.callstack_enable {
___tracy_emit_memory_alloc_callstack(new_memory, c.size_t(size), self.callstack_size, 1);
}
else {
___tracy_emit_memory_alloc(new_memory, c.size_t(size), 1);
}
}
case .Query_Info:
// TODO
case .Query_Features:
// TODO
}
return new_memory;
}
};
return self.tracked_allocator;
}