|
@@ -22,6 +22,13 @@ Tracking_Allocator :: struct {
|
|
bad_free_array: [dynamic]Tracking_Allocator_Bad_Free_Entry,
|
|
bad_free_array: [dynamic]Tracking_Allocator_Bad_Free_Entry,
|
|
mutex: sync.Mutex,
|
|
mutex: sync.Mutex,
|
|
clear_on_free_all: bool,
|
|
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) {
|
|
tracking_allocator_init :: proc(t: ^Tracking_Allocator, backing_allocator: Allocator, internals_allocator := context.allocator) {
|
|
@@ -44,6 +51,7 @@ tracking_allocator_clear :: proc(t: ^Tracking_Allocator) {
|
|
sync.mutex_lock(&t.mutex)
|
|
sync.mutex_lock(&t.mutex)
|
|
clear(&t.allocation_map)
|
|
clear(&t.allocation_map)
|
|
clear(&t.bad_free_array)
|
|
clear(&t.bad_free_array)
|
|
|
|
+ t.current_memory_allocated = 0
|
|
sync.mutex_unlock(&t.mutex)
|
|
sync.mutex_unlock(&t.mutex)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -59,6 +67,21 @@ tracking_allocator :: proc(data: ^Tracking_Allocator) -> Allocator {
|
|
tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|
tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|
size, alignment: int,
|
|
size, alignment: int,
|
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> (result: []byte, err: Allocator_Error) {
|
|
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)
|
|
data := (^Tracking_Allocator)(allocator_data)
|
|
|
|
|
|
sync.mutex_guard(&data.mutex)
|
|
sync.mutex_guard(&data.mutex)
|
|
@@ -100,13 +123,21 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|
err = err,
|
|
err = err,
|
|
location = loc,
|
|
location = loc,
|
|
}
|
|
}
|
|
|
|
+ track_alloc(data, &data.allocation_map[result_ptr])
|
|
case .Free:
|
|
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)
|
|
delete_key(&data.allocation_map, old_memory)
|
|
case .Free_All:
|
|
case .Free_All:
|
|
if data.clear_on_free_all {
|
|
if data.clear_on_free_all {
|
|
clear_map(&data.allocation_map)
|
|
clear_map(&data.allocation_map)
|
|
|
|
+ data.current_memory_allocated = 0
|
|
}
|
|
}
|
|
case .Resize, .Resize_Non_Zeroed:
|
|
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 {
|
|
if old_memory != result_ptr {
|
|
delete_key(&data.allocation_map, old_memory)
|
|
delete_key(&data.allocation_map, old_memory)
|
|
}
|
|
}
|
|
@@ -118,6 +149,7 @@ tracking_allocator_proc :: proc(allocator_data: rawptr, mode: Allocator_Mode,
|
|
err = err,
|
|
err = err,
|
|
location = loc,
|
|
location = loc,
|
|
}
|
|
}
|
|
|
|
+ track_alloc(data, &data.allocation_map[result_ptr])
|
|
|
|
|
|
case .Query_Features:
|
|
case .Query_Features:
|
|
set := (^Allocator_Mode_Set)(old_memory)
|
|
set := (^Allocator_Mode_Set)(old_memory)
|