Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
104 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,47 @@ | ||
// This file is part of libfringe, a low-level green threading library. | ||
// Copyright (c) whitequark <whitequark@whitequark.org> | ||
// See the LICENSE file included in this distribution. | ||
|
||
extern crate alloc; | ||
|
||
use core::slice; | ||
use self::alloc::heap; | ||
use self::alloc::boxed::Box; | ||
use stack::Stack; | ||
use stack::debug::StackId; | ||
|
||
/// OwnedStack holds a non-guarded, heap-allocated stack. | ||
#[derive(Debug)] | ||
pub struct OwnedStack(Box<[u8]>); | ||
pub struct OwnedStack { | ||
data: Box<[u8]>, | ||
id: StackId, | ||
} | ||
|
||
impl OwnedStack { | ||
/// Allocates a new stack with exactly `size` accessible bytes and alignment appropriate | ||
/// for the current platform using the default Rust allocator. | ||
pub fn new(size: usize) -> OwnedStack { | ||
unsafe { | ||
let ptr = heap::allocate(size, ::STACK_ALIGNMENT); | ||
OwnedStack(Box::from_raw(slice::from_raw_parts_mut(ptr, size))) | ||
OwnedStack { | ||
data: Box::from_raw(slice::from_raw_parts_mut(ptr, size)), | ||
id: StackId::register(ptr, ptr.offset(size as isize)), | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl ::stack::Stack for OwnedStack { | ||
unsafe impl Stack for OwnedStack { | ||
#[inline(always)] | ||
fn base(&self) -> *mut u8 { | ||
// The slice cannot wrap around the address space, so the conversion from usize | ||
// to isize will not wrap either. | ||
let len: isize = self.0.len() as isize; | ||
let len = self.data.len() as isize; | ||
unsafe { self.limit().offset(len) } | ||
} | ||
|
||
#[inline(always)] | ||
fn limit(&self) -> *mut u8 { | ||
self.0.as_ptr() as *mut u8 | ||
self.data.as_ptr() as *mut u8 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// This file is part of libfringe, a low-level green threading library. | ||
// Copyright (c) whitequark <whitequark@whitequark.org> | ||
// See the LICENSE file included in this distribution. | ||
|
||
use stack::Stack; | ||
use stack::debug::StackId; | ||
|
||
/// SliceStack holds a non-guarded stack allocated elsewhere and provided as a mutable slice. | ||
#[derive(Debug)] | ||
pub struct SliceStack<'a> { | ||
data: &'a mut [u8], | ||
id: StackId, | ||
} | ||
|
||
impl<'a> SliceStack<'a> { | ||
/// Creates a `SliceStack` from an existing slice. | ||
/// | ||
/// This function will automatically align the slice to make it suitable for | ||
/// use as a stack. However this function may panic if the slice is smaller | ||
/// than `STACK_ALIGNMENT`. | ||
pub fn new(slice: &'a mut [u8]) -> SliceStack<'a> { | ||
// Align the given slice so that it matches platform requirements | ||
let ptr = slice.as_ptr() as usize; | ||
let adjusted_ptr = (ptr + ::STACK_ALIGNMENT - 1) & !(::STACK_ALIGNMENT - 1); | ||
let offset = adjusted_ptr - ptr; | ||
let adjusted_len = (slice.len() - offset) & !(::STACK_ALIGNMENT - 1); | ||
|
||
SliceStack { | ||
id: StackId::register(adjusted_ptr as *mut u8, (adjusted_ptr + adjusted_len) as *mut u8), | ||
data: &mut slice[offset..(offset + adjusted_len)], | ||
} | ||
} | ||
} | ||
|
||
unsafe impl<'a> Stack for SliceStack<'a> { | ||
#[inline(always)] | ||
fn base(&self) -> *mut u8 { | ||
// The slice cannot wrap around the address space, so the conversion from usize | ||
// to isize will not wrap either. | ||
let len = self.data.len() as isize; | ||
unsafe { self.limit().offset(len) } | ||
} | ||
|
||
#[inline(always)] | ||
fn limit(&self) -> *mut u8 { | ||
self.data.as_ptr() as *mut u8 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters