Skip to content

Commit

Permalink
fix(alloc): update a bunch of uses of ptr::Unique to use new API
Browse files Browse the repository at this point in the history
API changed in rust-lang/rust#41064; see also the tracking issue
rust-lang/rust#27730 for more information.
  • Loading branch information
hawkw committed May 25, 2017
1 parent 0b7a514 commit 607ba69
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
16 changes: 8 additions & 8 deletions alloc/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ where A: Allocator
impl<'alloc, A> Deref for BorrowedPtr<'alloc, A>
where A: Allocator
, A: 'alloc {
type Target = Address;
fn deref(&self) -> &Self::Target { &(*self.ptr) }
type Target = Unique<u8>;
fn deref(&self) -> &Self::Target { &self.ptr }
}

impl<'alloc, A> Drop for BorrowedPtr<'alloc, A>
where A: Allocator
, A: 'alloc {
fn drop(&mut self) {
unsafe {
self.allocator.lock().dealloc(*self.ptr, self.layout.clone())
self.allocator.lock().dealloc(self.ptr.as_ptr(), self.layout.clone())
}
}
}
Expand Down Expand Up @@ -91,14 +91,14 @@ impl<'alloc, A, T> Deref for Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {
type Target = T;
fn deref(&self) -> &Self::Target { unsafe { self.value.get() } }
fn deref(&self) -> &Self::Target { unsafe { self.value.as_ref() } }
}

impl<'alloc, A, T> DerefMut for Borrowed<'alloc, A, T>
where A: Allocator
, A: 'alloc {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.value.get_mut() }
unsafe { self.value.as_mut() }
}
}

Expand All @@ -107,15 +107,15 @@ where A: Allocator
, A: 'alloc {
fn drop(&mut self) {
use mem::drop;
let address = *self.value as Address;
let address = self.value.as_ptr() as Address;
// ensure we drop the object _before_ deallocating it, so that
// the object's destructor gets run first
// i hope this is correct...
drop(*self.value);
drop(self.value.as_ptr());
unsafe {
self.allocator.lock()
.dealloc( address
, Layout::for_value(self.value.get()))
, Layout::for_value(self.value.as_ref()))
}
}
}
16 changes: 9 additions & 7 deletions alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ pub unsafe trait Allocator {
/// Captures a common usage pattern for allocators.
unsafe fn dealloc_one<T>(&mut self, mut ptr: Unique<T>)
where Self: Sized {
let raw_ptr = ptr.get_mut() as *mut T as *mut u8;
let raw_ptr = ptr.as_mut() as *mut T as *mut u8;
self.dealloc(raw_ptr, Layout::new::<T>());
}

Expand Down Expand Up @@ -682,7 +682,9 @@ pub unsafe trait Allocator {
n_old: usize,
n_new: usize) -> Result<Unique<T>, AllocErr>
where Self: Sized {
match (Layout::array::<T>(n_old), Layout::array::<T>(n_new), *ptr) {
match ( Layout::array::<T>(n_old)
, Layout::array::<T>(n_new)
, ptr.as_ptr()) {
(Some(ref k_old), Some(ref k_new), ptr) if k_old.size() > 0 && k_new.size() > 0 => {
self.realloc(ptr as *mut u8, k_old.clone(), k_new.clone())
.map(|p|Unique::new(p as *mut T))
Expand All @@ -698,7 +700,7 @@ pub unsafe trait Allocator {
/// Captures a common usage pattern for allocators.
unsafe fn dealloc_array<T>(&mut self, ptr: Unique<T>, n: usize) -> Result<(), AllocErr>
where Self: Sized {
let raw_ptr = *ptr as *mut u8;
let raw_ptr = ptr.as_ptr() as *mut u8;
match Layout::array::<T>(n) {
Some(ref k) if k.size() > 0 => {
Ok(self.dealloc(raw_ptr, k.clone()))
Expand Down Expand Up @@ -804,9 +806,9 @@ pub unsafe trait Allocator {
n_old: usize,
n_new: usize) -> Option<Unique<T>>
where Self: Sized {
let (k_old, k_new, ptr) = (Layout::array_unchecked::<T>(n_old),
Layout::array_unchecked::<T>(n_new),
*ptr);
let (k_old, k_new, ptr) = ( Layout::array_unchecked::<T>(n_old)
, Layout::array_unchecked::<T>(n_new)
, ptr.as_ptr());
self.realloc_unchecked(ptr as *mut u8, k_old, k_new)
.map(|p|Unique::new(*p as *mut T))
}
Expand All @@ -821,7 +823,7 @@ pub unsafe trait Allocator {
unsafe fn dealloc_array_unchecked<T>(&mut self, ptr: Unique<T>, n: usize)
where Self: Sized {
let layout = Layout::array_unchecked::<T>(n);
self.dealloc(*ptr as *mut u8, layout);
self.dealloc(ptr.as_ptr() as *mut u8, layout);
}
}

Expand Down
4 changes: 2 additions & 2 deletions paging/src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ impl ActivePML4 {
}

fn pml4(&self) -> &Table<PML4Level> {
unsafe { self.0.get() }
unsafe { self.0.as_ref() }
}

fn pml4_mut(&mut self) -> &mut Table<PML4Level> {
unsafe { self.0.get_mut() }
unsafe { self.0.as_mut() }
}

/// Returns true if the given page is mapped.
Expand Down

0 comments on commit 607ba69

Please sign in to comment.