hashglobe: Dump more information if out of memory while allocating a table.

Sometimes, we got a crash message:
"called `Result::unwrap()` on an `Err` value: FailedAllocationError {
reason: "out of memory when allocating RawTable" }" on stylo, but
this is not enough to debug, so let's add one more field in
FailedAllocationError, so we can know the size we are allocating.
This commit is contained in:
Boris Chiou 2017-12-06 16:18:19 +08:00
parent b24778202a
commit cfe6451d8e
2 changed files with 15 additions and 10 deletions

View file

@ -29,12 +29,14 @@ trait Recover<Q: ?Sized> {
#[derive(Debug)]
pub struct FailedAllocationError {
reason: &'static str,
/// The size we are allocating, if needed.
allocation_size: Option<usize>,
}
impl FailedAllocationError {
#[inline]
pub fn new(reason: &'static str) -> Self {
Self { reason }
Self { reason, allocation_size: None }
}
}
@ -46,6 +48,9 @@ impl error::Error for FailedAllocationError {
impl fmt::Display for FailedAllocationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.reason.fmt(f)
match self.allocation_size {
Some(size) => write!(f, "{}, allocation size: {}", self.reason, size),
None => self.reason.fmt(f),
}
}
}

View file

@ -757,7 +757,7 @@ impl<K, V> RawTable<K, V> {
align_of::<(K, V)>());
if oflo {
return Err(FailedAllocationError { reason: "capacity overflow when allocating RawTable" });
return Err(FailedAllocationError::new("capacity overflow when allocating RawTable" ));
}
// One check for overflow that covers calculation and rounding of size.
@ -767,21 +767,21 @@ impl<K, V> RawTable<K, V> {
if let Some(cap_bytes) = cap_bytes {
if size < cap_bytes {
return Err(FailedAllocationError { reason: "capacity overflow when allocating RawTable" });
return Err(FailedAllocationError::new("capacity overflow when allocating RawTable"));
}
} else {
return Err(FailedAllocationError { reason: "capacity overflow when allocating RawTable" });
return Err(FailedAllocationError::new("capacity overflow when allocating RawTable"));
}
// FORK NOTE: Uses alloc shim instead of Heap.alloc
let buffer = alloc(size, alignment);
if buffer.is_null() {
return Err(FailedAllocationError { reason: "out of memory when allocating RawTable" });
return Err(FailedAllocationError {
reason: "out of memory when allocating RawTable",
allocation_size: Some(size),
});
}
let hashes = buffer.offset(hash_offset as isize) as *mut HashUint;