aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTetralux <tetralux@teknik.io>2020-01-03 10:40:45 +0000
committerTetralux <tetralux@teknik.io>2020-01-03 10:40:45 +0000
commitb32ef9e47be90a49010246359475130718dd0af8 (patch)
tree4767723d6d6de0ae78cf6c57206551d483ff48ac
parentb8324b0776d0d8d653e44c9f7876d01509b60e77 (diff)
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 ```
-rw-r--r--core/mem/alloc.odin4
-rw-r--r--core/runtime/core.odin4
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;
}