From 84179ba915c86daa3f5169477063ea8cfcd062e2 Mon Sep 17 00:00:00 2001 From: Clifton King Date: Mon, 30 Dec 2019 13:50:59 -0500 Subject: [PATCH] vec examples compiling --- src/vec-alloc.md | 17 +++++++++-------- src/vec-dealloc.md | 5 ++++- src/vec-insert-remove.md | 10 +++++----- src/vec-into-iter.md | 5 +++-- src/vec-push-pop.md | 4 ++-- src/vec-raw.md | 35 +++++++++++++++++++++++------------ 6 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/vec-alloc.md b/src/vec-alloc.md index 2889a73..54b67d0 100644 --- a/src/vec-alloc.md +++ b/src/vec-alloc.md @@ -157,7 +157,7 @@ such we will guard against this case explicitly. Ok with all the nonsense out of the way, let's actually allocate some memory: ```rust,ignore -use std::alloc::oom; +use std::alloc::{alloc, realloc, Layout, rust_oom}; fn grow(&mut self) { // this is all pretty delicate, so let's say it's all unsafe @@ -167,7 +167,8 @@ fn grow(&mut self) { let elem_size = mem::size_of::(); let (new_cap, ptr) = if self.cap == 0 { - let ptr = heap::allocate(elem_size, align); + let layout = Layout::from_size_align_unchecked(elem_size, align); + let ptr = alloc(layout); (1, ptr) } else { // as an invariant, we can assume that `self.cap < isize::MAX`, @@ -186,17 +187,17 @@ fn grow(&mut self) { "capacity overflow"); let new_num_bytes = old_num_bytes * 2; - let ptr = heap::reallocate(self.ptr.as_ptr() as *mut _, - old_num_bytes, - new_num_bytes, - align); + let layout = Layout::from_size_align_unchecked(old_num_bytes, align); + let ptr = realloc(self.ptr.as_ptr() as *mut _, layout, num_new_bytes); (new_cap, ptr) }; // If allocate or reallocate fail, we'll get `null` back - if ptr.is_null() { oom(); } + if ptr.is_null() { + rust_oom(Layout::from_size_align_unchecked(new_cap * elem_size, align)); + } - self.ptr = Unique::new(ptr as *mut _); + self.ptr = Unique::new(ptr as *mut _).unwrap(); self.cap = new_cap; } } diff --git a/src/vec-dealloc.md b/src/vec-dealloc.md index 2f1b0b5..823ed83 100644 --- a/src/vec-dealloc.md +++ b/src/vec-dealloc.md @@ -12,6 +12,8 @@ haven't actually allocated any memory. ```rust,ignore +use std::alloc::dealloc; + impl Drop for Vec { fn drop(&mut self) { if self.cap != 0 { @@ -21,7 +23,8 @@ impl Drop for Vec { let elem_size = mem::size_of::(); let num_bytes = elem_size * self.cap; unsafe { - heap::deallocate(self.ptr.as_ptr() as *mut _, num_bytes, align); + let layout = Layout::from_size_align_unchecked(num_bytes, align); + dealloc(self.ptr.as_ptr() as *mut _, layout); } } } diff --git a/src/vec-insert-remove.md b/src/vec-insert-remove.md index 2c14bc4..d4b27e5 100644 --- a/src/vec-insert-remove.md +++ b/src/vec-insert-remove.md @@ -22,11 +22,11 @@ pub fn insert(&mut self, index: usize, elem: T) { unsafe { if index < self.len { // ptr::copy(src, dest, len): "copy from source to dest len elems" - ptr::copy(self.ptr.offset(index as isize), - self.ptr.offset(index as isize + 1), + ptr::copy(self.ptr.as_ptr().offset(index as isize), + self.ptr.as_ptr().offset(index as isize + 1), self.len - index); } - ptr::write(self.ptr.offset(index as isize), elem); + ptr::write(self.ptr.as_ptr().offset(index as isize), elem); self.len += 1; } } @@ -42,8 +42,8 @@ pub fn remove(&mut self, index: usize) -> T { unsafe { self.len -= 1; let result = ptr::read(self.ptr.offset(index as isize)); - ptr::copy(self.ptr.offset(index as isize + 1), - self.ptr.offset(index as isize), + ptr::copy(self.ptr.as_ptr().offset(index as isize + 1), + self.ptr.as_ptr().offset(index as isize), self.len - index); result } diff --git a/src/vec-into-iter.md b/src/vec-into-iter.md index df36757..32d9115 100644 --- a/src/vec-into-iter.md +++ b/src/vec-into-iter.md @@ -73,7 +73,7 @@ impl Vec { // can't offset off this pointer, it's not allocated! *ptr } else { - ptr.offset(len as isize) + ptr.as_ptr().offset(len as isize) } } } @@ -139,7 +139,8 @@ impl Drop for IntoIter { let elem_size = mem::size_of::(); let num_bytes = elem_size * self.cap; unsafe { - heap::deallocate(self.buf.as_ptr() as *mut _, num_bytes, align); + let layout = Layout::from_size_align_unchecked(num_bytes, align); + dealloc(self.buf.as_ptr() as *mut _, layout); } } } diff --git a/src/vec-push-pop.md b/src/vec-push-pop.md index d31a74c..6f39a05 100644 --- a/src/vec-push-pop.md +++ b/src/vec-push-pop.md @@ -22,7 +22,7 @@ pub fn push(&mut self, elem: T) { if self.len == self.cap { self.grow(); } unsafe { - ptr::write(self.ptr.offset(self.len as isize), elem); + ptr::write(self.ptr.as_ptr().offset(self.len as isize), elem); } // Can't fail, we'll OOM first. @@ -48,7 +48,7 @@ pub fn pop(&mut self) -> Option { } else { self.len -= 1; unsafe { - Some(ptr::read(self.ptr.offset(self.len as isize))) + Some(ptr::read(self.ptr.as_ptr().offset(self.len as isize))) } } } diff --git a/src/vec-raw.md b/src/vec-raw.md index ad24b61..690883b 100644 --- a/src/vec-raw.md +++ b/src/vec-raw.md @@ -28,21 +28,31 @@ impl RawVec { let elem_size = mem::size_of::(); let (new_cap, ptr) = if self.cap == 0 { - let ptr = heap::allocate(elem_size, align); + let layout = Layout::from_size_align_unchecked(elem_size, align); + let ptr = alloc(layout); (1, ptr) } else { - let new_cap = 2 * self.cap; - let ptr = heap::reallocate(self.ptr.as_ptr() as *mut _, - self.cap * elem_size, - new_cap * elem_size, - align); + let new_cap = self.cap * 2; + let old_num_bytes = self.cap * elem_size; + assert!( + old_num_bytes <= (::std::isize::MAX as usize) / 2, + "Capacity overflow!" + ); + let num_new_bytes = old_num_bytes * 2; + let layout = Layout::from_size_align_unchecked(old_num_bytes, align); + let ptr = realloc(self.ptr.as_ptr() as *mut _, layout, num_new_bytes); (new_cap, ptr) }; // If allocate or reallocate fail, we'll get `null` back - if ptr.is_null() { oom() } + if ptr.is_null() { + rust_oom(Layout::from_size_align_unchecked( + new_cap * elem_size, + align, + )); + } - self.ptr = Unique::new(ptr as *mut _); + self.ptr = Unique::new(ptr as *mut _).unwrap(); self.cap = new_cap; } } @@ -56,7 +66,8 @@ impl Drop for RawVec { let elem_size = mem::size_of::(); let num_bytes = elem_size * self.cap; unsafe { - heap::deallocate(self.ptr.as_mut() as *mut _, num_bytes, align); + let layout = Layout::from_size_align_unchecked(num_bytes, align); + dealloc(self.ptr.as_ptr() as *mut _, layout); } } } @@ -81,7 +92,7 @@ impl Vec { } // push/pop/insert/remove largely unchanged: - // * `self.ptr -> self.ptr()` + // * `self.ptr.as_ptr() -> self.ptr()` // * `self.cap -> self.cap()` // * `self.grow -> self.buf.grow()` } @@ -123,8 +134,8 @@ impl Vec { mem::forget(self); IntoIter { - start: *buf.ptr, - end: buf.ptr.offset(len as isize), + start: buf.ptr.as_ptr(), + end: buf.ptr.as_ptr().offset(len as isize), _buf: buf, } }