Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
migrate ringbuf to use faux allocator
  • Loading branch information
Gankra committed Nov 25, 2014
commit 7eaf46717cf3ee849201fc3919fa6b265d8cc671
4 changes: 2 additions & 2 deletions src/liballoc/faux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub unsafe fn dealloc<T>(ptr: *mut T) {
if size == 0 {
// Do nothing
} else {
deallocate(ptr as *mut u8, size, min_align_of::<T>());
heap::deallocate(ptr as *mut u8, size, min_align_of::<T>());
}
}

Expand All @@ -131,6 +131,6 @@ pub unsafe fn dealloc_array<T>(ptr: *mut T, len: uint) {
} else {
// No need to check size * len, must have been checked when the ptr was made, or
// else UB anyway.
deallocate(ptr as *mut u8, size * len, min_align_of::<T>());
heap::deallocate(ptr as *mut u8, size * len, min_align_of::<T>());
}
}
34 changes: 5 additions & 29 deletions src/libcollections/ring_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use core::num::{Int, UnsignedInt};
use std::hash::{Writer, Hash};
use std::cmp;

use alloc::heap;
use alloc::faux;

static INITIAL_CAPACITY: uint = 8u; // 2^3
static MINIMUM_CAPACITY: uint = 2u;
Expand Down Expand Up @@ -62,11 +62,7 @@ impl<T> Drop for RingBuf<T> {
fn drop(&mut self) {
self.clear();
unsafe {
if mem::size_of::<T>() != 0 {
heap::deallocate(self.ptr as *mut u8,
self.cap * mem::size_of::<T>(),
mem::min_align_of::<T>())
}
faux::dealloc_array(self.ptr, self.cap);
}
}
}
Expand Down Expand Up @@ -116,18 +112,7 @@ impl<T> RingBuf<T> {
pub fn with_capacity(n: uint) -> RingBuf<T> {
// +1 since the ringbuffer always leaves one space empty
let cap = cmp::max(n + 1, MINIMUM_CAPACITY).next_power_of_two();
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
};
let ptr = unsafe { faux::alloc_array(cap) };

RingBuf {
tail: 0,
Expand Down Expand Up @@ -283,17 +268,8 @@ impl<T> RingBuf<T> {
let count = (new_len + 1).next_power_of_two();
assert!(count >= new_len + 1);

if mem::size_of::<T>() != 0 {
let old = self.cap * mem::size_of::<T>();
let new = count.checked_mul(mem::size_of::<T>())
.expect("capacity overflow");
unsafe {
self.ptr = heap::reallocate(self.ptr as *mut u8,
old,
new,
mem::min_align_of::<T>()) as *mut T;
if self.ptr.is_null() { ::alloc::oom() }
}
unsafe {
self.ptr = faux::realloc_array(self.ptr, self.cap, count);
}

// Move the shortest contiguous section of the ring buffer
Expand Down