On latest nightly works with System allocator

pull/71/head
Ivan 7 years ago
parent b4f8731919
commit 409e874167

@ -28,7 +28,7 @@ For instance, a custom implementation of `Box` might write `Drop` like this:
```rust
#![feature(ptr_internals, allocator_api, unique)]
use std::alloc::{Global, GlobalAlloc, Layout};
use std::alloc::{System, GlobalAlloc, Layout};
use std::mem;
use std::ptr::{drop_in_place, Unique};
@ -38,7 +38,7 @@ impl<T> Drop for Box<T> {
fn drop(&mut self) {
unsafe {
drop_in_place(self.ptr.as_ptr());
Global.dealloc(self.ptr.as_ptr() as *mut _, Layout::new::<T>())
System.dealloc(self.ptr.as_ptr() as *mut _, Layout::new::<T>())
}
}
}
@ -54,7 +54,7 @@ However this wouldn't work:
```rust
#![feature(allocator_api, ptr_internals, unique)]
use std::alloc::{Global, GlobalAlloc, Layout};
use std::alloc::{System, GlobalAlloc, Layout};
use std::ptr::{drop_in_place, Unique};
use std::mem;
@ -64,7 +64,7 @@ impl<T> Drop for Box<T> {
fn drop(&mut self) {
unsafe {
drop_in_place(self.ptr.as_ptr());
Global.dealloc(self.ptr.as_ptr() as *mut _, Layout::new::<T>());
System.dealloc(self.ptr.as_ptr() as *mut _, Layout::new::<T>());
}
}
}
@ -76,7 +76,7 @@ impl<T> Drop for SuperBox<T> {
unsafe {
// Hyper-optimized: deallocate the box's contents for it
// without `drop`ing the contents
Global.dealloc(self.my_box.ptr.as_ptr() as *mut _, Layout::new::<T>());
System.dealloc(self.my_box.ptr.as_ptr() as *mut _, Layout::new::<T>());
}
}
}
@ -125,7 +125,7 @@ of Self during `drop` is to use an Option:
```rust
#![feature(allocator_api, ptr_internals, unique)]
use std::alloc::{GlobalAlloc, Global, Layout};
use std::alloc::{GlobalAlloc, System, Layout};
use std::ptr::{drop_in_place, Unique};
use std::mem;
@ -135,7 +135,7 @@ impl<T> Drop for Box<T> {
fn drop(&mut self) {
unsafe {
drop_in_place(self.ptr.as_ptr());
Global.dealloc(self.ptr.as_ptr() as *mut _, Layout::new::<T>());
System.dealloc(self.ptr.as_ptr() as *mut _, Layout::new::<T>());
}
}
}
@ -149,7 +149,7 @@ impl<T> Drop for SuperBox<T> {
// without `drop`ing the contents. Need to set the `box`
// field as `None` to prevent Rust from trying to Drop it.
let my_box = self.my_box.take().unwrap();
Global.dealloc(my_box.ptr.as_ptr() as *mut _, Layout::new::<T>());
System.dealloc(my_box.ptr.as_ptr() as *mut _, Layout::new::<T>());
mem::forget(my_box);
}
}

@ -5,7 +5,7 @@
#![feature(allocator_api)]
#![feature(unique)]
use std::alloc::{oom, Global, GlobalAlloc, Layout};
use std::alloc::{oom, System, GlobalAlloc, Layout};
use std::marker::PhantomData;
use std::mem;
use std::ops::{Deref, DerefMut};
@ -37,11 +37,11 @@ impl<T> RawVec<T> {
assert!(elem_size != 0, "capacity overflow");
let (new_cap, ptr) = if self.cap == 0 {
let ptr = Global.alloc(Layout::array::<T>(1).unwrap());
let ptr = System.alloc(Layout::array::<T>(1).unwrap());
(1, ptr)
} else {
let new_cap = 2 * self.cap;
let ptr = Global.realloc(
let ptr = System.realloc(
self.ptr.as_ptr() as *mut _,
Layout::array::<T>(self.cap).unwrap(),
Layout::array::<T>(new_cap).unwrap().size(),
@ -65,7 +65,7 @@ impl<T> Drop for RawVec<T> {
let elem_size = mem::size_of::<T>();
if self.cap != 0 && elem_size != 0 {
unsafe {
Global.dealloc(
System.dealloc(
self.ptr.as_ptr() as *mut _,
Layout::array::<T>(self.cap).unwrap(),
);

Loading…
Cancel
Save