From 7aa58f43f7568be028fd24e6610122ddf425eaf1 Mon Sep 17 00:00:00 2001 From: edef Date: Wed, 15 Apr 2015 15:56:57 -0400 Subject: [PATCH] add StackSource and implement it for platform::Stack --- src/context.rs | 4 ++-- src/platform.rs | 11 ++++++++++- src/stack.rs | 5 +++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/context.rs b/src/context.rs index 6554a9d..8c11a7f 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,5 +1,5 @@ use core::prelude::*; -use platform::Stack; +use stack::StackSource; use arch::Registers; use platform; @@ -11,7 +11,7 @@ pub struct Context { impl Context { #[inline] pub unsafe fn new(f: F) -> Context where F: FnOnce() + Send + 'static { - let mut stack = Stack::new(4 << 20); + let mut stack = platform::StackSource::get_stack(4 << 20); let regs = Registers::new(&mut stack, f); Context { regs: regs, diff --git a/src/platform.rs b/src/platform.rs index d7d3995..f9d50f9 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -22,6 +22,15 @@ pub struct Stack { valgrind_id: libc::c_uint } +pub struct StackSource; + +impl stack::StackSource for StackSource { + type Output = Stack; + fn get_stack(size: usize) -> Stack { + Stack::new(size) + } +} + impl stack::Stack for Stack { fn top(&mut self) -> *mut u8 { unsafe { @@ -37,7 +46,7 @@ impl stack::Stack for Stack { } impl Stack { - pub fn new(size: usize) -> Stack { + fn new(size: usize) -> Stack { let page_size = env::page_size(); // round the page size up, diff --git a/src/stack.rs b/src/stack.rs index 81b8326..061a90f 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -2,3 +2,8 @@ pub trait Stack { fn top(&mut self) -> *mut u8; fn limit(&self) -> *const u8; } + +pub trait StackSource { + type Output: Stack; + fn get_stack(size: usize) -> Self::Output; +}