From b32ef9e47be90a49010246359475130718dd0af8 Mon Sep 17 00:00:00 2001 From: Tetralux Date: Fri, 3 Jan 2020 10:40:45 +0000 Subject: [PATCH] Fix make and reserve - Set the allocator, even if memory allocation fails. Right now it doesn't, which means that if allocation fails, it'll use the context allocator instead. This memory will be leaked if the user doesn't understand that this happened. - Only set len and cap of the array returned from make iif the memory allocation succeeded. This means that reserve will return false if you do this: ``` a := make([dynamic]int, failing_allocator); if !reserve(&a, 5) do return; // or whatever indicates failure ``` --- core/mem/alloc.odin | 4 +++- core/runtime/core.odin | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index 9feac7958..767e59183 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -124,8 +124,10 @@ make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, auto_cast len: int, alloc make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, auto_cast len: int, auto_cast cap: int, allocator := context.allocator, loc := #caller_location) -> T { runtime.make_dynamic_array_error_loc(loc, len, cap); data := alloc(size_of(E)*cap, align_of(E), allocator, loc); - if data == nil do return nil; s := Raw_Dynamic_Array{data, len, cap, allocator}; + if data == nil { + s.len, s.cap = 0, 0; + } return transmute(T)s; } make_map :: proc($T: typeid/map[$K]$E, auto_cast cap: int = 16, allocator := context.allocator, loc := #caller_location) -> T { diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 067d3d7fb..05f425eb3 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -628,8 +628,10 @@ make_dynamic_array_len :: proc($T: typeid/[dynamic]$E, auto_cast len: int, alloc make_dynamic_array_len_cap :: proc($T: typeid/[dynamic]$E, auto_cast len: int, auto_cast cap: int, allocator := context.allocator, loc := #caller_location) -> T { make_dynamic_array_error_loc(loc, len, cap); data := mem_alloc(size_of(E)*cap, align_of(E), allocator, loc); - if data == nil do return nil; s := Raw_Dynamic_Array{data, len, cap, allocator}; + if data == nil { + s.len, s.cap = 0, 0; + } return transmute(T)s; }