Skip to content

Commit

Permalink
Realloc at the assignment side
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexclique committed Oct 15, 2019
1 parent 206b2af commit 4561833
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 59 deletions.
5 changes: 3 additions & 2 deletions lightproc/examples/task_build.rs
@@ -1,6 +1,7 @@
use lightproc::prelude::*;

fn main() {
LightProc::<()>::new()
.with_future(async move { println!("test"); });
LightProc::<()>::new().with_future(async move {
println!("test");
});
}
28 changes: 15 additions & 13 deletions lightproc/src/layout_helpers.rs
@@ -1,29 +1,31 @@
use std::alloc::{Layout, LayoutErr};
use std::alloc::Layout;
use std::io::{Error, ErrorKind};

#[inline]
pub fn extend(layout: Layout, next: Layout) -> (Layout, usize) {
let new_align = std::cmp::max(layout.align(), next.align());
let pad = padding_needed_for(layout, next.align());

let offset = layout.size().checked_add(pad)
.ok_or(
Error::new(ErrorKind::Other, "Padding overflow check failed")
).unwrap();
let new_size = offset.checked_add(next.size())
.ok_or(
Error::new(ErrorKind::Other, "New size can't be computed")
).unwrap();
let offset = layout
.size()
.checked_add(pad)
.ok_or(Error::new(
ErrorKind::Other,
"Padding overflow check failed",
))
.unwrap();
let new_size = offset
.checked_add(next.size())
.ok_or(Error::new(ErrorKind::Other, "New size can't be computed"))
.unwrap();

let layout =
Layout::from_size_align(new_size, new_align).unwrap();
let layout = Layout::from_size_align(new_size, new_align).unwrap();
(layout, offset)
}

#[inline]
pub fn padding_needed_for(layout: Layout, align: usize) -> usize {
let len = layout.size();
let len_rounded_up = len.wrapping_add(align).wrapping_sub(1)
& !align.wrapping_sub(1);
let len_rounded_up = len.wrapping_add(align).wrapping_sub(1) & !align.wrapping_sub(1);
len_rounded_up.wrapping_sub(len)
}
8 changes: 4 additions & 4 deletions lightproc/src/lib.rs
@@ -1,12 +1,12 @@
pub mod layout_helpers;
pub mod lightproc;
pub mod proc_handle;
pub mod proc_data;
pub mod proc_handle;
pub mod proc_layout;
pub mod proc_vtable;
pub mod state;
pub mod raw_proc;
pub mod proc_layout;
pub mod stack;
pub mod layout_helpers;
pub mod state;

pub mod prelude {
pub use crate::lightproc::*;
Expand Down
38 changes: 23 additions & 15 deletions lightproc/src/lightproc.rs
@@ -1,19 +1,19 @@
use std::ptr::NonNull;
use std::marker::PhantomData as marker;
use std::alloc;
use std::future::Future;
use crate::proc_handle::ProcHandle;
use crate::raw_proc::RawProc;
use crate::stack::ProcStack;
use std::marker::PhantomData as marker;
use std::ptr::NonNull;

use crate::proc_layout::ProcLayout;
use std::alloc;
use std::alloc::Layout;

use crate::layout_helpers::extend;
use crate::raw_proc::RawProc;
use std::alloc::Layout;

pub struct LightProc<T> {
pub(crate) raw_proc: NonNull<()>,

pub(crate) proc_layout: ProcLayout,
pub(crate) _private: marker<T>
pub(crate) _private: marker<T>,
}

unsafe impl<T> Send for LightProc<T> {}
Expand All @@ -25,22 +25,30 @@ impl<T> LightProc<T> {

unsafe {
LightProc {
raw_proc: NonNull::dangling(),
raw_proc: NonNull::new(alloc::alloc(proc_layout.layout) as *mut ()).unwrap(),
proc_layout,
_private: marker
_private: marker,
}
}
}

pub fn with_future<F, R>(mut self, f: F)
where
F: Future<Output = R> + Send + 'static
F: Future<Output = R> + Send + 'static,
{
let fut_mem = Layout::new::<F>();
let (new_layout, offset_f) =
extend(self.proc_layout.layout, fut_mem);
let (new_layout, offset_f) = extend(self.proc_layout.layout, fut_mem);
self.proc_layout.layout = new_layout;
self.proc_layout.offset_table
.insert("future", offset_f);
self.proc_layout.offset_table.insert("future", offset_f);

unsafe {
alloc::realloc(
self.raw_proc.as_ptr() as *mut u8,
self.proc_layout.layout,
self.proc_layout.layout.size(),
);
}

// RawProc::from_ptr(self.raw_proc.as_ptr(), self.proc_layout);
}
}
6 changes: 3 additions & 3 deletions lightproc/src/proc_data.rs
@@ -1,7 +1,7 @@
use std::task::Waker;
use std::cell::UnsafeCell;
use crate::proc_vtable::ProcVTable;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::cell::UnsafeCell;
use std::sync::atomic::AtomicUsize;
use std::task::Waker;

