From b79dcf3d79fa89c84a9b9bb53ede5e8dbe3225d8 Mon Sep 17 00:00:00 2001 From: edef Date: Sun, 10 Sep 2017 11:41:04 +0200 Subject: [PATCH] Handle liballoc changes --- src/lib.rs | 2 +- src/stack/owned_stack.rs | 5 +++-- tests/stack.rs | 17 +++++++++++------ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1a4957b..af4cf14 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. #![feature(asm, naked_functions, cfg_target_vendor, untagged_unions)] -#![cfg_attr(feature = "alloc", feature(alloc, heap_api))] +#![cfg_attr(feature = "alloc", feature(alloc, heap_api, allocator_api))] #![cfg_attr(test, feature(test))] #![no_std] diff --git a/src/stack/owned_stack.rs b/src/stack/owned_stack.rs index 42b9791..1ba3780 100644 --- a/src/stack/owned_stack.rs +++ b/src/stack/owned_stack.rs @@ -4,7 +4,8 @@ extern crate alloc; use core::slice; -use self::alloc::heap; +use self::alloc::heap::Heap; +use self::alloc::allocator::{Alloc, Layout}; use self::alloc::boxed::Box; use stack::Stack; @@ -18,7 +19,7 @@ impl OwnedStack { pub fn new(size: usize) -> OwnedStack { unsafe { let aligned_size = size & !(::STACK_ALIGNMENT - 1); - let ptr = heap::allocate(aligned_size, ::STACK_ALIGNMENT); + let ptr = Heap.alloc(Layout::from_size_align_unchecked(aligned_size, ::STACK_ALIGNMENT)).unwrap(); OwnedStack(Box::from_raw(slice::from_raw_parts_mut(ptr, aligned_size))) } } diff --git a/tests/stack.rs b/tests/stack.rs index 0956ffe..a2216b4 100644 --- a/tests/stack.rs +++ b/tests/stack.rs @@ -4,20 +4,25 @@ // http://apache.org/licenses/LICENSE-2.0> or the MIT license , at your option. This file may not be // copied, modified, or distributed except according to those terms. -#![feature(alloc, heap_api)] +#![feature(alloc, heap_api, allocator_api)] extern crate alloc; extern crate fringe; -use alloc::heap; +use alloc::heap::Heap; +use alloc::allocator::{Alloc, Layout}; use alloc::boxed::Box; use std::slice; use fringe::{STACK_ALIGNMENT, Stack, SliceStack, OwnedStack, OsStack}; +unsafe fn heap_allocate(size: usize, align: usize) -> *mut u8 { + Heap.alloc(Layout::from_size_align_unchecked(size, align)).expect("couldn't allocate") +} + #[test] fn slice_aligned() { unsafe { - let ptr = heap::allocate(16384, STACK_ALIGNMENT); + let ptr = heap_allocate(16384, STACK_ALIGNMENT); let mut slice = Box::from_raw(slice::from_raw_parts_mut(ptr, 16384)); let stack = SliceStack::new(&mut slice[4096..8192]); assert_eq!(stack.base() as usize & (STACK_ALIGNMENT - 1), 0); @@ -28,7 +33,7 @@ fn slice_aligned() { #[test] fn slice_unaligned() { unsafe { - let ptr = heap::allocate(16384, STACK_ALIGNMENT); + let ptr = heap_allocate(16384, STACK_ALIGNMENT); let mut slice = Box::from_raw(slice::from_raw_parts_mut(ptr, 16384)); let stack = SliceStack::new(&mut slice[4097..8193]); assert_eq!(stack.base() as usize & (STACK_ALIGNMENT - 1), 0); @@ -39,7 +44,7 @@ fn slice_unaligned() { #[test] fn slice_too_small() { unsafe { - let ptr = heap::allocate(STACK_ALIGNMENT, STACK_ALIGNMENT); + let ptr = heap_allocate(STACK_ALIGNMENT, STACK_ALIGNMENT); let mut slice = Box::from_raw(slice::from_raw_parts_mut(ptr, STACK_ALIGNMENT)); let stack = SliceStack::new(&mut slice[0..1]); assert_eq!(stack.base() as usize & (STACK_ALIGNMENT - 1), 0); @@ -51,7 +56,7 @@ fn slice_too_small() { #[should_panic(expected = "SliceStack too small")] fn slice_too_small_unaligned() { unsafe { - let ptr = heap::allocate(STACK_ALIGNMENT, STACK_ALIGNMENT); + let ptr = heap_allocate(STACK_ALIGNMENT, STACK_ALIGNMENT); let mut slice = Box::from_raw(slice::from_raw_parts_mut(ptr, STACK_ALIGNMENT)); SliceStack::new(&mut slice[1..2]); }