Skip to content

Commit

Permalink
Minimize some code and fix lints
Browse files Browse the repository at this point in the history
  • Loading branch information
DelSkayn committed Jul 11, 2024
1 parent 06d298e commit ff702c2
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 212 deletions.
25 changes: 4 additions & 21 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,6 @@ impl StackAllocator {
(block_addr..=(block_addr + size)).contains(&addr)
}

/// Creates an allocator with reserve capacity for atleast cap bytes of space not taking into
/// account alignment.
pub fn with_capacity(cap: usize) -> Self {
let layout = Layout::new::<BlockHeader>();
let block_size =
layout.size() + cap.next_power_of_two() + Layout::new::<BlockHeader>().size();

StackAllocator {
block: Some(unsafe { Self::alloc_new_block(block_size) }),
top: None,
}
}

pub fn alloc<T>(&mut self, t: T) -> Owned<T> {
self.alloc_with(|| t)
}

#[inline(always)]
pub fn alloc_with<T, F: FnOnce() -> T>(&mut self, f: F) -> Owned<T> {
#[inline(always)]
Expand Down Expand Up @@ -135,7 +118,7 @@ impl StackAllocator {
pub unsafe fn pop_dealloc(&mut self) {
let top = self.top.expect("invalid deallocation");
// if there is a top, there must be a block.
let mut block = self.block.unwrap();
let block = self.block.unwrap();
self.top = top.cast::<Option<Owned<u8>>>().read();

if Self::within_block(block, top.addr().get()) {
Expand Down Expand Up @@ -169,7 +152,7 @@ impl StackAllocator {
.unwrap()
.next_power_of_two()
};
let mut block = Self::alloc_new_block(new_alloc_size);
let block = Self::alloc_new_block(new_alloc_size);

block.as_mut().previous = Some(old_block);
self.block = Some(block);
Expand Down Expand Up @@ -270,7 +253,7 @@ mod test {
v: i,
_other: [0usize; 64],
};
let alloc = alloc.alloc(b);
let alloc = alloc.alloc_with(|| b);
assert!(!allocations.contains(&alloc));
allocations.push(alloc);
}
Expand All @@ -288,7 +271,7 @@ mod test {
let mut allocations_2 = Vec::new();

for i in 0..amount {
let alloc = alloc.alloc(i as u128);
let alloc = alloc.alloc_with(|| i as u128);
assert!(!allocations_2.contains(&alloc));
allocations_2.push(alloc);
}
Expand Down
163 changes: 2 additions & 161 deletions src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@ impl<T> Clone for Owned<T> {
}

impl<T> Owned<T> {
pub fn into_ref<'a>(self) -> Ref<'a, T> {
Ref {
ptr: self.ptr,
_marker: PhantomData,
}
}

pub fn into_mut<'a>(self) -> Mut<'a, T> {
Mut {
ptr: self.ptr,
_marker: PhantomData,
}
}

pub unsafe fn write(&self, v: T) {
self.ptr.as_ptr().write(v);
}
Expand Down Expand Up @@ -74,116 +60,6 @@ impl<T> From<&mut T> for Owned<T> {
}
}

pub struct Ref<'a, T> {
ptr: NonNull<T>,
_marker: PhantomData<&'a T>,
}

impl<T> Copy for Ref<'_, T> {}
impl<T> Clone for Ref<'_, T> {
fn clone(&self) -> Self {
*self
}
}

impl<'a, T> Ref<'a, T> {
pub fn into_owned(self) -> Owned<T> {
Owned {
ptr: self.ptr,
_marker: PhantomData,
}
}

pub unsafe fn read(&self) -> T {
self.ptr.as_ptr().read()
}
}

impl<'a, T> From<&'a T> for Ref<'a, T> {
fn from(value: &'a T) -> Self {
Ref {
ptr: NonNull::from(value),
_marker: PhantomData,
}
}
}

impl<'a, T> From<&'a mut T> for Ref<'a, T> {
fn from(value: &'a mut T) -> Self {
Ref {
ptr: NonNull::from(value),
_marker: PhantomData,
}
}
}

