Skip to content

Commit

Permalink
Merge pull request #3268 from olesya-wo/core-mem-tracking_allocator-i…
Browse files Browse the repository at this point in the history
…mprove

Improved statistics for core/mem/Tracking_Allocator
  • Loading branch information
gingerBill authored Apr 28, 2024
2 parents 3b4169c + 51a4d97 commit a37826e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions core/mem/tracking_allocator.odin
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ Tracking_Allocator :: struct {
bad_free_array: [dynamic]Tracking_Allocator_Bad_Free_Entry,
mutex: sync.Mutex,
clear_on_free_all: bool,

total_memory_allocated: i64,
total_allocation_count: i64,
total_memory_freed: i64,
total_free_count: i64,
peak_memory_allocated: i64,
current_memory_allocated: i64,
}

tracking_allocator_init :: proc(t: ^Tracking_Allocator, backing_allocator: Allocator, internals_allocator := context.allocator) {
Expand All @@ -44,6 +51,7 @@ tracking_allocator_clear :: proc(t: ^Tracking_Allocator) {
sync.mutex_lock(&t.mutex)
clear(&t.allocation_map)
clear(&t.bad_free_array)
t.current_memory_allocated = 0
sync.mutex_unlock(&t.mutex)
}

Expand All @@ -59,6 +67,21 @@ tracking_allocator :: proc(data: ^Tracking_Allocator) -> Allocator {
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) {
track_alloc :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) {
data.total_memory_allocated += i64(entry.size)
data.total_allocation_count += 1
data.current_memory_allocated += i64(entry.size)
if data.current_memory_allocated > data.peak_memory_allocated {
data.peak_memory_allocated = data.current_memory_allocated
}
}

track_free :: proc(data: ^Tracking_Allocator, entry: ^Tracking_Allocator_Entry) {
data.total_memory_freed += i64(entry.size)
data.total_free_count += 1
data.current_memory_allocated -= i64(entry.size)
}

data := (^Tracking_Allocator)(allocator_data)

sync.mutex_guard(&data.mutex)
Expand Down Expand Up @@ -100,13 +123,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_memory_allocated = 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)
}
Expand All @@ -118,6 +149,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)
Expand Down

0 comments on commit a37826e

Please sign in to comment.