pub(crate) struct ProcData {
pub(crate) state: AtomicUsize,
Expand Down
6 changes: 3 additions & 3 deletions lightproc/src/proc_handle.rs
@@ -1,9 +1,9 @@
use std::ptr::NonNull;
use std::marker::PhantomData as marker;
use std::ptr::NonNull;

pub struct ProcHandle<R, T> {
raw_proc: NonNull<()>,
_private: marker<(R, T)>
_private: marker<(R, T)>,
}

unsafe impl<R, T> Send for ProcHandle<R, T> {}
Expand All @@ -13,7 +13,7 @@ impl<R, T> Unpin for ProcHandle<R, T> {}

impl<R, T> ProcHandle<R, T> {
pub fn cancel(&self) {
let ptr = self.raw_proc.as_ptr();
let _ptr = self.raw_proc.as_ptr();
unimplemented!()
}
}
6 changes: 3 additions & 3 deletions lightproc/src/proc_layout.rs
@@ -1,18 +1,18 @@
use std::alloc::Layout;
use crate::stack::ProcStack;
use rustc_hash::FxHashMap;
use std::alloc::Layout;

#[derive(Clone)]
pub struct ProcLayout {
pub layout: Layout,
pub offset_table: FxHashMap<&'static str, usize>
pub offset_table: FxHashMap<&'static str, usize>,
}

impl Default for ProcLayout {
fn default() -> Self {
ProcLayout {
layout: Layout::new::<ProcStack>(),
offset_table: FxHashMap::default()
offset_table: FxHashMap::default(),
}
}
}
5 changes: 2 additions & 3 deletions lightproc/src/proc_vtable.rs
Expand Up @@ -5,7 +5,6 @@ pub(crate) struct ProcVTable {
pub(crate) raw_waker: RawWakerVTable,

pub(crate) schedule: unsafe fn(*const ()),

// // Callbacks
// pub(crate) callbacks: ProcCallbacks
// // Callbacks
// pub(crate) callbacks: ProcCallbacks
}
35 changes: 22 additions & 13 deletions lightproc/src/raw_proc.rs
@@ -1,12 +1,7 @@
use crate::proc_data::ProcData;
use std::future::Future;
use crate::lightproc::LightProc;
use std::ptr::NonNull;
use std::sync::atomic::AtomicUsize;
use std::cell::UnsafeCell;
use std::task::RawWakerVTable;
use std::alloc::{Layout, alloc};
use crate::proc_data::ProcData;
use crate::proc_layout::ProcLayout;
use std::future::Future;

/// Raw pointers to the fields of a task.
pub struct RawProc<F, R, S, T> {
Expand Down Expand Up @@ -36,11 +31,25 @@ impl<F, R, S, T> Clone for RawProc<F, R, S, T> {
}

impl<F, R, S, T> RawProc<F, R, S, T>
where
F: Future<Output = R> + Send + 'static,
R: Send + 'static,
S: Fn(LightProc<T>) + Send + Sync + 'static,
T: Send + 'static,
where
F: Future<Output = R> + Send + 'static,
R: Send + 'static,
S: Fn(LightProc<T>) + Send + Sync + 'static,
T: Send + 'static,
{

#[inline]
pub(crate) fn from_ptr(ptr: *const (), proc_layout: ProcLayout) -> Self {
let p = ptr as *const u8;

unsafe {
Self {
pdata: p as *const ProcData,
schedule: p.add(proc_layout.offset_table.get("schedule").cloned().unwrap())
as *const S,
stack: p.add(proc_layout.offset_table.get("stack").cloned().unwrap()) as *mut T,
future: p.add(proc_layout.offset_table.get("future").cloned().unwrap()) as *mut F,
output: p.add(proc_layout.offset_table.get("output").cloned().unwrap()) as *mut R,
}
}
}
}
1 change: 1 addition & 0 deletions lightproc/src/state.rs
@@ -0,0 +1 @@

0 comments on commit 4561833

Please sign in to comment.