aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/kernel.rs12
-rw-r--r--src/mem/alloc.rs141
2 files changed, 73 insertions, 80 deletions
diff --git a/src/kernel.rs b/src/kernel.rs
index 19c4e5f..e3e9166 100644
--- a/src/kernel.rs
+++ b/src/kernel.rs
@@ -63,17 +63,5 @@ fn kernel_main() -> ! {
}
}
println!("I should be able to print {} here!", 5);
- {
- let a: Box<u8> = Box::new(5);
- println!("{:?}", a);
- let b: Box<u8> = Box::new(7);
- println!("{:?}", b);
- }
- {
- let a: Box<u8> = Box::new(8);
- println!("{:?}", a);
- let b: Box<u16> = Box::new(9);
- println!("{:?}", b);
- }
loop { }
}
diff --git a/src/mem/alloc.rs b/src/mem/alloc.rs
index 0b02fbd..927c00d 100644
--- a/src/mem/alloc.rs
+++ b/src/mem/alloc.rs
@@ -192,44 +192,46 @@ unsafe impl GlobalAlloc for GrandAllocator {
}
}
}
- /*
2 => {
- U16_GRAND_ALLOC.inner.lock(|pool| {
- match pool.alloc() {
- None => {
- panic!("No cells to allocate!");
- }
- Some(elem) => {
- (*elem).inner() as *mut u8;
- }
+ match U16_GRAND_ALLOC.alloc() {
+ None => {
+ panic!("No cells to allocate!");
}
- })
+ Some(elem) => {
+ return &mut (*(*elem).inner() as u8) as *mut u8;
+ }
+ }
}
4 => {
- U32_GRAND_ALLOC.inner.lock(|pool| {
- match pool.alloc() {
- None => {
- panic!("No cells to allocate!");
- }
- Some(elem) => {
- (*elem).inner() as *mut u8;
- }
+ match U32_GRAND_ALLOC.alloc() {
+ None => {
+ panic!("No cells to allocate!");
+ }
+ Some(elem) => {
+ return &mut (*(*elem).inner() as u8) as *mut u8;
}
- })
+ }
}
8 => {
- U64_GRAND_ALLOC.inner.lock(|pool| {
- match pool.alloc() {
- None => {
- panic!("No cells to allocate!");
- }
- Some(elem) => {
- (*elem).inner() as *mut u8;
- }
+ match U64_GRAND_ALLOC.alloc() {
+ None => {
+ panic!("No cells to allocate!");
}
- })
+ Some(elem) => {
+ return &mut (*(*elem).inner() as u8) as *mut u8;
+ }
+ }
+ }
+ 16 => {
+ match U128_GRAND_ALLOC.alloc() {
+ None => {
+ panic!("No cells to allocate!");
+ }
+ Some(elem) => {
+ return &mut (*(*elem).inner() as u8) as *mut u8;
+ }
+ }
}
- */
_ => {
panic!("No allocators for size {}!", layout.size());
}
@@ -246,56 +248,59 @@ unsafe impl GlobalAlloc for GrandAllocator {
return;
}
}
+ panic!("Didn't deallocate!");
});
}
- _ => {
- panic!("No deallocators for size {}!", layout.size());
+ 2 => {
+ U16_GRAND_ALLOC.inner.lock(|pool| {
+ for idx in 2..pool.len() {
+ if &mut (*pool[idx].inner() as u8) as *mut u8 == ptr {
+ U16_GRAND_ALLOC.free(&mut pool[idx]);
+ return;
+ }
+ }
+ panic!("Didn't deallocate!");
+ });
}
- }
- }
-}
-
-#[global_allocator]
-pub static ALLOCATOR: GrandAllocator = GrandAllocator{};
-/*
-unsafe impl<const COUNT: usize> GlobalAlloc for NullLock<QueueAllocator<'_,u8,COUNT>> {
- unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
- match layout.size() {
- 1 => {
- self.lock(|qa| {
- match qa.alloc() {
- None => {
- panic!("No data to allocate!");
+ 4 => {
+ U32_GRAND_ALLOC.inner.lock(|pool| {
+ for idx in 2..pool.len() {
+ if &mut (*pool[idx].inner() as u8) as *mut u8 == ptr {
+ U32_GRAND_ALLOC.free(&mut pool[idx]);
+ return;
+ }
+ }
+ panic!("Didn't deallocate!");
+ });
+ }
+ 8 => {
+ U64_GRAND_ALLOC.inner.lock(|pool| {
+ for idx in 2..pool.len() {
+ if &mut (*pool[idx].inner() as u8) as *mut u8 == ptr {
+ U64_GRAND_ALLOC.free(&mut pool[idx]);
+ return;
}
- Some(elem) => {
- return (*elem).inner() as *mut u8;
+ }
+ panic!("Didn't deallocate!");
+ });
+ }
+ 16 => {
+ U128_GRAND_ALLOC.inner.lock(|pool| {
+ for idx in 2..pool.len() {
+ if &mut (*pool[idx].inner() as u8) as *mut u8 == ptr {
+ U128_GRAND_ALLOC.free(&mut pool[idx]);
+ return;
}
}
- })
+ panic!("Didn't deallocate!");
+ });
}
_ => {
- panic!("No allocators for size {}!", layout.size());
+ panic!("No deallocators for size {}!", layout.size());
}
}
}
-
- unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
- self.lock(|qa| {
- qa.inner.lock(|pool| {
- for idx in 2..COUNT {
- if pool[idx].inner() as *mut u8 == _ptr {
- qa.free(&mut pool[idx]);
- return;
- }
- }
- });
- });
- }
}
-const GLOBAL_ALLOCATOR_SIZE: usize = 100;
-
-// TODO: Add other allocation sizes
#[global_allocator]
-pub static ALLOCATOR: NullLock<QueueAllocator<'static,u8,{GLOBAL_ALLOCATOR_SIZE+2}>> = NullLock::new(QueueAllocator::<u8,{GLOBAL_ALLOCATOR_SIZE+2}>{inner: NullLock::new([QueueItem{data: 0, next: None}; {GLOBAL_ALLOCATOR_SIZE+2}])});
-*/
+pub static ALLOCATOR: GrandAllocator = GrandAllocator{};