aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2022-02-05 12:26:10 +0000
committerGitHub <noreply@github.com>2022-02-05 12:26:10 +0000
commitfb710f8cbfa67ad169eff9a33ed67f07cfb5b643 (patch)
treeb59a591ce86d9a8a522d806f95eba017c303247b
parent1553137c2365c3980488a6455fd83f0fcb9e28ca (diff)
parente5868e32050fc2fe92a50caa743c8123d3fe59d9 (diff)
Merge pull request #1376 from jasonKercher/master
Added zeroing to new memory regions from _unix_realloc
-rw-r--r--core/os/os.odin12
-rw-r--r--core/os/os_darwin.odin2
-rw-r--r--core/os/os_freebsd.odin2
-rw-r--r--core/os/os_linux.odin2
4 files changed, 16 insertions, 2 deletions
diff --git a/core/os/os.odin b/core/os/os.odin
index 83158be80..2ebfebd2e 100644
--- a/core/os/os.odin
+++ b/core/os/os.odin
@@ -206,11 +206,19 @@ heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode,
}
}
- aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int) -> ([]byte, mem.Allocator_Error) {
+ aligned_resize :: proc(p: rawptr, old_size: int, new_size: int, new_alignment: int) -> (new_memory: []byte, err: mem.Allocator_Error) {
if p == nil {
return nil, nil
}
- return aligned_alloc(new_size, new_alignment, p)
+
+ new_memory = aligned_alloc(new_size, new_alignment, p) or_return
+
+ // NOTE: heap_resize does not zero the new memory, so we do it
+ if new_size > old_size {
+ new_region := mem.raw_data(new_memory[old_size:])
+ mem.zero(new_region, new_size - old_size)
+ }
+ return
}
switch mode {
diff --git a/core/os/os_darwin.odin b/core/os/os_darwin.odin
index ef1d6185a..adb490e4f 100644
--- a/core/os/os_darwin.odin
+++ b/core/os/os_darwin.odin
@@ -553,6 +553,8 @@ heap_alloc :: proc(size: int) -> rawptr {
return _unix_calloc(1, size)
}
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
+ // NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on
+ // POSIX platforms. Ensure your caller takes this into account.
return _unix_realloc(ptr, new_size)
}
heap_free :: proc(ptr: rawptr) {
diff --git a/core/os/os_freebsd.odin b/core/os/os_freebsd.odin
index e9314b468..82317532d 100644
--- a/core/os/os_freebsd.odin
+++ b/core/os/os_freebsd.odin
@@ -378,6 +378,8 @@ heap_alloc :: proc(size: int) -> rawptr {
}
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
+ // NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on
+ // POSIX platforms. Ensure your caller takes this into account.
return _unix_realloc(ptr, c.size_t(new_size));
}
diff --git a/core/os/os_linux.odin b/core/os/os_linux.odin
index 1c796f1b8..3288388ea 100644
--- a/core/os/os_linux.odin
+++ b/core/os/os_linux.odin
@@ -727,6 +727,8 @@ heap_alloc :: proc(size: int) -> rawptr {
}
heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr {
+ // NOTE: _unix_realloc doesn't guarantee new memory will be zeroed on
+ // POSIX platforms. Ensure your caller takes this into account.
return _unix_realloc(ptr, c.size_t(new_size))
}