impl<'a, T> From<Mut<'a, T>> for Ref<'a, T> {
fn from(value: Mut<'a, T>) -> Self {
Ref {
ptr: value.ptr,
_marker: PhantomData,
}
}
}

pub struct Mut<'a, T> {
ptr: NonNull<T>,
_marker: PhantomData<&'a mut T>,
}

impl<T> Clone for Mut<'_, T> {
fn clone(&self) -> Self {
Self {
ptr: self.ptr,
_marker: PhantomData,
}
}
}

impl<'a, T> Mut<'a, T> {
pub unsafe fn write(&self, t: T) {
self.ptr.as_ptr().write(t)
}

pub unsafe fn read(&self) -> T {
self.ptr.as_ptr().read()
}

pub unsafe fn replace(&self, v: T) -> T {
std::ptr::replace(self.as_ptr(), v)
}

pub fn into_owned(self) -> Owned<T> {
Owned {
ptr: self.ptr,
_marker: PhantomData,
}
}

pub fn into_ref(self) -> Ref<'a, T> {
Ref {
ptr: self.ptr,
_marker: PhantomData,
}
}

pub fn reborrow<'b>(&mut self) -> Mut<'b, T> {
Mut {
ptr: self.ptr,
_marker: PhantomData,
}
}
}

impl<'a, T> From<&'a mut T> for Mut<'a, T> {
fn from(value: &'a mut T) -> Self {
Mut {
ptr: NonNull::from(value),
_marker: PhantomData,
}
}
}

