diff --git a/src/dyn.rs b/src/dyn.rs deleted file mode 100644 index a381e5f..0000000 --- a/src/dyn.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub unsafe trait Dyn: Drop + Sized { - fn getRef<'s>(&'s self) -> &'s mut T; - fn free<'s>(&'s mut self); -} - diff --git a/src/general.rs b/src/general.rs index eaa03ce..f7e11c6 100644 --- a/src/general.rs +++ b/src/general.rs @@ -1,27 +1,23 @@ use std::mem::MaybeUninit; pub unsafe trait GeneralPurposeHeap { - type Dyn: crate::Dyn; - - fn alloc<'s, T: Sized>(&'s mut self, value: T) -> Self::Dyn { - return self - .tryAlloc(value) - .unwrap_or_else(|| panic!("Out of memory")); + fn alloc<'s, T: Sized>(&'s mut self, value: T) -> &'s mut T { + return self.tryAlloc(value).unwrap_or_else(|| crate::panicOOM()); } - fn allocUninitialized<'s, T: Sized>(&'s mut self) -> Self::Dyn> { + fn allocUninitialized<'s, T: Sized>(&'s mut self) -> &'s mut MaybeUninit { return self .tryAllocUninitialized() - .unwrap_or_else(|| panic!("Out of memory")); + .unwrap_or_else(|| crate::panicOOM()); } - fn allocZeroed<'s, T: Sized>(&'s mut self) -> Self::Dyn> { + fn allocZeroed<'s, T: Sized>(&'s mut self) -> &'s mut MaybeUninit { return self .tryAllocZeroed() - .unwrap_or_else(|| panic!("Out of memory")); + .unwrap_or_else(|| crate::panicOOM()); } - fn tryAlloc<'s, T: Sized>(&'s mut self, value: T) -> Option>; - fn tryAllocUninitialized<'s, T: Sized>(&'s mut self) -> Option>>; - fn tryAllocZeroed<'s, T: Sized>(&'s mut self) -> Option>>; + 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>; + fn tryAllocZeroed<'s, T: Sized>(&'s mut self) -> Option<&'s mut MaybeUninit>; } diff --git a/src/lib.rs b/src/lib.rs index 09c1390..b09728a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,16 @@ -mod r#dyn; +#[allow(drop_bounds)] + mod general; mod pages; mod stack; mod typed; +pub fn panicOOM() -> ! { + panic!("Out of memory"); +} + pub use pages::Page; pub use pages::PagesManager; -pub use r#dyn::Dyn; pub use stack::StackHeap; pub use typed::TypedHeap; pub use general::GeneralPurposeHeap; \ No newline at end of file diff --git a/src/pages.rs b/src/pages.rs index db72e3d..fbc5038 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -1,12 +1,13 @@ use std::ptr::NonNull; -enum ExtensionResult { +pub enum ExtensionResult { OK, NO_MEMORY, EXTENSION_BUSY, + NOT_SUPPORTED, } -pub unsafe trait Page { +pub unsafe trait Page: Drop + Sized { fn getMemory(&self) -> NonNull<[u8]>; fn releasePage(self); @@ -17,11 +18,11 @@ pub unsafe trait Page { pub unsafe trait PagesManager { 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 .tryOfferPage(minsize) .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; } diff --git a/src/stack.rs b/src/stack.rs index 8b34716..d613afd 100644 --- a/src/stack.rs +++ b/src/stack.rs @@ -1,25 +1,23 @@ use std::mem::MaybeUninit; -pub unsafe trait StackHeap { +pub unsafe trait StackHeap: Drop { fn childScope<'s>(&'s mut self) -> impl StackHeap; fn alloc<'s, T: Sized>(&'s mut self, value: T) -> &'s mut T { - return self - .tryAlloc(value) - .unwrap_or_else(|| panic!("Out of memory")); + return self.tryAlloc(value).unwrap_or_else(|| crate::panicOOM()); } fn allocUninitialized<'s, T: Sized>(&'s mut self) -> &'s mut MaybeUninit { return self .tryAllocUninitialized() - .unwrap_or_else(|| panic!("Out of memory")); + .unwrap_or_else(|| crate::panicOOM()); } fn allocZeroed<'s, T: Sized>(&'s mut self) -> &'s mut MaybeUninit { - return self - .tryAllocZeroed() - .unwrap_or_else(|| panic!("Out of memory")); + return self.tryAllocZeroed().unwrap_or_else(|| crate::panicOOM()); } 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>; fn tryAllocZeroed<'s, T: Sized>(&'s mut self) -> Option<&'s mut MaybeUninit>; + + fn dealloc<'s, T: Sized>(&'s mut self, p: &'s mut T); } diff --git a/src/typed.rs b/src/typed.rs index b9a1c65..7b390c6 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -1,28 +1,23 @@ use std::mem::MaybeUninit; -pub unsafe trait TypedHeap { - type Dyn<_T: Sized>: crate::Dyn<_T>; - - fn alloc<'s>(&'s mut self, value: T) -> Self::Dyn { - return self - .tryAlloc(value) - .unwrap_or_else(|| panic!("Out of memory")); +pub unsafe trait TypedHeap: Drop { + fn alloc<'s>(&'s mut self, value: T) -> &'s mut T { + return self.tryAlloc(value).unwrap_or_else(|| crate::panicOOM()); } - fn allocUninitialized<'s>(&'s mut self) -> Self::Dyn> { + fn allocUninitialized<'s>(&'s mut self) -> &'s mut MaybeUninit { return self .tryAllocUninitialized() - .unwrap_or_else(|| panic!("Out of memory")); + .unwrap_or_else(|| crate::panicOOM()); } - fn allocZeroed<'s>(&'s mut self) -> Self::Dyn> { - return self - .tryAllocZeroed() - .unwrap_or_else(|| panic!("Out of memory")); + fn allocZeroed<'s>(&'s mut self) -> &'s mut MaybeUninit { + return self.tryAllocZeroed().unwrap_or_else(|| crate::panicOOM()); } - fn tryAlloc<'s>(&'s mut self, value: T) -> Option>; - fn tryAllocUninitialized<'s>(&'s mut self) -> Option>>; - fn tryAllocZeroed<'s>(&'s mut self) -> Option>>; + fn tryAlloc<'s>(&'s mut self, value: T) -> Option<&'s mut T>; + fn tryAllocUninitialized<'s>(&'s mut self) -> Option<&'s mut MaybeUninit>; + fn tryAllocZeroed<'s>(&'s mut self) -> Option<&'s mut MaybeUninit>; + + fn dealloc<'s>(&'s mut self, p: &'s mut T); } -