Skip to content

Commit

Permalink
Rename alloc::Void to alloc::Opaque
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Apr 12, 2018
1 parent ed29777 commit f607a38
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 140 deletions.
2 changes: 1 addition & 1 deletion src/doc/nomicon
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ looks like:
```rust
#![feature(global_allocator, allocator_api, heap_api)]

use std::alloc::{GlobalAlloc, System, Layout, Void};
use std::alloc::{GlobalAlloc, System, Layout, Opaque};
use std::ptr::NonNull;

struct MyAllocator;

unsafe impl GlobalAlloc for MyAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut Void {
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
System.alloc(layout)
}

unsafe fn dealloc(&self, ptr: *mut Void, layout: Layout) {
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
System.dealloc(ptr, layout)
}
}
Expand Down
26 changes: 13 additions & 13 deletions src/liballoc/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,36 +76,36 @@ pub const Heap: Global = Global;

unsafe impl GlobalAlloc for Global {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut Void {
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
#[cfg(not(stage0))]
let ptr = __rust_alloc(layout.size(), layout.align());
#[cfg(stage0)]
let ptr = __rust_alloc(layout.size(), layout.align(), &mut 0);
ptr as *mut Void
ptr as *mut Opaque
}

#[inline]
unsafe fn dealloc(&self, ptr: *mut Void, layout: Layout) {
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
__rust_dealloc(ptr as *mut u8, layout.size(), layout.align())
}

#[inline]
unsafe fn realloc(&self, ptr: *mut Void, layout: Layout, new_size: usize) -> *mut Void {
unsafe fn realloc(&self, ptr: *mut Opaque, layout: Layout, new_size: usize) -> *mut Opaque {
#[cfg(not(stage0))]
let ptr = __rust_realloc(ptr as *mut u8, layout.size(), layout.align(), new_size);
#[cfg(stage0)]
let ptr = __rust_realloc(ptr as *mut u8, layout.size(), layout.align(),
new_size, layout.align(), &mut 0);
ptr as *mut Void
ptr as *mut Opaque
}

#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Void {
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Opaque {
#[cfg(not(stage0))]
let ptr = __rust_alloc_zeroed(layout.size(), layout.align());
#[cfg(stage0)]
let ptr = __rust_alloc_zeroed(layout.size(), layout.align(), &mut 0);
ptr as *mut Void
ptr as *mut Opaque
}

#[inline]
Expand All @@ -121,27 +121,27 @@ unsafe impl GlobalAlloc for Global {

unsafe impl Alloc for Global {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> {
NonNull::new(GlobalAlloc::alloc(self, layout)).ok_or(AllocErr)
}

#[inline]
unsafe fn dealloc(&mut self, ptr: NonNull<Void>, layout: Layout) {
unsafe fn dealloc(&mut self, ptr: NonNull<Opaque>, layout: Layout) {
GlobalAlloc::dealloc(self, ptr.as_ptr(), layout)
}

#[inline]
unsafe fn realloc(&mut self,
ptr: NonNull<Void>,
ptr: NonNull<Opaque>,
layout: Layout,
new_size: usize)
-> Result<NonNull<Void>, AllocErr>
-> Result<NonNull<Opaque>, AllocErr>
{
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
}

#[inline]
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> {
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
}

Expand Down Expand Up @@ -178,7 +178,7 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
if size != 0 {
let layout = Layout::from_size_align_unchecked(size, align);
Global.dealloc(ptr as *mut Void, layout);
Global.dealloc(ptr as *mut Opaque, layout);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl<T: ?Sized> Arc<T> {

if self.inner().weak.fetch_sub(1, Release) == 1 {
atomic::fence(Acquire);
Global.dealloc(self.ptr.as_void(), Layout::for_value(self.ptr.as_ref()))
Global.dealloc(self.ptr.as_opaque(), Layout::for_value(self.ptr.as_ref()))
}
}

Expand Down Expand Up @@ -637,7 +637,7 @@ impl<T: Clone> ArcFromSlice<T> for Arc<[T]> {
let slice = from_raw_parts_mut(self.elems, self.n_elems);
ptr::drop_in_place(slice);

Global.dealloc(self.mem.as_void(), self.layout.clone());
Global.dealloc(self.mem.as_opaque(), self.layout.clone());
}
}
}
Expand Down Expand Up @@ -1156,7 +1156,7 @@ impl<T: ?Sized> Drop for Weak<T> {
if self.inner().weak.fetch_sub(1, Release) == 1 {
atomic::fence(Acquire);
unsafe {
Global.dealloc(self.ptr.as_void(), Layout::for_value(self.ptr.as_ref()))
Global.dealloc(self.ptr.as_opaque(), Layout::for_value(self.ptr.as_ref()))
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/liballoc/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<K, V> Root<K, V> {
self.as_mut().as_leaf_mut().parent = ptr::null();

unsafe {
Global.dealloc(NonNull::from(top).as_void(), Layout::new::<InternalNode<K, V>>());
Global.dealloc(NonNull::from(top).as_opaque(), Layout::new::<InternalNode<K, V>>());
}
}
}
Expand Down Expand Up @@ -435,7 +435,7 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::Leaf> {
> {
let node = self.node;
let ret = self.ascend().ok();
Global.dealloc(node.as_void(), Layout::new::<LeafNode<K, V>>());
Global.dealloc(node.as_opaque(), Layout::new::<LeafNode<K, V>>());
ret
}
}
Expand All @@ -456,7 +456,7 @@ impl<K, V> NodeRef<marker::Owned, K, V, marker::Internal> {
> {
let node = self.node;
let ret = self.ascend().ok();
Global.dealloc(node.as_void(), Layout::new::<InternalNode<K, V>>());
Global.dealloc(node.as_opaque(), Layout::new::<InternalNode<K, V>>());
ret
}
}
Expand Down Expand Up @@ -1239,12 +1239,12 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
}

Global.dealloc(
right_node.node.as_void(),
right_node.node.as_opaque(),
Layout::new::<InternalNode<K, V>>(),
);
} else {
Global.dealloc(
right_node.node.as_void(),
right_node.node.as_opaque(),
Layout::new::<LeafNode<K, V>>(),
);
}
Expand Down
12 changes: 6 additions & 6 deletions src/liballoc/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#![allow(deprecated)]

pub use alloc::{Layout, AllocErr, CannotReallocInPlace, Void};
pub use alloc::{Layout, AllocErr, CannotReallocInPlace, Opaque};
use core::alloc::Alloc as CoreAlloc;
use core::ptr::NonNull;

Expand Down Expand Up @@ -54,7 +54,7 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
}

unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
let ptr = NonNull::new_unchecked(ptr as *mut Void);
let ptr = NonNull::new_unchecked(ptr as *mut Opaque);
CoreAlloc::dealloc(self, ptr, layout)
}

Expand All @@ -70,7 +70,7 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
ptr: *mut u8,
layout: Layout,
new_layout: Layout) -> Result<*mut u8, AllocErr> {
let ptr = NonNull::new_unchecked(ptr as *mut Void);
let ptr = NonNull::new_unchecked(ptr as *mut Opaque);
CoreAlloc::realloc(self, ptr, layout, new_layout.size()).map(|ptr| ptr.cast().as_ptr())
}

Expand All @@ -87,7 +87,7 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
ptr: *mut u8,
layout: Layout,
new_layout: Layout) -> Result<Excess, AllocErr> {
let ptr = NonNull::new_unchecked(ptr as *mut Void);
let ptr = NonNull::new_unchecked(ptr as *mut Opaque);
CoreAlloc::realloc_excess(self, ptr, layout, new_layout.size())
.map(|e| Excess(e.0 .cast().as_ptr(), e.1))
}
Expand All @@ -96,15 +96,15 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
ptr: *mut u8,
layout: Layout,
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
let ptr = NonNull::new_unchecked(ptr as *mut Void);
let ptr = NonNull::new_unchecked(ptr as *mut Opaque);
CoreAlloc::grow_in_place(self, ptr, layout, new_layout.size())
}

unsafe fn shrink_in_place(&mut self,
ptr: *mut u8,
layout: Layout,
new_layout: Layout) -> Result<(), CannotReallocInPlace> {
let ptr = NonNull::new_unchecked(ptr as *mut Void);
let ptr = NonNull::new_unchecked(ptr as *mut Opaque);
CoreAlloc::shrink_in_place(self, ptr, layout, new_layout.size())
}
}
22 changes: 11 additions & 11 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl<T, A: Alloc> RawVec<T, A> {

// handles ZSTs and `cap = 0` alike
let ptr = if alloc_size == 0 {
NonNull::<T>::dangling().as_void()
NonNull::<T>::dangling().as_opaque()
} else {
let align = mem::align_of::<T>();
let result = if zeroed {
Expand Down Expand Up @@ -310,7 +310,7 @@ impl<T, A: Alloc> RawVec<T, A> {
let new_cap = 2 * self.cap;
let new_size = new_cap * elem_size;
alloc_guard(new_size).expect("capacity overflow");
let ptr_res = self.a.realloc(NonNull::from(self.ptr).as_void(),
let ptr_res = self.a.realloc(NonNull::from(self.ptr).as_opaque(),
cur,
new_size);
match ptr_res {
Expand Down Expand Up @@ -369,7 +369,7 @@ impl<T, A: Alloc> RawVec<T, A> {
let new_cap = 2 * self.cap;
let new_size = new_cap * elem_size;
alloc_guard(new_size).expect("capacity overflow");
match self.a.grow_in_place(NonNull::from(self.ptr).as_void(), old_layout, new_size) {
match self.a.grow_in_place(NonNull::from(self.ptr).as_opaque(), old_layout, new_size) {
Ok(_) => {
// We can't directly divide `size`.
self.cap = new_cap;
Expand Down Expand Up @@ -426,7 +426,7 @@ impl<T, A: Alloc> RawVec<T, A> {
let res = match self.current_layout() {
Some(layout) => {
debug_assert!(new_layout.align() == layout.align());
self.a.realloc(NonNull::from(self.ptr).as_void(), layout, new_layout.size())
self.a.realloc(NonNull::from(self.ptr).as_opaque(), layout, new_layout.size())
}
None => self.a.alloc(new_layout),
};
Expand Down Expand Up @@ -535,7 +535,7 @@ impl<T, A: Alloc> RawVec<T, A> {
let res = match self.current_layout() {
Some(layout) => {
debug_assert!(new_layout.align() == layout.align());
self.a.realloc(NonNull::from(self.ptr).as_void(), layout, new_layout.size())
self.a.realloc(NonNull::from(self.ptr).as_opaque(), layout, new_layout.size())
}
None => self.a.alloc(new_layout),
};
Expand Down Expand Up @@ -601,7 +601,7 @@ impl<T, A: Alloc> RawVec<T, A> {
// FIXME: may crash and burn on over-reserve
alloc_guard(new_layout.size()).expect("capacity overflow");
match self.a.grow_in_place(
NonNull::from(self.ptr).as_void(), old_layout, new_layout.size(),
NonNull::from(self.ptr).as_opaque(), old_layout, new_layout.size(),
) {
Ok(_) => {
self.cap = new_cap;
Expand Down Expand Up @@ -662,7 +662,7 @@ impl<T, A: Alloc> RawVec<T, A> {
let new_size = elem_size * amount;
let align = mem::align_of::<T>();
let old_layout = Layout::from_size_align_unchecked(old_size, align);
match self.a.realloc(NonNull::from(self.ptr).as_void(),
match self.a.realloc(NonNull::from(self.ptr).as_opaque(),
old_layout,
new_size) {
Ok(p) => self.ptr = p.cast().into(),
Expand Down Expand Up @@ -698,7 +698,7 @@ impl<T, A: Alloc> RawVec<T, A> {
let elem_size = mem::size_of::<T>();
if elem_size != 0 {
if let Some(layout) = self.current_layout() {
self.a.dealloc(NonNull::from(self.ptr).as_void(), layout);
self.a.dealloc(NonNull::from(self.ptr).as_opaque(), layout);
}
}
}
Expand Down Expand Up @@ -734,7 +734,7 @@ fn alloc_guard(alloc_size: usize) -> Result<(), CollectionAllocErr> {
#[cfg(test)]
mod tests {
use super::*;
use alloc::Void;
use alloc::Opaque;

#[test]
fn allocator_param() {
Expand All @@ -754,7 +754,7 @@ mod tests {
// before allocation attempts start failing.
struct BoundedAlloc { fuel: usize }
unsafe impl Alloc for BoundedAlloc {
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Void>, AllocErr> {
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> {
let size = layout.size();
if size > self.fuel {
return Err(AllocErr);
Expand All @@ -764,7 +764,7 @@ mod tests {
err @ Err(_) => err,
}
}
unsafe fn dealloc(&mut self, ptr: NonNull<Void>, layout: Layout) {
unsafe fn dealloc(&mut self, ptr: NonNull<Opaque>, layout: Layout) {
Global.dealloc(ptr, layout)
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ use core::ops::CoerceUnsized;
use core::ptr::{self, NonNull};
use core::convert::From;

use alloc::{Global, Alloc, Layout, Void, box_free};
use alloc::{Global, Alloc, Layout, Opaque, box_free};
use string::String;
use vec::Vec;

Expand Down Expand Up @@ -737,7 +737,7 @@ impl<T: Clone> RcFromSlice<T> for Rc<[T]> {
// In the event of a panic, elements that have been written
// into the new RcBox will be dropped, then the memory freed.
struct Guard<T> {
mem: NonNull<Void>,
mem: NonNull<Opaque>,
elems: *mut T,
layout: Layout,
n_elems: usize,
Expand All @@ -760,7 +760,7 @@ impl<T: Clone> RcFromSlice<T> for Rc<[T]> {
let v_ptr = v as *const [T];
let ptr = Self::allocate_for_ptr(v_ptr);

let mem = ptr as *mut _ as *mut Void;
let mem = ptr as *mut _ as *mut Opaque;
let layout = Layout::for_value(&*ptr);

// Pointer to first element
Expand Down Expand Up @@ -844,7 +844,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
self.dec_weak();

if self.weak() == 0 {
Global.dealloc(self.ptr.as_void(), Layout::for_value(self.ptr.as_ref()));
Global.dealloc(self.ptr.as_opaque(), Layout::for_value(self.ptr.as_ref()));
}
}
}
Expand Down Expand Up @@ -1268,7 +1268,7 @@ impl<T: ?Sized> Drop for Weak<T> {
// the weak count starts at 1, and will only go to zero if all
// the strong pointers have disappeared.
if self.weak() == 0 {
Global.dealloc(self.ptr.as_void(), Layout::for_value(self.ptr.as_ref()));
Global.dealloc(self.ptr.as_opaque(), Layout::for_value(self.ptr.as_ref()));
}
}
}
Expand Down
Loading

0 comments on commit f607a38

Please sign in to comment.