Skip to content

Commit 6c38d58

Browse files
committed
fix alignment in zeroed allocations
1 parent f34a026 commit 6c38d58

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

zlib-rs/src/allocate.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ impl Allocator<'static> {
184184

185185
impl Allocator<'_> {
186186
fn allocate_layout(&self, layout: Layout) -> *mut c_void {
187+
assert!(layout.align() <= ALIGN.into());
188+
187189
// Special case for the Rust `alloc` backed allocator
188190
#[cfg(feature = "rust-allocator")]
189191
if self.zalloc == Allocator::RUST.zalloc {
@@ -268,6 +270,8 @@ impl Allocator<'_> {
268270
}
269271

270272
fn allocate_layout_zeroed(&self, layout: Layout) -> *mut c_void {
273+
assert!(layout.align() <= ALIGN.into());
274+
271275
#[cfg(feature = "rust-allocator")]
272276
if self.zalloc == Allocator::RUST.zalloc {
273277
let ptr = unsafe { zalloc_rust_calloc(self.opaque, layout.size() as _, 1) };
@@ -313,8 +317,7 @@ impl Allocator<'_> {
313317
}
314318

315319
pub fn allocate_zeroed_buffer(&self, len: usize) -> Option<NonNull<u8>> {
316-
// internally, we want to align allocations to 64 bytes (in part for SIMD reasons)
317-
let layout = Layout::from_size_align(len, ALIGN.into()).unwrap();
320+
let layout = Layout::array::<u8>(len).ok()?;
318321
NonNull::new(self.allocate_layout_zeroed(layout).cast())
319322
}
320323

0 commit comments

Comments
 (0)