Removing some safety because of cost overhead

This commit is contained in:
Andrew Golovashevich 2025-12-12 13:25:33 +03:00
parent 42349a315e
commit 44288828a2
6 changed files with 38 additions and 49 deletions

View File

@ -1,5 +0,0 @@
pub unsafe trait Dyn<T: Sized>: Drop + Sized {
fn getRef<'s>(&'s self) -> &'s mut T;
fn free<'s>(&'s mut self);
}

View File

@ -1,27 +1,23 @@
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
pub unsafe trait GeneralPurposeHeap { pub unsafe trait GeneralPurposeHeap {
type Dyn<T: Sized>: crate::Dyn<T>; fn alloc<'s, T: Sized>(&'s mut self, value: T) -> &'s mut T {
return self.tryAlloc(value).unwrap_or_else(|| crate::panicOOM());
fn alloc<'s, T: Sized>(&'s mut self, value: T) -> Self::Dyn<T> {
return self
.tryAlloc(value)
.unwrap_or_else(|| panic!("Out of memory"));
} }
fn allocUninitialized<'s, T: Sized>(&'s mut self) -> Self::Dyn<MaybeUninit<T>> { fn allocUninitialized<'s, T: Sized>(&'s mut self) -> &'s mut MaybeUninit<T> {
return self return self
.tryAllocUninitialized() .tryAllocUninitialized()
.unwrap_or_else(|| panic!("Out of memory")); .unwrap_or_else(|| crate::panicOOM());
} }
fn allocZeroed<'s, T: Sized>(&'s mut self) -> Self::Dyn<MaybeUninit<T>> { fn allocZeroed<'s, T: Sized>(&'s mut self) -> &'s mut MaybeUninit<T> {
return self return self
.tryAllocZeroed() .tryAllocZeroed()
.unwrap_or_else(|| panic!("Out of memory")); .unwrap_or_else(|| crate::panicOOM());
} }
fn tryAlloc<'s, T: Sized>(&'s mut self, value: T) -> Option<Self::Dyn<T>>; fn tryAlloc<'s, T: Sized>(&'s mut self, value: T) -> Option<&'s mut T>;
fn tryAllocUninitialized<'s, T: Sized>(&'s mut self) -> Option<Self::Dyn<MaybeUninit<T>>>; fn tryAllocUninitialized<'s, T: Sized>(&'s mut self) -> Option<&'s mut MaybeUninit<T>>;
fn tryAllocZeroed<'s, T: Sized>(&'s mut self) -> Option<Self::Dyn<MaybeUninit<T>>>; fn tryAllocZeroed<'s, T: Sized>(&'s mut self) -> Option<&'s mut MaybeUninit<T>>;
} }

View File

@ -1,12 +1,16 @@
mod r#dyn; #[allow(drop_bounds)]
mod general; mod general;
mod pages; mod pages;
mod stack; mod stack;
mod typed; mod typed;
pub fn panicOOM() -> ! {
panic!("Out of memory");
}
pub use pages::Page; pub use pages::Page;
pub use pages::PagesManager; pub use pages::PagesManager;
pub use r#dyn::Dyn;
pub use stack::StackHeap; pub use stack::StackHeap;
pub use typed::TypedHeap; pub use typed::TypedHeap;
pub use general::GeneralPurposeHeap; pub use general::GeneralPurposeHeap;

View File

@ -1,12 +1,13 @@
use std::ptr::NonNull; use std::ptr::NonNull;
enum ExtensionResult { pub enum ExtensionResult {
OK, OK,
NO_MEMORY, NO_MEMORY,
EXTENSION_BUSY, EXTENSION_BUSY,
NOT_SUPPORTED,
} }
pub unsafe trait Page { pub unsafe trait Page: Drop + Sized {
fn getMemory(&self) -> NonNull<[u8]>; fn getMemory(&self) -> NonNull<[u8]>;
fn releasePage(self); fn releasePage(self);
@ -17,11 +18,11 @@ pub unsafe trait Page {
pub unsafe trait PagesManager { pub unsafe trait PagesManager {
type Page: Page; type Page: Page;
fn offerPage<'s>(&'s mut self, minsize: usize) -> &'s Self::Page { fn offerPage<'s>(&'s mut self, minsize: usize) -> Self::Page {
return self return self
.tryOfferPage(minsize) .tryOfferPage(minsize)
.unwrap_or_else(|| panic!("Out of memory")); .unwrap_or_else(|| panic!("Out of memory"));
} }
fn tryOfferPage<'s>(&'s mut self, minimalSize: usize) -> Option<&'s Self::Page>; fn tryOfferPage<'s>(&'s mut self, minimalSize: usize) -> Option<Self::Page>;
} }

View File

@ -1,25 +1,23 @@
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
pub unsafe trait StackHeap { pub unsafe trait StackHeap: Drop {
fn childScope<'s>(&'s mut self) -> impl StackHeap; fn childScope<'s>(&'s mut self) -> impl StackHeap;
fn alloc<'s, T: Sized>(&'s mut self, value: T) -> &'s mut T { fn alloc<'s, T: Sized>(&'s mut self, value: T) -> &'s mut T {
return self return self.tryAlloc(value).unwrap_or_else(|| crate::panicOOM());
.tryAlloc(value)
.unwrap_or_else(|| panic!("Out of memory"));
} }
fn allocUninitialized<'s, T: Sized>(&'s mut self) -> &'s mut MaybeUninit<T> { fn allocUninitialized<'s, T: Sized>(&'s mut self) -> &'s mut MaybeUninit<T> {
return self return self
.tryAllocUninitialized() .tryAllocUninitialized()
.unwrap_or_else(|| panic!("Out of memory")); .unwrap_or_else(|| crate::panicOOM());
} }
fn allocZeroed<'s, T: Sized>(&'s mut self) -> &'s mut MaybeUninit<T> { fn allocZeroed<'s, T: Sized>(&'s mut self) -> &'s mut MaybeUninit<T> {
return self return self.tryAllocZeroed().unwrap_or_else(|| crate::panicOOM());
.tryAllocZeroed()
.unwrap_or_else(|| panic!("Out of memory"));
} }
fn tryAlloc<'s, T: Sized>(&'s mut self, value: T) -> Option<&'s mut T>; fn tryAlloc<'s, T: Sized>(&'s mut self, value: T) -> Option<&'s mut T>;
fn tryAllocUninitialized<'s, T: Sized>(&'s mut self) -> Option<&'s mut MaybeUninit<T>>; fn tryAllocUninitialized<'s, T: Sized>(&'s mut self) -> Option<&'s mut MaybeUninit<T>>;
fn tryAllocZeroed<'s, T: Sized>(&'s mut self) -> Option<&'s mut MaybeUninit<T>>; fn tryAllocZeroed<'s, T: Sized>(&'s mut self) -> Option<&'s mut MaybeUninit<T>>;
fn dealloc<'s, T: Sized>(&'s mut self, p: &'s mut T);
} }

View File

@ -1,28 +1,23 @@
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
pub unsafe trait TypedHeap<T: Sized> { pub unsafe trait TypedHeap<T: Sized>: Drop {
type Dyn<_T: Sized>: crate::Dyn<_T>; fn alloc<'s>(&'s mut self, value: T) -> &'s mut T {
return self.tryAlloc(value).unwrap_or_else(|| crate::panicOOM());
fn alloc<'s>(&'s mut self, value: T) -> Self::Dyn<T> {
return self
.tryAlloc(value)
.unwrap_or_else(|| panic!("Out of memory"));
} }
fn allocUninitialized<'s>(&'s mut self) -> Self::Dyn<MaybeUninit<T>> { fn allocUninitialized<'s>(&'s mut self) -> &'s mut MaybeUninit<T> {
return self return self
.tryAllocUninitialized() .tryAllocUninitialized()
.unwrap_or_else(|| panic!("Out of memory")); .unwrap_or_else(|| crate::panicOOM());
} }
fn allocZeroed<'s>(&'s mut self) -> Self::Dyn<MaybeUninit<T>> { fn allocZeroed<'s>(&'s mut self) -> &'s mut MaybeUninit<T> {
return self return self.tryAllocZeroed().unwrap_or_else(|| crate::panicOOM());
.tryAllocZeroed()
.unwrap_or_else(|| panic!("Out of memory"));
} }
fn tryAlloc<'s>(&'s mut self, value: T) -> Option<Self::Dyn<T>>; fn tryAlloc<'s>(&'s mut self, value: T) -> Option<&'s mut T>;
fn tryAllocUninitialized<'s>(&'s mut self) -> Option<Self::Dyn<MaybeUninit<T>>>; fn tryAllocUninitialized<'s>(&'s mut self) -> Option<&'s mut MaybeUninit<T>>;
fn tryAllocZeroed<'s>(&'s mut self) -> Option<Self::Dyn<MaybeUninit<T>>>; fn tryAllocZeroed<'s>(&'s mut self) -> Option<&'s mut MaybeUninit<T>>;
fn dealloc<'s>(&'s mut self, p: &'s mut T);
} }