diff options
| author | Colin Sherratt <colin.sherratt@gmail.com> | 2014-11-09 22:34:53 -0500 |
|---|---|---|
| committer | Colin Sherratt <colin.sherratt@gmail.com> | 2014-11-14 03:41:07 -0500 |
| commit | ba24e3302102eb97c253ad8d0ad08a5678428ae5 (patch) | |
| tree | 355a9ae977be67e5bb95a36b53e1c858b687155d | |
| parent | 7a666df5fa6295e82d4350a6eb105c0370aca7a1 (diff) | |
| download | rust-ba24e3302102eb97c253ad8d0ad08a5678428ae5.tar.gz rust-ba24e3302102eb97c253ad8d0ad08a5678428ae5.zip | |
Handle allocate/reallocate errors in ring_buf
Use is_some() in clear to simplify the clear loop.
| -rw-r--r-- | src/libcollections/ring_buf.rs | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index fcf9e33cc9d..79201b1db54 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -115,15 +115,21 @@ impl<T> RingBuf<T> { let size = cap.checked_mul(&mem::size_of::<T>()) .expect("capacity overflow"); + let ptr = if mem::size_of::<T>() != 0 { + unsafe { + let ptr = heap::allocate(size, mem::min_align_of::<T>()) 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::<T>() != 0 { - unsafe { heap::allocate(size, mem::min_align_of::<T>()) as *mut T } - } else { - heap::EMPTY as *mut T - } + ptr: ptr } } @@ -282,6 +288,7 @@ impl<T> RingBuf<T> { old, new, mem::min_align_of::<T>()) as *mut T; + if self.ptr.is_null() { ::alloc::oom() } } } @@ -422,9 +429,7 @@ impl<T> RingBuf<T> { /// ``` #[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::<T>() != 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) }) } } |
