aboutsummaryrefslogtreecommitdiff
path: root/core/mem/tracking_allocator.odin
diff options
context:
space:
mode:
authorolesya-wo <29059032+olesya-wo@users.noreply.github.com>2024-03-12 13:53:31 +0300
committerolesya-wo <29059032+olesya-wo@users.noreply.github.com>2024-03-12 13:53:31 +0300
commit9045c9ed0c91563bf1dc806e3da8ed0d123f4c12 (patch)
tree95a5d92454d890b4a721a48e6913f4b6fd0e3c14 /core/mem/tracking_allocator.odin
parent47837b206ed5b1b50b9390b51d8bb000a0b97030 (diff)
Improved statistics for core/mem/Tracking_Allocator
Diffstat (limited to 'core/mem/tracking_allocator.odin')
-rw-r--r--core/mem/tracking_allocator.odin29
1 files changed, 29 insertions, 0 deletions
diff --git a/core/mem/tracking_allocator.odin b/core/mem/tracking_allocator.odin
index d6d189731..bdf6aa5e2 100644
--- a/core/mem/tracking_allocator.odin
+++ b/core/mem/tracking_allocator.odin
@@ -22,6 +22,12 @@ Tracking_Allocator :: struct {
bad_free_array: [dynamic]Tracking_Allocator_Bad_Free_Entry,
mutex: sync.Mutex,
clear_on_free_all: bool,
+ allocated: int,
+ alloc_count: int,
+ freed: int,
+ free_count: int,
+ peak: int,
+ current: int,
}
tracking_allocator_init :: proc(t: ^Tracking_Allocator, backing_allocator: Allocator, internals_allocator := context.allocator) {
@@ -44,6 +50,7 @@ tracking_allocator_clear :: proc(t: ^Tracking_Allocator) {
sync.mutex_lock(&t.mutex)
clear(&t.allocation_map)
clear(&t.bad_free_array)
+ t.current = 0
sync.mutex_unlock(&t.mutex)
}
@@ -56,6 +63,19 @@ tracking_allocator :: proc(data: ^Tracking_Allocator) -> Allocator {
}
}
+track_alloc :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) {
+ data.allocated += entry.size
+ data.alloc_count += 1
+ data.current += entry.size
+ if data.current > data.peak do data.peak = data.current
+}
+
+track_free :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) {
+ data.freed += entry.size
+ data.free_count += 1
+ data.current -= entry.size
+}
+
tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
size, alignment: int,
old_memory: rawptr, old_size: int, loc := #caller_location) -> (result: []byte, err: Allocator_Error) {
@@ -100,13 +120,21 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
err = err,
location = loc,
}
+ track_alloc(data, &data.allocation_map[result_ptr])
case .Free:
+ if old_memory != nil && old_memory in data.allocation_map {
+ track_free(data, &data.allocation_map[old_memory])
+ }
delete_key(&data.allocation_map, old_memory)
case .Free_All:
if data.clear_on_free_all {
clear_map(&data.allocation_map)
+ data.current = 0
}
case .Resize, .Resize_Non_Zeroed:
+ if old_memory != nil && old_memory in data.allocation_map {
+ track_free(data, &data.allocation_map[old_memory])
+ }
if old_memory != result_ptr {
delete_key(&data.allocation_map, old_memory)
}
@@ -118,6 +146,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
err = err,
location = loc,
}
+ track_alloc(data, &data.allocation_map[result_ptr])
case .Query_Features:
set := (^Allocator_Mode_Set)(old_memory)