Skip to content

Commit

Permalink
Add new_boxed fn to AlignedBytes
Browse files Browse the repository at this point in the history
Due to a long unfixed issue in Rust (rust-lang/rust#53827), initializing
large arrays and boxing them causes a stack overflow.

To avoid this, the new_boxed fn allocates itself manually.
  • Loading branch information
prof64 committed Sep 5, 2024
1 parent 3e341dc commit 55a6050
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/data_type/aligned_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ where
value: bytes,
}
}

/// Use this for large allocations better suited to the heap.
///
/// This fn also deals with [the boxed array issue in Rust](https://github.com/rust-lang/rust/issues/53827).
pub fn new_boxed() -> Box<AlignedBytes<ALIGN, N>> {
let layout = std::alloc::Layout::new::<AlignedBytes<ALIGN, N>>();
unsafe {
let ptr = std::alloc::alloc(layout) as *mut AlignedBytes<ALIGN, N>;
Box::from_raw(ptr)
}
}
}

impl<const ALIGN: usize, const N: usize> Default for AlignedBytes<ALIGN, N>
Expand Down

0 comments on commit 55a6050

Please sign in to comment.