From ba24e3302102eb97c253ad8d0ad08a5678428ae5 Mon Sep 17 00:00:00 2001 From: Colin Sherratt Date: Sun, 9 Nov 2014 22:34:53 -0500 Subject: [PATCH] Handle allocate/reallocate errors in ring_buf Use is_some() in clear to simplify the clear loop. --- src/libcollections/ring_buf.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index fcf9e33cc9d9e..79201b1db54f3 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -115,15 +115,21 @@ impl RingBuf { let size = cap.checked_mul(&mem::size_of::()) .expect("capacity overflow"); + let ptr = if mem::size_of::() != 0 { + unsafe { + let ptr = heap::allocate(size, mem::min_align_of::()) as *mut T;; + if ptr.is_null() { ::alloc::oom() } + ptr + } + } else { + heap::EMPTY as *mut T + }; + RingBuf { tail: 0, head: 0, cap: cap, - ptr: if mem::size_of::() != 0 { - unsafe { heap::allocate(size, mem::min_align_of::()) as *mut T } - } else { - heap::EMPTY as *mut T - } + ptr: ptr } } @@ -282,6 +288,7 @@ impl RingBuf { old, new, mem::min_align_of::()) as *mut T; + if self.ptr.is_null() { ::alloc::oom() } } } @@ -422,9 +429,7 @@ impl RingBuf { /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn clear(&mut self) { - while !self.is_empty() { - self.pop_front(); - } + while self.pop_front().is_some() {} self.head = 0; self.tail = 0; } @@ -720,7 +725,7 @@ impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> { if mem::size_of::() != 0 { unsafe { Some(&mut *self.ptr.offset(tail as int)) } } else { - // use a none zero pointer + // use a non-zero pointer Some(unsafe { mem::transmute(1u) }) } }