From 6e293d029bd60f5565bb18629e3baf2d049e53cf Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Wed, 24 Aug 2022 19:29:00 -0700 Subject: *cargo fmt --- src/mem/alloc.rs | 360 ++++++++++++++++++++++++++----------------------------- src/mem/types.rs | 50 ++++---- 2 files changed, 195 insertions(+), 215 deletions(-) (limited to 'src/mem') diff --git a/src/mem/alloc.rs b/src/mem/alloc.rs index a98e680..a388c85 100644 --- a/src/mem/alloc.rs +++ b/src/mem/alloc.rs @@ -2,20 +2,20 @@ //! //! Provides the Global allocator and methods //! to create special purpose allocators. -use alloc::alloc::{GlobalAlloc,Layout}; -use crate::sync::NullLock; +use super::types::*; use crate::sync::interface::Mutex; +use crate::sync::NullLock; use crate::vprintln; -use super::types::*; -use os_pic::util::node::Node; -use os_pic::util::lifo_queue::LifoQueue; +use alloc::alloc::{GlobalAlloc, Layout}; use os_pic::init_lifo_queue; +use os_pic::util::lifo_queue::LifoQueue; +use os_pic::util::node::Node; use os_pic::util::queue::Queue; /// # Grand Allocator /// /// The structure that uses different sized pools and allocates memory chunks -pub struct GrandAllocator { } +pub struct GrandAllocator {} /// # The number of elements of each size const GRAND_ALLOC_SIZE: usize = 64; @@ -25,160 +25,140 @@ init_lifo_queue!(U16_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u16); init_lifo_queue!(U32_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u32); init_lifo_queue!(U64_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u64); init_lifo_queue!(U128_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u128); -init_lifo_queue!(U256_GRAND_ALLOC, GRAND_ALLOC_SIZE, {U256::new()}, U256); -init_lifo_queue!(U512_GRAND_ALLOC, GRAND_ALLOC_SIZE, {U512::new()}, U512); -init_lifo_queue!(U1024_GRAND_ALLOC, GRAND_ALLOC_SIZE, {U1024::new()}, U1024); -init_lifo_queue!(U2048_GRAND_ALLOC, GRAND_ALLOC_SIZE, {U2048::new()}, U2048); -init_lifo_queue!(U4096_GRAND_ALLOC, GRAND_ALLOC_SIZE, {U4096::new()}, U4096); +init_lifo_queue!(U256_GRAND_ALLOC, GRAND_ALLOC_SIZE, { U256::new() }, U256); +init_lifo_queue!(U512_GRAND_ALLOC, GRAND_ALLOC_SIZE, { U512::new() }, U512); +init_lifo_queue!(U1024_GRAND_ALLOC, GRAND_ALLOC_SIZE, { U1024::new() }, U1024); +init_lifo_queue!(U2048_GRAND_ALLOC, GRAND_ALLOC_SIZE, { U2048::new() }, U2048); +init_lifo_queue!(U4096_GRAND_ALLOC, GRAND_ALLOC_SIZE, { U4096::new() }, U4096); impl GrandAllocator { - pub fn init(&self) -> Result<(), &'static str> { - vprintln!("GA: \x1b[93mInit U8 Pool\x1b[0m"); - U8_GRAND_ALLOC.init(); - vprintln!("GA: \x1b[93mInit U16 Pool\x1b[0m"); - U16_GRAND_ALLOC.init(); - vprintln!("GA: \x1b[93mInit U32 Pool\x1b[0m"); - U32_GRAND_ALLOC.init(); - vprintln!("GA: \x1b[93mInit U64 Pool\x1b[0m"); - U64_GRAND_ALLOC.init(); - vprintln!("GA: \x1b[93mInit U128 Pool\x1b[0m"); - U128_GRAND_ALLOC.init(); - vprintln!("GA: \x1b[93mInit U256 Pool\x1b[0m"); - U256_GRAND_ALLOC.init(); - vprintln!("GA: \x1b[93mInit U512 Pool\x1b[0m"); - U512_GRAND_ALLOC.init(); - vprintln!("GA: \x1b[93mInit U1024 Pool\x1b[0m"); - U1024_GRAND_ALLOC.init(); - vprintln!("GA: \x1b[93mInit U2048 Pool\x1b[0m"); - U2048_GRAND_ALLOC.init(); - vprintln!("GA: \x1b[93mInit U4096 Pool\x1b[0m"); - U4096_GRAND_ALLOC.init(); - vprintln!("GA: \x1b[94mPools Initialized!\x1b[0m"); - Ok(()) - } + pub fn init(&self) -> Result<(), &'static str> { + vprintln!("GA: \x1b[93mInit U8 Pool\x1b[0m"); + U8_GRAND_ALLOC.init(); + vprintln!("GA: \x1b[93mInit U16 Pool\x1b[0m"); + U16_GRAND_ALLOC.init(); + vprintln!("GA: \x1b[93mInit U32 Pool\x1b[0m"); + U32_GRAND_ALLOC.init(); + vprintln!("GA: \x1b[93mInit U64 Pool\x1b[0m"); + U64_GRAND_ALLOC.init(); + vprintln!("GA: \x1b[93mInit U128 Pool\x1b[0m"); + U128_GRAND_ALLOC.init(); + vprintln!("GA: \x1b[93mInit U256 Pool\x1b[0m"); + U256_GRAND_ALLOC.init(); + vprintln!("GA: \x1b[93mInit U512 Pool\x1b[0m"); + U512_GRAND_ALLOC.init(); + vprintln!("GA: \x1b[93mInit U1024 Pool\x1b[0m"); + U1024_GRAND_ALLOC.init(); + vprintln!("GA: \x1b[93mInit U2048 Pool\x1b[0m"); + U2048_GRAND_ALLOC.init(); + vprintln!("GA: \x1b[93mInit U4096 Pool\x1b[0m"); + U4096_GRAND_ALLOC.init(); + vprintln!("GA: \x1b[94mPools Initialized!\x1b[0m"); + Ok(()) + } } unsafe impl GlobalAlloc for GrandAllocator { - /// # Allocator - /// - /// Allocate the fixed size chunks - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - vprintln!("GA: Allocating chunk of size {}!", layout.size()); - match layout.size() { - 1 => { - match U8_GRAND_ALLOC.pop() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - return (*elem).ptr(); - } - } - } - 2 => { - match U16_GRAND_ALLOC.pop() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - return (*elem).ptr(); - } - } - } - 3..=4 => { - match U32_GRAND_ALLOC.pop() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - return (*elem).ptr(); - } - } - } - 5..=8 => { - match U64_GRAND_ALLOC.pop() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - return (*elem).ptr(); - } - } - } - 9..=16 => { - match U128_GRAND_ALLOC.pop() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - return (*elem).ptr(); - } - } - } - 17..=32 => { - match U256_GRAND_ALLOC.pop() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - return (*elem).ptr(); - } - } - } - 33..=64 => { - match U512_GRAND_ALLOC.pop() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - return (*elem).ptr(); - } - } - } - 65..=128 => { - match U1024_GRAND_ALLOC.pop() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - return (*elem).ptr(); - } - } - } - 129..=256 => { - match U2048_GRAND_ALLOC.pop() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - return (*elem).ptr(); - } - } - } - 257..=512 => { - match U4096_GRAND_ALLOC.pop() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - return (*elem).ptr(); - } - } - } - _ => { - panic!("No allocators for size {}!", layout.size()); - } - } - } + /// # Allocator + /// + /// Allocate the fixed size chunks + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + vprintln!("GA: Allocating chunk of size {}!", layout.size()); + match layout.size() { + 1 => match U8_GRAND_ALLOC.pop() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).ptr(); + } + }, + 2 => match U16_GRAND_ALLOC.pop() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).ptr(); + } + }, + 3..=4 => match U32_GRAND_ALLOC.pop() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).ptr(); + } + }, + 5..=8 => match U64_GRAND_ALLOC.pop() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).ptr(); + } + }, + 9..=16 => match U128_GRAND_ALLOC.pop() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).ptr(); + } + }, + 17..=32 => match U256_GRAND_ALLOC.pop() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).ptr(); + } + }, + 33..=64 => match U512_GRAND_ALLOC.pop() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).ptr(); + } + }, + 65..=128 => match U1024_GRAND_ALLOC.pop() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).ptr(); + } + }, + 129..=256 => match U2048_GRAND_ALLOC.pop() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).ptr(); + } + }, + 257..=512 => match U4096_GRAND_ALLOC.pop() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return (*elem).ptr(); + } + }, + _ => { + panic!("No allocators for size {}!", layout.size()); + } + } + } - /// # Deallocate - /// - /// Deallocate the fixed size chunks by searching for them - unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { - vprintln!("GA: Deallocating chunk of size {}!", layout.size()); - match layout.size() { - 1 => { - U8_GRAND_ALLOC.inner.lock(|pool| { + /// # Deallocate + /// + /// Deallocate the fixed size chunks by searching for them + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + vprintln!("GA: Deallocating chunk of size {}!", layout.size()); + match layout.size() { + 1 => { + U8_GRAND_ALLOC.inner.lock(|pool| { let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize); let diff: usize = (ptr as usize) - (pool[1].ptr() as usize); let index: usize = diff/spacing; @@ -187,9 +167,9 @@ unsafe impl GlobalAlloc for GrandAllocator { U8_GRAND_ALLOC.push(&mut pool[index+1]); vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); - } - 2 => { - U16_GRAND_ALLOC.inner.lock(|pool| { + } + 2 => { + U16_GRAND_ALLOC.inner.lock(|pool| { let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize); let diff: usize = (ptr as usize) - (pool[1].ptr() as usize); let index: usize = diff/spacing; @@ -198,9 +178,9 @@ unsafe impl GlobalAlloc for GrandAllocator { U16_GRAND_ALLOC.push(&mut pool[index+1]); vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); - } - 3..=4 => { - U32_GRAND_ALLOC.inner.lock(|pool| { + } + 3..=4 => { + U32_GRAND_ALLOC.inner.lock(|pool| { let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize); let diff: usize = (ptr as usize) - (pool[1].ptr() as usize); let index: usize = diff/spacing; @@ -209,9 +189,9 @@ unsafe impl GlobalAlloc for GrandAllocator { U32_GRAND_ALLOC.push(&mut pool[index+1]); vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); - } - 5..=8 => { - U64_GRAND_ALLOC.inner.lock(|pool| { + } + 5..=8 => { + U64_GRAND_ALLOC.inner.lock(|pool| { let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize); let diff: usize = (ptr as usize) - (pool[1].ptr() as usize); let index: usize = diff/spacing; @@ -220,9 +200,9 @@ unsafe impl GlobalAlloc for GrandAllocator { U64_GRAND_ALLOC.push(&mut pool[index+1]); vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); - } - 9..=16 => { - U128_GRAND_ALLOC.inner.lock(|pool| { + } + 9..=16 => { + U128_GRAND_ALLOC.inner.lock(|pool| { let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize); let diff: usize = (ptr as usize) - (pool[1].ptr() as usize); let index: usize = diff/spacing; @@ -231,9 +211,9 @@ unsafe impl GlobalAlloc for GrandAllocator { U128_GRAND_ALLOC.push(&mut pool[index+1]); vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); - } - 17..=32 => { - U256_GRAND_ALLOC.inner.lock(|pool| { + } + 17..=32 => { + U256_GRAND_ALLOC.inner.lock(|pool| { let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize); let diff: usize = (ptr as usize) - (pool[1].ptr() as usize); let index: usize = diff/spacing; @@ -242,9 +222,9 @@ unsafe impl GlobalAlloc for GrandAllocator { U256_GRAND_ALLOC.push(&mut pool[index+1]); vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); - } - 33..=64 => { - U512_GRAND_ALLOC.inner.lock(|pool| { + } + 33..=64 => { + U512_GRAND_ALLOC.inner.lock(|pool| { let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize); let diff: usize = (ptr as usize) - (pool[1].ptr() as usize); let index: usize = diff/spacing; @@ -253,9 +233,9 @@ unsafe impl GlobalAlloc for GrandAllocator { U512_GRAND_ALLOC.push(&mut pool[index+1]); vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); - } - 65..=128 => { - U1024_GRAND_ALLOC.inner.lock(|pool| { + } + 65..=128 => { + U1024_GRAND_ALLOC.inner.lock(|pool| { let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize); let diff: usize = (ptr as usize) - (pool[1].ptr() as usize); let index: usize = diff/spacing; @@ -264,9 +244,9 @@ unsafe impl GlobalAlloc for GrandAllocator { U1024_GRAND_ALLOC.push(&mut pool[index+1]); vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); - } - 129..=256 => { - U2048_GRAND_ALLOC.inner.lock(|pool| { + } + 129..=256 => { + U2048_GRAND_ALLOC.inner.lock(|pool| { let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize); let diff: usize = (ptr as usize) - (pool[1].ptr() as usize); let index: usize = diff/spacing; @@ -275,9 +255,9 @@ unsafe impl GlobalAlloc for GrandAllocator { U2048_GRAND_ALLOC.push(&mut pool[index+1]); vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); - } - 257..=512 => { - U4096_GRAND_ALLOC.inner.lock(|pool| { + } + 257..=512 => { + U4096_GRAND_ALLOC.inner.lock(|pool| { let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize); let diff: usize = (ptr as usize) - (pool[1].ptr() as usize); let index: usize = diff/spacing; @@ -286,24 +266,24 @@ unsafe impl GlobalAlloc for GrandAllocator { U4096_GRAND_ALLOC.push(&mut pool[index+1]); vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); - } - _ => { - panic!("No deallocators for size {}!", layout.size()); - } - } - } + } + _ => { + panic!("No deallocators for size {}!", layout.size()); + } + } + } } /// # Grand Allocator /// /// The allocator of allocators. It hands out fixed sized memory chunks. #[global_allocator] -pub static ALLOCATOR: GrandAllocator = GrandAllocator{}; +pub static ALLOCATOR: GrandAllocator = GrandAllocator {}; /// # Global Allocator /// /// Returns a borrow for the Global Allocator pub fn allocator() -> &'static crate::mem::alloc::GrandAllocator { - vprintln!("AL: Getting global allocator!"); - &crate::mem::alloc::ALLOCATOR + vprintln!("AL: Getting global allocator!"); + &crate::mem::alloc::ALLOCATOR } diff --git a/src/mem/types.rs b/src/mem/types.rs index 04522bc..ed22132 100644 --- a/src/mem/types.rs +++ b/src/mem/types.rs @@ -1,54 +1,54 @@ /// # u256 struct /// /// 256 bit size field -#[derive(Copy,Clone)] -pub struct U256(u128,u128); +#[derive(Copy, Clone)] +pub struct U256(u128, u128); impl U256 { - pub const fn new() -> Self { - U256(0,0) - } + pub const fn new() -> Self { + U256(0, 0) + } } /// # u512 struct /// /// 512 bit size field -#[derive(Copy,Clone)] -pub struct U512(U256,U256); +#[derive(Copy, Clone)] +pub struct U512(U256, U256); impl U512 { - pub const fn new() -> Self { - U512(U256::new(), U256::new()) - } + pub const fn new() -> Self { + U512(U256::new(), U256::new()) + } } /// # u1024 struct /// /// 1024 bit size field -#[derive(Copy,Clone)] -pub struct U1024(U512,U512); +#[derive(Copy, Clone)] +pub struct U1024(U512, U512); impl U1024 { - pub const fn new() -> Self { - U1024(U512::new(), U512::new()) - } + pub const fn new() -> Self { + U1024(U512::new(), U512::new()) + } } /// # u2048 struct /// /// 2048 bit size field -#[derive(Copy,Clone)] -pub struct U2048(U1024,U1024); +#[derive(Copy, Clone)] +pub struct U2048(U1024, U1024); impl U2048 { - pub const fn new() -> Self { - U2048(U1024::new(), U1024::new()) - } + pub const fn new() -> Self { + U2048(U1024::new(), U1024::new()) + } } /// # u4096 struct /// /// 4096 bit size field -#[derive(Copy,Clone)] -pub struct U4096(U2048,U2048); +#[derive(Copy, Clone)] +pub struct U4096(U2048, U2048); impl U4096 { - pub const fn new() -> Self { - U4096(U2048::new(), U2048::new()) - } + pub const fn new() -> Self { + U4096(U2048::new(), U2048::new()) + } } -- cgit v1.2.1