Fix to work with new allocator APIs.

This commit is contained in:
Gerd Zellweger 2019-02-07 17:18:13 -08:00 committed by edef
parent 1d97ab9d02
commit 9ca9c72e0e
3 changed files with 18 additions and 12 deletions

View File

@ -5,7 +5,7 @@
// http://opensource.org/licenses/MIT>, at your option. This file may not be // http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms. // copied, modified, or distributed except according to those terms.
#![feature(asm, naked_functions, cfg_target_vendor, untagged_unions)] #![feature(asm, naked_functions, cfg_target_vendor, untagged_unions)]
#![cfg_attr(feature = "alloc", feature(alloc, heap_api, allocator_api))] #![cfg_attr(feature = "alloc", feature(alloc, allocator_api))]
#![cfg_attr(test, feature(test))] #![cfg_attr(test, feature(test))]
#![no_std] #![no_std]
@ -33,6 +33,9 @@
#[macro_use] #[macro_use]
extern crate std; extern crate std;
#[cfg(feature = "alloc")]
extern crate alloc;
pub use stack::*; pub use stack::*;
pub use generator::Generator; pub use generator::Generator;

View File

@ -1,12 +1,11 @@
// This file is part of libfringe, a low-level green threading library. // This file is part of libfringe, a low-level green threading library.
// Copyright (c) whitequark <whitequark@whitequark.org> // Copyright (c) whitequark <whitequark@whitequark.org>
// See the LICENSE file included in this distribution. // See the LICENSE file included in this distribution.
extern crate alloc;
use core::slice; use core::slice;
use self::alloc::heap::Global; use alloc::alloc::alloc;
use self::alloc::allocator::{Alloc, Layout}; use alloc::boxed::Box;
use self::alloc::boxed::Box; use core::alloc::Layout;
use stack::Stack; use stack::Stack;
/// OwnedStack holds a non-guarded, heap-allocated stack. /// OwnedStack holds a non-guarded, heap-allocated stack.
@ -19,8 +18,11 @@ impl OwnedStack {
pub fn new(size: usize) -> OwnedStack { pub fn new(size: usize) -> OwnedStack {
unsafe { unsafe {
let aligned_size = size & !(::STACK_ALIGNMENT - 1); let aligned_size = size & !(::STACK_ALIGNMENT - 1);
let ptr = Global.alloc(Layout::from_size_align_unchecked(aligned_size, ::STACK_ALIGNMENT)).unwrap(); let ptr = alloc(Layout::from_size_align_unchecked(
OwnedStack(Box::from_raw(slice::from_raw_parts_mut(ptr.as_ptr() as *mut u8, aligned_size))) aligned_size,
::STACK_ALIGNMENT,
));
OwnedStack(Box::from_raw(slice::from_raw_parts_mut(ptr, aligned_size)))
} }
} }
} }

View File

@ -4,20 +4,21 @@
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be // http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms. // copied, modified, or distributed except according to those terms.
#![feature(alloc, heap_api, allocator_api)] #![feature(alloc, allocator_api)]
extern crate core;
extern crate alloc; extern crate alloc;
extern crate fringe; extern crate fringe;
use alloc::heap::Global; use alloc::alloc::alloc;
use alloc::allocator::{Alloc, Layout}; use core::alloc::Layout;
use alloc::boxed::Box; use alloc::boxed::Box;
use std::slice; use std::slice;
use fringe::{STACK_ALIGNMENT, Stack, SliceStack, OwnedStack, OsStack}; use fringe::{STACK_ALIGNMENT, Stack, SliceStack, OwnedStack, OsStack};
unsafe fn heap_allocate(size: usize, align: usize) -> *mut u8 { unsafe fn heap_allocate(size: usize, align: usize) -> *mut u8 {
let ptr = Global.alloc(Layout::from_size_align_unchecked(size, align)).expect("couldn't allocate"); alloc(Layout::from_size_align_unchecked(size, align))
ptr.as_ptr() as *mut u8
} }
#[test] #[test]