Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allocator_api: provide a grow_zeroed implementation #99

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,17 @@ unsafe impl<'a> Allocator for &'a Bump {
.map(|p| NonNull::slice_from_raw_parts(p, new_size))
.map_err(|_| AllocError)
}

unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
let mut ptr = self.grow(ptr, old_layout, new_layout)?;
ptr.as_mut()[old_layout.size()..].fill(0);
Ok(ptr)
}
}

#[cfg(test)]
Expand Down
26 changes: 21 additions & 5 deletions tests/allocator_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#![cfg(feature = "allocator_api")]
use bumpalo::Bump;

use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
use std::alloc::{AllocError, Allocator, Layout};
use std::ptr::NonNull;
use std::alloc::{Layout, AllocError, Allocator};
use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};

#[derive(Debug)]
struct AllocatorDebug {
Expand All @@ -27,8 +27,6 @@ impl AllocatorDebug {
}
}



unsafe impl Allocator for AllocatorDebug {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.allocs.fetch_add(1, Relaxed);
Expand Down Expand Up @@ -63,7 +61,6 @@ unsafe impl Allocator for AllocatorDebug {
let ref bump = self.bump;
bump.grow(ptr, old_layout, new_layout)
}

}

#[test]
Expand Down Expand Up @@ -93,3 +90,22 @@ fn allocator_api_push_a_bunch_of_items() {
drop(v);
assert_eq!(b.deallocs.load(Relaxed), 1);
}

#[test]
fn allocator_grow_zeroed() {
// Create a new bump arena.
let ref bump = Bump::new();

// Make a first allocation
let first = Layout::from_size_align(4, 4).expect("create a layout");
let mut p = bump.allocate_zeroed(first).expect("allocate a first chunk");
let allocated = bump.allocated_bytes();
unsafe { p.as_mut().fill(42) };
let p = p.cast();

// Should we grow the last allocation, that just allocate new bytes, not more
let second = Layout::from_size_align(5, 8).expect("create a expanded layout");
let p = unsafe { bump.grow_zeroed(p, first, second) }.expect("allocate a second chunk");
assert!(bump.allocated_bytes() <= allocated * 2);
assert_eq!(unsafe { p.as_ref() }, [42, 42, 42, 42, 0])
}
14 changes: 14 additions & 0 deletions valgrind.supp
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@
fun:syscall
fun:statx
}
{
<false positive fixed in valgrind 3.16 and higher part 3>
Memcheck:Param
statx(buf)
fun:statx
fun:statx
}
{
<false positive fixed in valgrind 3.16 and higher part 4>
Memcheck:Param
statx(file_name)
fun:statx
fun:statx
}