Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Provide malloc'd fiberstacks (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhil authored Jul 12, 2023
1 parent 87378fd commit 149bcdb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/fibre/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ impl FiberStack {
Ok(Self(imp::FiberStack::new(size)?))
}

/// Creates a new fiber stack of the given size (using malloc).
pub fn malloc(size: usize) -> io::Result<Self> {
Ok(Self(imp::FiberStack::malloc(size)?))
}

/// Creates a new fiber stack with the given pointer to the bottom of the
/// stack plus the byte length of the stack.
///
Expand Down
12 changes: 12 additions & 0 deletions crates/fibre/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#![allow(unused_macros)]

use crate::RunResult;
use std::alloc::{alloc, dealloc, Layout};
use std::cell::Cell;
use std::io;
use std::ops::Range;
Expand Down Expand Up @@ -83,6 +84,14 @@ impl FiberStack {
}
}

pub fn malloc(size: usize) -> io::Result<Self> {
unsafe {
let layout = Layout::array::<u8>(size).unwrap();
let base = alloc(layout);
FiberStack::from_raw_parts(base, size)
}
}

pub unsafe fn from_raw_parts(base: *mut u8, len: usize) -> io::Result<Self> {
Ok(Self {
top: base.add(len),
Expand Down Expand Up @@ -115,6 +124,9 @@ impl Drop for FiberStack {
if self.mmap {
let ret = rustix::mm::munmap(self.top.sub(self.len) as _, self.len);
debug_assert!(ret.is_ok());
} else {
let layout = Layout::array::<u8>(self.len).unwrap();
dealloc(self.top.sub(self.len), layout);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/fibre/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ impl FiberStack {
Ok(Self(size))
}

pub fn malloc(_size: usize) -> io::Result<Self> {
unimplemented!()
}

pub unsafe fn from_raw_parts(_base: *mut u8, _len: usize) -> io::Result<Self> {
Err(io::Error::from_raw_os_error(ERROR_NOT_SUPPORTED as i32))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/runtime/src/continuation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ pub fn cont_new(
let args_ptr = payload.data;
let fiber = Box::new(
Fiber::new(
FiberStack::new(4096).unwrap(),
FiberStack::malloc(4096).unwrap(),
move |_first_val: (), _suspend: &Yield| unsafe {
f(callee_ctx, caller_ctx, args_ptr as *mut ValRaw, capacity)
},
Expand Down

0 comments on commit 149bcdb

Please sign in to comment.