Skip to content

Commit

Permalink
Remove & -> &mut transmute from TypedArena
Browse files Browse the repository at this point in the history
  • Loading branch information
renato-zannon committed Jun 10, 2014
1 parent a535cfb commit 47b72e3
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions src/libarena/lib.rs
Expand Up @@ -345,19 +345,20 @@ fn test_arena_destructors_fail() {
/// run again for these objects.
pub struct TypedArena<T> {
/// A pointer to the next object to be allocated.
ptr: *T,
ptr: Cell<*T>,

/// A pointer to the end of the allocated area. When this pointer is
/// reached, a new chunk is allocated.
end: *T,
end: Cell<*T>,

/// A pointer to the first arena segment.
first: Option<Box<TypedArenaChunk<T>>>,
first: RefCell<TypedArenaChunkRef<T>>,
}
type TypedArenaChunkRef<T> = Option<Box<TypedArenaChunk<T>>>;

struct TypedArenaChunk<T> {
/// Pointer to the next arena segment.
next: Option<Box<TypedArenaChunk<T>>>,
next: TypedArenaChunkRef<T>,

/// The number of elements that this chunk can hold.
capacity: uint,
Expand Down Expand Up @@ -443,53 +444,52 @@ impl<T> TypedArena<T> {
pub fn with_capacity(capacity: uint) -> TypedArena<T> {
let chunk = TypedArenaChunk::<T>::new(None, capacity);
TypedArena {
ptr: chunk.start() as *T,
end: chunk.end() as *T,
first: Some(chunk),
ptr: Cell::new(chunk.start() as *T),
end: Cell::new(chunk.end() as *T),
first: RefCell::new(Some(chunk)),
}
}

/// Allocates an object in the TypedArena, returning a reference to it.
#[inline]
pub fn alloc<'a>(&'a self, object: T) -> &'a T {
unsafe {
// FIXME #13933: Remove/justify all `&T` to `&mut T` transmutes
let this: &mut TypedArena<T> = mem::transmute::<&_, &mut _>(self);
if this.ptr == this.end {
this.grow()
}
if self.ptr == self.end {
self.grow()
}

let ptr: &'a mut T = mem::transmute(this.ptr);
let ptr: &'a T = unsafe {
let ptr: &'a mut T = mem::transmute(self.ptr);
ptr::write(ptr, object);
this.ptr = this.ptr.offset(1);
let ptr: &'a T = ptr;
self.ptr.set(self.ptr.get().offset(1));
ptr
}
};

ptr
}

/// Grows the arena.
#[inline(never)]
fn grow(&mut self) {
let chunk = self.first.take_unwrap();
fn grow(&self) {
let chunk = self.first.borrow_mut().take_unwrap();
let new_capacity = chunk.capacity.checked_mul(&2).unwrap();
let chunk = TypedArenaChunk::<T>::new(Some(chunk), new_capacity);
self.ptr = chunk.start() as *T;
self.end = chunk.end() as *T;
self.first = Some(chunk)
self.ptr.set(chunk.start() as *T);
self.end.set(chunk.end() as *T);
*self.first.borrow_mut() = Some(chunk)
}
}

#[unsafe_destructor]
impl<T> Drop for TypedArena<T> {
fn drop(&mut self) {
// Determine how much was filled.
let start = self.first.get_ref().start() as uint;
let end = self.ptr as uint;
let start = self.first.borrow().get_ref().start() as uint;
let end = self.ptr.get() as uint;
let diff = (end - start) / mem::size_of::<T>();

// Pass that to the `destroy` method.
unsafe {
self.first.get_mut_ref().destroy(diff)
self.first.borrow_mut().get_mut_ref().destroy(diff)
}
}
}
Expand Down

5 comments on commit 47b72e3

@bors
Copy link
Contributor

@bors bors commented on 47b72e3 Jun 11, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at renato-zannon@47b72e3

@bors
Copy link
Contributor

@bors bors commented on 47b72e3 Jun 11, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging riccieri/rust/detransmute-arena = 47b72e3 into auto

@bors
Copy link
Contributor

@bors bors commented on 47b72e3 Jun 11, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

riccieri/rust/detransmute-arena = 47b72e3 merged ok, testing candidate = f92a8fa

@bors
Copy link
Contributor

@bors bors commented on 47b72e3 Jun 11, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = f92a8fa

Please sign in to comment.