Skip to content

Commit

Permalink
Auto merge of #379 - stepancheg:fix-all-info, r=Amanieu
Browse files Browse the repository at this point in the history
Fix RawTable::allocation_info for empty table

Fixes #376
  • Loading branch information
bors committed Jan 10, 2023
2 parents 09dc17e + 471c5a6 commit 55175ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
#[inline]
#[cfg(feature = "raw")]
pub fn allocation_info(&self) -> (NonNull<u8>, Layout) {
self.table.allocation_info(Self::TABLE_LAYOUT)
self.table.allocation_info_or_zero(Self::TABLE_LAYOUT)
}

/// Returns the index of a bucket from a `Bucket`.
Expand Down Expand Up @@ -1589,6 +1589,11 @@ impl<A: Allocator + Clone> RawTableInner<A> {

#[inline]
fn allocation_info(&self, table_layout: TableLayout) -> (NonNull<u8>, Layout) {
debug_assert!(
!self.is_empty_singleton(),
"this function can only be called on non-empty tables"
);

// Avoid `Option::unwrap_or_else` because it bloats LLVM IR.
let (layout, ctrl_offset) = match table_layout.calculate_layout_for(self.buckets()) {
Some(lco) => lco,
Expand All @@ -1600,6 +1605,15 @@ impl<A: Allocator + Clone> RawTableInner<A> {
)
}

#[cfg(feature = "raw")]
fn allocation_info_or_zero(&self, table_layout: TableLayout) -> (NonNull<u8>, Layout) {
if self.is_empty_singleton() {
(NonNull::dangling(), Layout::new::<()>())
} else {
self.allocation_info(table_layout)
}
}

/// Marks all table buckets as empty without dropping their contents.
#[inline]
fn clear_no_drop(&mut self) {
Expand Down
11 changes: 11 additions & 0 deletions tests/raw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![cfg(feature = "raw")]

use hashbrown::raw::RawTable;
use std::mem;

#[test]
fn test_allocation_info() {
assert_eq!(RawTable::<()>::new().allocation_info().1.size(), 0);
assert_eq!(RawTable::<u32>::new().allocation_info().1.size(), 0);
assert!(RawTable::<u32>::with_capacity(1).allocation_info().1.size() > mem::size_of::<u32>());
}

0 comments on commit 55175ca

Please sign in to comment.