Skip to content

Commit

Permalink
Dump the requested aligment if out of memory while allocating a table.
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisChiou committed Jan 2, 2018
1 parent 2a139ce commit aebe2cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
20 changes: 15 additions & 5 deletions components/hashglobe/src/lib.rs
Expand Up @@ -26,17 +26,25 @@ trait Recover<Q: ?Sized> {
fn replace(&mut self, key: Self::Key) -> Option<Self::Key>;
}

#[derive(Debug)]
pub struct AllocationInfo {
/// The size we are requesting.
size: usize,
/// The alignment we are requesting.
alignment: usize,
}

#[derive(Debug)]
pub struct FailedAllocationError {
reason: &'static str,
/// The size we are allocating, if needed.
allocation_size: Option<usize>,
/// The allocation info we are requesting, if needed.
allocation_info: Option<AllocationInfo>,
}

impl FailedAllocationError {
#[inline]
pub fn new(reason: &'static str) -> Self {
Self { reason, allocation_size: None }
Self { reason, allocation_info: None }
}
}

Expand All @@ -48,8 +56,10 @@ impl error::Error for FailedAllocationError {

impl fmt::Display for FailedAllocationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.allocation_size {
Some(size) => write!(f, "{}, allocation size: {}", self.reason, size),
match self.allocation_info {
Some(ref info) => {
write!(f, "{}, allocation: (size: {}, alignment: {})", self.reason, info.size, info.alignment)
},
None => self.reason.fmt(f),
}
}
Expand Down
3 changes: 2 additions & 1 deletion components/hashglobe/src/table.rs
Expand Up @@ -778,9 +778,10 @@ impl<K, V> RawTable<K, V> {
let buffer = alloc(size, alignment);

if buffer.is_null() {
use AllocationInfo;
return Err(FailedAllocationError {
reason: "out of memory when allocating RawTable",
allocation_size: Some(size),
allocation_info: Some(AllocationInfo { size, alignment }),
});
}

Expand Down

0 comments on commit aebe2cf

Please sign in to comment.