diff options
| author | gingerBill <bill@gingerbill.org> | 2022-08-08 15:16:18 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2022-08-08 15:16:18 +0100 |
| commit | 659c3c528dbcb0d4121f511f2fe89c885f5998c9 (patch) | |
| tree | 910f7abbe48ad7aa4fd721a658e916234487215d /core/runtime | |
| parent | 60aeab3c38941925ccc2cba30e8bcaa896a53d96 (diff) | |
Update `delete` to pass size in bytes to free when possible
Diffstat (limited to 'core/runtime')
| -rw-r--r-- | core/runtime/core_builtin.odin | 15 | ||||
| -rw-r--r-- | core/runtime/internal.odin | 8 |
2 files changed, 19 insertions, 4 deletions
diff --git a/core/runtime/core_builtin.odin b/core/runtime/core_builtin.odin index 3b8a0fab3..b779ffade 100644 --- a/core/runtime/core_builtin.odin +++ b/core/runtime/core_builtin.odin @@ -143,7 +143,7 @@ free_all :: proc{mem_free_all} @builtin delete_string :: proc(str: string, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { - return mem_free(raw_data(str), allocator, loc) + return mem_free_with_size(raw_data(str), len(str), allocator, loc) } @builtin delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { @@ -151,17 +151,24 @@ delete_cstring :: proc(str: cstring, allocator := context.allocator, loc := #cal } @builtin delete_dynamic_array :: proc(array: $T/[dynamic]$E, loc := #caller_location) -> Allocator_Error { - return mem_free(raw_data(array), array.allocator, loc) + return mem_free_with_size(raw_data(array), cap(array)*size_of(E), array.allocator, loc) } @builtin delete_slice :: proc(array: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { - return mem_free(raw_data(array), allocator, loc) + return mem_free_with_size(raw_data(array), len(array)*size_of(E), allocator, loc) } @builtin delete_map :: proc(m: $T/map[$K]$V, loc := #caller_location) -> Allocator_Error { + Entry :: struct { + hash: uintptr, + next: int, + key: K, + value: V, + } + raw := transmute(Raw_Map)m err := delete_slice(raw.hashes, raw.entries.allocator, loc) - err1 := mem_free(raw.entries.data, raw.entries.allocator, loc) + err1 := mem_free_with_size(raw.entries.data, raw.entries.cap*size_of(Entry), raw.entries.allocator, loc) if err == nil { err = err1 } diff --git a/core/runtime/internal.odin b/core/runtime/internal.odin index 55f600d68..048bff7ca 100644 --- a/core/runtime/internal.odin +++ b/core/runtime/internal.odin @@ -153,6 +153,14 @@ mem_free :: #force_inline proc(ptr: rawptr, allocator := context.allocator, loc return err } +mem_free_with_size :: #force_inline 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 +} + mem_free_bytes :: #force_inline proc(bytes: []byte, allocator := context.allocator, loc := #caller_location) -> Allocator_Error { if bytes == nil || allocator.procedure == nil { return nil |