diff options
| author | gingerBill <bill@gingerbill.org> | 2022-11-09 22:31:49 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2022-11-09 22:31:49 +0000 |
| commit | f2f2d532f5bb16164cc1f0128b3f5e087c56b8d9 (patch) | |
| tree | 2317c390fbf687ae1b45b16dc2b085a80154fd01 /core | |
| parent | 1bcec3f7696243664d8bfefa24b4262d4f412755 (diff) | |
Add extra calls to `Tracking_Allocator`
Diffstat (limited to 'core')
| -rw-r--r-- | core/mem/alloc.odin | 14 | ||||
| -rw-r--r-- | core/mem/allocators.odin | 11 |
2 files changed, 22 insertions, 3 deletions
diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index 05f3e145d..4f449bd37 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -77,6 +77,14 @@ free :: proc(ptr: rawptr, allocator := context.allocator, loc := #caller_locatio return runtime.mem_free(ptr, allocator, loc) } +free_with_size :: proc(ptr: rawptr, byte_count: int, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { + if ptr == nil || allocator.procedure == nil { + return nil + } + _, err := allocator.procedure(allocator.data, .Free, 0, 0, ptr, byte_count, loc) + return err +} + free_bytes :: proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { return runtime.mem_free_bytes(bytes, allocator, loc) } @@ -113,16 +121,16 @@ query_info :: proc(pointer: rawptr, allocator: Allocator, loc := #caller_locatio delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { - return free(raw_data(str), allocator, loc) + return free_with_size(raw_data(str), len(str), allocator, loc) } delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { return free((^byte)(str), allocator, loc) } delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) -> Allocator_Error { - return free(raw_data(array), array.allocator, loc) + return free_with_size(raw_data(array), cap(array)*size_of(E), array.allocator, loc) } delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { - return free(raw_data(array), allocator, loc) + return free_with_size(raw_data(array), len(array)*size_of(E), allocator, loc) } delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) -> Allocator_Error { return runtime.map_free_dynamic(transmute(Raw_Map)m, runtime.map_info(T), loc) diff --git a/core/mem/allocators.odin b/core/mem/allocators.odin index a7c50da02..364a49fba 100644 --- a/core/mem/allocators.odin +++ b/core/mem/allocators.odin @@ -864,6 +864,10 @@ tracking_allocator_init :: proc(t: ^Tracking_Allocator, backing_allocator: Alloc t.backing = backing_allocator t.allocation_map.allocator = internals_allocator t.bad_free_array.allocator = internals_allocator + + if .Free_All in query_features(t.backing) { + t.clear_on_free_all = true + } } tracking_allocator_destroy :: proc(t: ^Tracking_Allocator) { @@ -871,6 +875,13 @@ tracking_allocator_destroy :: proc(t: ^Tracking_Allocator) { delete(t.bad_free_array) } + +tracking_allocator_clear :: proc(t: ^Tracking_Allocator) { + clear(&t.allocation_map) + clear(&t.bad_free_array) +} + + tracking_allocator :: proc(data: ^Tracking_Allocator) -> Allocator { return Allocator{ data = data, |