diff --git a/src/aligned_memory.rs b/src/aligned_memory.rs index b9d189c50..4be67d52c 100644 --- a/src/aligned_memory.rs +++ b/src/aligned_memory.rs @@ -19,6 +19,17 @@ impl AlignedMemory { mem.resize(align_offset, 0); (mem, align_offset) } + + fn get_mem_zeroed(max_len: usize) -> (Vec, usize) { + // use calloc() to get zeroed memory from the OS instead of using + // malloc() + memset(), see + // https://github.com/rust-lang/rust/issues/54628 + let mut mem = vec![0; max_len]; + let align_offset = mem.as_ptr().align_offset(ALIGN); + mem.resize(align_offset + max_len, 0); + (mem, align_offset) + } + /// Return a new AlignedMemory type pub fn new(max_len: usize) -> Self { let (mem, align_offset) = Self::get_mem(max_len); @@ -30,8 +41,7 @@ impl AlignedMemory { } /// Return a pre-filled AlignedMemory type pub fn new_with_size(len: usize) -> Self { - let (mut mem, align_offset) = Self::get_mem(len); - mem.resize(align_offset + len, 0); + let (mem, align_offset) = Self::get_mem_zeroed(len); Self { max_len: len, align_offset, @@ -147,6 +157,10 @@ mod tests { aligned_memory.resize(1, 3).unwrap_err(); aligned_memory.write(&[4u8; 1]).unwrap_err(); assert_eq!(aligned_memory.as_slice(), &[0, 0, 0, 0, 0, 1, 1, 2, 2, 2]); + + let aligned_memory = AlignedMemory::::new_with_size(10); + assert_eq!(aligned_memory.len(), 10); + assert_eq!(aligned_memory.as_slice(), &[0u8; 10]); } #[test] diff --git a/src/call_frames.rs b/src/call_frames.rs index 7a1cf5185..010e279de 100644 --- a/src/call_frames.rs +++ b/src/call_frames.rs @@ -33,8 +33,7 @@ impl<'a> CallFrames<'a> { /// New call frame, depth indicates maximum call depth pub fn new(config: &'a Config) -> Self { let stack_len = config.stack_size(); - let mut stack = AlignedMemory::new(stack_len); - stack.resize(stack_len, 0).unwrap(); + let stack = AlignedMemory::new_with_size(stack_len); let mut frames = CallFrames { config,