Browse Source

Fix behaviour for `make` to return `nil` when alloc returns `nil`

gingerBill 5 years ago
parent
commit
b8324b0776
2 changed files with 4 additions and 0 deletions
  1. 2 0
      core/mem/alloc.odin
  2. 2 0
      core/runtime/core.odin

+ 2 - 0
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 {
 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);
 	runtime.make_slice_error_loc(loc, len);
 	data := alloc(size_of(E)*len, alignment, allocator, loc);
 	data := alloc(size_of(E)*len, alignment, allocator, loc);
+	if data == nil do return nil;
 	s := Raw_Slice{data, len};
 	s := Raw_Slice{data, len};
 	return transmute(T)s;
 	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 {
 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);
 	runtime.make_dynamic_array_error_loc(loc, len, cap);
 	data := alloc(size_of(E)*cap, align_of(E), allocator, loc);
 	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};
 	s := Raw_Dynamic_Array{data, len, cap, allocator};
 	return transmute(T)s;
 	return transmute(T)s;
 }
 }

+ 2 - 0
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_aligned :: proc($T: typeid/[]$E, auto_cast len: int, alignment: int, allocator := context.allocator, loc := #caller_location) -> T {
 	make_slice_error_loc(loc, len);
 	make_slice_error_loc(loc, len);
 	data := mem_alloc(size_of(E)*len, alignment, allocator, loc);
 	data := mem_alloc(size_of(E)*len, alignment, allocator, loc);
+	if data == nil do return nil;
 	s := Raw_Slice{data, len};
 	s := Raw_Slice{data, len};
 	return transmute(T)s;
 	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_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);
 	make_dynamic_array_error_loc(loc, len, cap);
 	data := mem_alloc(size_of(E)*cap, align_of(E), allocator, loc);
 	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};
 	s := Raw_Dynamic_Array{data, len, cap, allocator};
 	return transmute(T)s;
 	return transmute(T)s;
 }
 }