From 498ac2997420f7b25f7cd0a3f8202950d8ad93ec Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Fri, 6 Apr 2018 00:30:20 +0200 Subject: [PATCH] Use the new GlobalAlloc trait --- src/destructors.md | 16 ++++++++-------- src/vec-final.md | 21 ++++++++++----------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/destructors.md b/src/destructors.md index 6656747..7b6b47e 100644 --- a/src/destructors.md +++ b/src/destructors.md @@ -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::heap::{Heap, Alloc, Layout}; +use std::alloc::{Global, GlobalAlloc, Layout, Void}; use std::mem; use std::ptr::{drop_in_place, Unique}; @@ -38,7 +38,7 @@ impl Drop for Box { fn drop(&mut self) { unsafe { drop_in_place(self.ptr.as_ptr()); - Heap.dealloc(self.ptr.as_ptr() as *mut u8, Layout::new::()) + Global.dealloc(self.ptr.as_ptr() as *mut Void, Layout::new::()) } } } @@ -54,7 +54,7 @@ However this wouldn't work: ```rust #![feature(allocator_api, ptr_internals, unique)] -use std::heap::{Heap, Alloc, Layout}; +use std::alloc::{Global, GlobalAlloc, Layout, Void}; use std::ptr::{drop_in_place, Unique}; use std::mem; @@ -64,7 +64,7 @@ impl Drop for Box { fn drop(&mut self) { unsafe { drop_in_place(self.ptr.as_ptr()); - Heap.dealloc(self.ptr.as_ptr() as *mut u8, Layout::new::()); + Global.dealloc(self.ptr.as_ptr() as *mut Void, Layout::new::()); } } } @@ -76,7 +76,7 @@ impl Drop for SuperBox { unsafe { // Hyper-optimized: deallocate the box's contents for it // without `drop`ing the contents - Heap.dealloc(self.my_box.ptr.as_ptr() as *mut u8, Layout::new::()); + Global.dealloc(self.my_box.ptr.as_ptr() as *mut Void, Layout::new::()); } } } @@ -125,7 +125,7 @@ of Self during `drop` is to use an Option: ```rust #![feature(allocator_api, ptr_internals, unique)] -use std::heap::{Alloc, Heap, Layout}; +use std::alloc::{GlobalAlloc, Global, Layout, Void}; use std::ptr::{drop_in_place, Unique}; use std::mem; @@ -135,7 +135,7 @@ impl Drop for Box { fn drop(&mut self) { unsafe { drop_in_place(self.ptr.as_ptr()); - Heap.dealloc(self.ptr.as_ptr() as *mut u8, Layout::new::()); + Global.dealloc(self.ptr.as_ptr() as *mut Void, Layout::new::()); } } } @@ -149,7 +149,7 @@ impl Drop for SuperBox { // 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(); - Heap.dealloc(my_box.ptr.as_ptr() as *mut u8, Layout::new::()); + Global.dealloc(my_box.ptr.as_ptr() as *mut Void, Layout::new::()); mem::forget(my_box); } } diff --git a/src/vec-final.md b/src/vec-final.md index 70a7a6b..110d5e2 100644 --- a/src/vec-final.md +++ b/src/vec-final.md @@ -9,7 +9,7 @@ use std::ptr::{Unique, self}; use std::mem; use std::ops::{Deref, DerefMut}; use std::marker::PhantomData; -use std::heap::{Alloc, Layout, Heap}; +use std::alloc::{GlobalAlloc, Layout, Global}; struct RawVec { ptr: Unique, @@ -34,21 +34,20 @@ impl RawVec { assert!(elem_size != 0, "capacity overflow"); let (new_cap, ptr) = if self.cap == 0 { - let ptr = Heap.alloc(Layout::array::(1).unwrap()); + let ptr = Global.alloc(Layout::array::(1).unwrap()); (1, ptr) } else { let new_cap = 2 * self.cap; - let ptr = Heap.realloc(self.ptr.as_ptr() as *mut _, - Layout::array::(self.cap).unwrap(), - Layout::array::(new_cap).unwrap()); + let ptr = Global.realloc(self.ptr.as_ptr() as *mut _, + Layout::array::(self.cap).unwrap(), + Layout::array::(new_cap).unwrap().size()); (new_cap, ptr) }; // If allocate or reallocate fail, oom - let ptr = match ptr { - Ok(ptr) => ptr, - Err(err) => Heap.oom(err), - }; + if ptr.is_null() { + Global.oom() + } self.ptr = Unique::new_unchecked(ptr as *mut _); self.cap = new_cap; @@ -61,8 +60,8 @@ impl Drop for RawVec { let elem_size = mem::size_of::(); if self.cap != 0 && elem_size != 0 { unsafe { - Heap.dealloc(self.ptr.as_ptr() as *mut _, - Layout::array::(self.cap).unwrap()); + Global.dealloc(self.ptr.as_ptr() as *mut _, + Layout::array::(self.cap).unwrap()); } } }