From b8324b0776d0d8d653e44c9f7876d01509b60e77 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 3 Jan 2020 10:17:30 +0000 Subject: Fix behaviour for `make` to return `nil` when alloc returns `nil` --- core/mem/alloc.odin | 2 ++ core/runtime/core.odin | 2 ++ 2 files changed, 4 insertions(+) diff --git a/core/mem/alloc.odin b/core/mem/alloc.odin index 52ba19738..9feac7958 100644 --- a/core/mem/alloc.odin +++ b/core/mem/alloc.odin @@ -111,6 +111,7 @@ make_slice :: inline proc($T: typeid/[]$E, auto_cast len: int, allocator := cont make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T { runtime.make_slice_error_loc(loc, len); data := alloc(size_of(E)*len, alignment, allocator, loc); + if data == nil do return nil; s := Raw_Slice{data, len}; return transmute(T)s; } @@ -123,6 +124,7 @@ 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}; return transmute(T)s; } diff --git a/core/runtime/core.odin b/core/runtime/core.odin index aa2d9affa..067d3d7fb 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -604,6 +604,7 @@ new_clone :: inline proc(data: $T, allocator := context.allocator, loc := #calle make_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T { make_slice_error_loc(loc, len); data := mem_alloc(size_of(E)*len, alignment, allocator, loc); + if data == nil do return nil; s := Raw_Slice{data, len}; return transmute(T)s; } @@ -627,6 +628,7 @@ 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}; return transmute(T)s; } -- cgit v1.2.3