Skip to content

Commit ba24e33

Browse files
committed
Handle allocate/reallocate errors in ring_buf
Use is_some() in clear to simplify the clear loop.
1 parent 7a666df commit ba24e33

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/libcollections/ring_buf.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,21 @@ impl<T> RingBuf<T> {
115115
let size = cap.checked_mul(&mem::size_of::<T>())
116116
.expect("capacity overflow");
117117

118+
let ptr = if mem::size_of::<T>() != 0 {
119+
unsafe {
120+
let ptr = heap::allocate(size, mem::min_align_of::<T>()) as *mut T;;
121+
if ptr.is_null() { ::alloc::oom() }
122+
ptr
123+
}
124+
} else {
125+
heap::EMPTY as *mut T
126+
};
127+
118128
RingBuf {
119129
tail: 0,
120130
head: 0,
121131
cap: cap,
122-
ptr: if mem::size_of::<T>() != 0 {
123-
unsafe { heap::allocate(size, mem::min_align_of::<T>()) as *mut T }
124-
} else {
125-
heap::EMPTY as *mut T
126-
}
132+
ptr: ptr
127133
}
128134
}
129135

@@ -282,6 +288,7 @@ impl<T> RingBuf<T> {
282288
old,
283289
new,
284290
mem::min_align_of::<T>()) as *mut T;
291+
if self.ptr.is_null() { ::alloc::oom() }
285292
}
286293
}
287294

@@ -422,9 +429,7 @@ impl<T> RingBuf<T> {
422429
/// ```
423430
#[unstable = "matches collection reform specification, waiting for dust to settle"]
424431
pub fn clear(&mut self) {
425-
while !self.is_empty() {
426-
self.pop_front();
427-
}
432+
while self.pop_front().is_some() {}
428433
self.head = 0;
429434
self.tail = 0;
430435
}
@@ -720,7 +725,7 @@ impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> {
720725
if mem::size_of::<T>() != 0 {
721726
unsafe { Some(&mut *self.ptr.offset(tail as int)) }
722727
} else {
723-
// use a none zero pointer
728+
// use a non-zero pointer
724729
Some(unsafe { mem::transmute(1u) })
725730
}
726731
}

0 commit comments

Comments
 (0)