-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1dc5fb2
commit f1dba3f
Showing
3 changed files
with
73 additions
and
22 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use core::{ | ||
alloc::{GlobalAlloc, Layout}, | ||
mem::transmute, | ||
}; | ||
|
||
static mut NODES: Option<&mut LinkedAllocatorNode> = None; | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: LinkedListAllocator = LinkedListAllocator {}; | ||
|
||
pub struct LinkedListAllocator {} | ||
|
||
unsafe impl<'a> GlobalAlloc for LinkedListAllocator { | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
if NODES.is_none() { | ||
panic!("LinkedListAllocator has not been initialized yet"); | ||
} | ||
|
||
let node = NODES.as_deref_mut().unwrap(); | ||
|
||
if layout.size() > node.size { | ||
panic!("Out of heap memory"); | ||
} | ||
|
||
node.size = node.size - layout.size(); | ||
let ptr = node as *mut _ as usize + node.size; | ||
|
||
ptr as *mut u8 | ||
} | ||
|
||
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { | ||
// unimplemented!("Deallocate") | ||
} | ||
} | ||
|
||
pub unsafe fn init(node: &'static mut LinkedAllocatorNode) { | ||
if NODES.is_some() { | ||
panic!("LinkedListAllocator is already initialized"); | ||
} | ||
NODES = Some(node); | ||
} | ||
|
||
pub struct LinkedAllocatorNode<'a> { | ||
pub(crate) size: usize, | ||
pub(crate) next: Option<&'a mut Self>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters