Skip to content
Open
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
16 changes: 13 additions & 3 deletions src/with_alloc/alloc_ringbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,19 @@ impl<T> AllocRingBuffer<T> {
#[must_use]
pub fn new(capacity: usize) -> Self {
assert_ne!(capacity, 0, "Capacity must be greater than 0");
let size = capacity.next_power_of_two();
let layout = alloc::alloc::Layout::array::<T>(size).unwrap();
let buf = unsafe { alloc::alloc::alloc(layout).cast() };
let size = capacity
.checked_next_power_of_two()
.expect("Capacity is too large");
let layout = alloc::alloc::Layout::array::<T>(size)
.expect("Capacity is too large");
assert!(
layout.size() <= (1usize << 40),
"Capacity is too large"
);
let buf: *mut T = unsafe { alloc::alloc::alloc(layout).cast() };
if buf.is_null() {
alloc::alloc::handle_alloc_error(layout);
}
Self {
buf,
size,
Expand Down
Loading