From 6a6d0f423449d1309bad066d1c7518d5872393fd Mon Sep 17 00:00:00 2001 From: Paddy Horan Date: Thu, 13 Feb 2020 21:06:23 -0500 Subject: [PATCH 1/2] Initialize memory at allocation time. --- rust/arrow/src/memory.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/rust/arrow/src/memory.rs b/rust/arrow/src/memory.rs index a7186d088fd..afa5a4b9da9 100644 --- a/rust/arrow/src/memory.rs +++ b/rust/arrow/src/memory.rs @@ -26,7 +26,7 @@ pub const ALIGNMENT: usize = 64; pub fn allocate_aligned(size: usize) -> *mut u8 { unsafe { let layout = Layout::from_size_align_unchecked(size, ALIGNMENT); - std::alloc::alloc(layout) + std::alloc::alloc_zeroed(layout) } } @@ -38,11 +38,19 @@ pub fn free_aligned(p: *mut u8, size: usize) { pub fn reallocate(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8 { unsafe { - std::alloc::realloc( + let new_ptr = std::alloc::realloc( ptr, Layout::from_size_align_unchecked(old_size, ALIGNMENT), new_size, - ) + ); + if new_size > old_size { + std::ptr::write_bytes( + new_ptr.offset(old_size as isize), + 0, + new_size - old_size, + ); + } + new_ptr } } From c90f153544e68bdd8fbd315e0ef0099ea88aefcc Mon Sep 17 00:00:00 2001 From: Paddy Horan Date: Fri, 14 Feb 2020 16:15:28 -0500 Subject: [PATCH 2/2] Addressed review comments. --- rust/arrow/src/memory.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/rust/arrow/src/memory.rs b/rust/arrow/src/memory.rs index afa5a4b9da9..70ae2b6c53a 100644 --- a/rust/arrow/src/memory.rs +++ b/rust/arrow/src/memory.rs @@ -43,12 +43,8 @@ pub fn reallocate(ptr: *mut u8, old_size: usize, new_size: usize) -> *mut u8 { Layout::from_size_align_unchecked(old_size, ALIGNMENT), new_size, ); - if new_size > old_size { - std::ptr::write_bytes( - new_ptr.offset(old_size as isize), - 0, - new_size - old_size, - ); + if !new_ptr.is_null() && new_size > old_size { + new_ptr.add(old_size).write_bytes(0, new_size - old_size); } new_ptr }