Skip to content

Commit

Permalink
Merge pull request #36 from meilisearch/log_alloc_failure_size
Browse files Browse the repository at this point in the history
Add the size of failed allocation in the panic message
  • Loading branch information
dureuill authored Nov 16, 2022
2 parents 5aa3b37 + e679573 commit 95c6fcd
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/sorter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ struct EntryBound {
data_length: u32,
}

/// Representes an `EntryBound` aligned buffer.
/// Represents an `EntryBound` aligned buffer.
struct EntryBoundAlignedBuffer(&'static mut [u8]);

impl EntryBoundAlignedBuffer {
Expand All @@ -347,7 +347,11 @@ impl EntryBoundAlignedBuffer {
let size = (size + entry_bound_size - 1) / entry_bound_size * entry_bound_size;
let layout = Layout::from_size_align(size, align_of::<EntryBound>()).unwrap();
let ptr = unsafe { alloc(layout) };
assert!(!ptr.is_null(), "the allocator is unable to allocate that much memory");
assert!(
!ptr.is_null(),
"the allocator is unable to allocate that much memory ({} bytes requested)",
size
);
let slice = unsafe { slice::from_raw_parts_mut(ptr, size) };
EntryBoundAlignedBuffer(slice)
}
Expand Down Expand Up @@ -776,4 +780,13 @@ mod tests {
assert!(value.windows(2).all(|w| w[0] <= w[1]), "{:?}", value);
}
}

#[test]
#[should_panic(
expected = "the allocator is unable to allocate that much memory (281474976710656 bytes requested)"
)]
#[cfg_attr(miri, ignore)]
fn too_big_allocation() {
EntryBoundAlignedBuffer::new(1 << 48);
}
}

0 comments on commit 95c6fcd

Please sign in to comment.