macro_rules! impl_base_methods {
($ty:ident<$($lt:lifetime,)?$gen:ident>) => {

Expand Down Expand Up @@ -224,12 +100,6 @@ macro_rules! impl_base_methods {
}
}

pub fn from_nonnull(ptr: NonNull<$gen>) -> Self {
Self {
ptr,
_marker: PhantomData,
}
}

pub fn from_ptr(ptr: *mut $gen) -> Option<Self> {
NonNull::new(ptr).map(|x| Self {
Expand All @@ -245,27 +115,15 @@ macro_rules! impl_base_methods {
}
}

pub fn into_ptr(self) -> *mut $gen {
self.ptr.as_ptr()
}

pub fn as_ptr(&self) -> *mut $gen {
self.ptr.as_ptr()
}

pub fn into_nonnull(self) -> NonNull<$gen> {
self.ptr
}

pub fn as_nonnull(&self) -> NonNull<$gen> {
self.ptr
}

pub unsafe fn as_ref(&self) -> &$($lt)? $gen {
pub unsafe fn as_ref<'a>(self) -> &'a $($lt)? $gen {
self.ptr.as_ref()
}

pub unsafe fn as_mut(&mut self) -> &$($lt)? mut $gen {
pub unsafe fn as_mut<'a>(mut self) -> &'a $($lt)? mut $gen {
self.ptr.as_mut()
}

Expand All @@ -277,10 +135,6 @@ macro_rules! impl_base_methods {
self.map_addr(|x| NonZeroUsize::new_unchecked(x.get() - offset))
}

pub unsafe fn offset(self, offset: isize) -> Self{
self.map_addr(|x| NonZeroUsize::new_unchecked((x.get() as isize + offset) as usize))
}

pub unsafe fn offset_from(self, other: $ty<$gen>) -> isize{
self.ptr.as_ptr().offset_from(other.ptr.as_ptr())
}
Expand Down Expand Up @@ -338,17 +192,6 @@ macro_rules! impl_base_methods {
}
}

pub fn with_addr(self, addr: NonZeroUsize) -> Self{
#[cfg(feature = "nightly")]
{
unsafe{ Self::from_ptr_unchecked(self.ptr.as_ptr().with_addr(addr.get())) }
}
#[cfg(not(feature = "nightly"))]
{
unsafe{ Self::from_ptr_unchecked(addr.get() as *mut $gen) }
}
}

pub fn map_addr<F>(self, f: F) -> Self
where F: FnOnce(NonZeroUsize) -> NonZeroUsize
{
Expand Down Expand Up @@ -385,6 +228,4 @@ macro_rules! impl_base_methods {
};
}

impl_base_methods!(Ref<'a, T>);
impl_base_methods!(Mut<'a, T>);
impl_base_methods!(Owned<T>);
16 changes: 7 additions & 9 deletions src/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use crate::{
allocator::StackAllocator,
defer::Defer,
ptr::{map_ptr, Owned},
vtable::{TaskBox, VTable},
};
Expand Down Expand Up @@ -113,13 +114,10 @@ impl Stack {
F: FnOnce() -> R,
{
let ptr = STACK_PTR.with(|x| x.replace(Some(Owned::from(self))));
struct Dropper(Option<Owned<Stack>>);
impl Drop for Dropper {
fn drop(&mut self) {
STACK_PTR.with(|x| x.set(self.0))
}
}
let _dropper = Dropper(ptr);
let _defer = Defer::new(ptr, |ptr| {
let ptr = *ptr;
STACK_PTR.with(|x| x.set(ptr));
});
f()
}

Expand Down Expand Up @@ -234,12 +232,12 @@ impl Stack {

unsafe fn drive_task(drive: Owned<TaskBox<u8>>, context: &mut Context) -> Poll<()> {
let v_table = drive.map_ptr(map_ptr!(Owned<TaskBox<u8>>, v_table)).read();
(v_table.driver)(drive.into_mut(), context)
(v_table.driver)(drive, context)
}

unsafe fn drop_task_inline(drive: Owned<TaskBox<u8>>) {
let v_table = drive.map_ptr(map_ptr!(Owned<TaskBox<u8>>, v_table)).read();
(v_table.dropper)(drive.into_mut())
(v_table.dropper)(drive)
}

pub(crate) fn is_rebless_context(&self, context: &mut Context) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion src/stack/stk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl StackMarker for Stk {
unsafe fn create() -> &'static mut Self {
// Safety: Stk is an unsized typed so any pointer that is not null is a valid pointer to the type.
// Therefore we can create a static reference to the type from a dangling pointer.
unsafe { Owned::dangling().into_mut().as_mut() }
unsafe { Owned::dangling().as_mut() }
}
}

Expand Down
18 changes: 7 additions & 11 deletions src/tree/future.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{schedular::CancelToken, stk::ScopeStk, Stk};
use crate::{
ptr::{map_ptr, Mut, Owned},
ptr::{map_ptr, Owned},
stack::{future::InnerStkFuture, StackState},
Stack, TreeStack,
};
Expand Down Expand Up @@ -69,11 +69,10 @@ where

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
unsafe {
let mut this = Mut::from(self.get_unchecked_mut());
let this = Owned::from(self.get_unchecked_mut());
match this.as_ref().state {
ScopeFutureState::Initial(_) => {
let ScopeFutureState::Initial(x) = this
.reborrow()
.map_ptr(map_ptr!(Self, state))
.replace(ScopeFutureState::Running(Cell::new(None), Cell::new(None)))
else {
Expand Down Expand Up @@ -120,21 +119,18 @@ where
}
Poll::Pending
}
ScopeFutureState::Finished => return Poll::Pending,
ScopeFutureState::Finished => Poll::Pending,
}
}
}
}

impl<F, R> Drop for ScopeFuture<'_, F, R> {
fn drop(&mut self) {
match self.state {
ScopeFutureState::Running(_, ref mut c) => {
// drop the cancellation so that the future won't run anymore.
// Manually dropped first so that the future is always dropped before the place is.
std::mem::drop(c.take());
}
_ => {}
if let ScopeFutureState::Running(_, ref mut c) = self.state {
// drop the cancellation so that the future won't run anymore.
// Manually dropped first so that the future is always dropped before the place is.
std::mem::drop(c.take());
}
}
}
Expand Down
Loading

0 comments on commit ff702c2

Please sign in to comment.