aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-08-20 12:22:19 -0700
committerChristian Cunningham <cc@localhost>2022-08-20 12:22:19 -0700
commitd6b83355ac0cdd266d11973bbc88ab10417e3e68 (patch)
tree71afaa90f0f442dc047d2fb4615d74ee3289ec6d
parentce4585a574c638f4dfa0b482074d4c3134cab2f9 (diff)
Inclusive ranges for allocations
-rw-r--r--src/mem/alloc.rs64
1 files changed, 44 insertions, 20 deletions
diff --git a/src/mem/alloc.rs b/src/mem/alloc.rs
index ed73e72..a361cf4 100644
--- a/src/mem/alloc.rs
+++ b/src/mem/alloc.rs
@@ -1,4 +1,8 @@
//! # Allocate
+use alloc::alloc::{GlobalAlloc,Layout};
+use crate::sync::NullLock;
+use crate::sync::interface::Mutex;
+use core::fmt::{Debug,Formatter,Result};
/// # Initialize Queue
/// - Name: Symbol name
@@ -16,10 +20,6 @@ macro_rules! init_queue {
};
}
-use crate::sync::NullLock;
-use crate::sync::interface::Mutex;
-use core::fmt::{Debug,Formatter,Result};
-
#[derive(Copy,Clone)]
/// # Queue Item
///
@@ -153,20 +153,22 @@ impl<T: Debug,const COUNT: usize> Debug for QueueAllocator<'_,T,COUNT> {
}
}
-/// Number of U64s to hand out
-const U64_POOL_SIZE: usize = 2;
-
-init_queue!(U64_QUEUE_ALLOCATOR, U64_POOL_SIZE, 0, u64);
+/// # u256 struct
+///
+/// 256 bit size field
+#[derive(Copy,Clone)]
+pub struct U256(u128,u128);
-
-extern crate alloc;
-use alloc::alloc::{GlobalAlloc,Layout};
-
+/// # Grand Allocator
+///
+/// The structure that uses different sized pools and allocates memory chunks
pub struct GrandAllocator { }
+
+/// # The number of elements of each size
const GRAND_ALLOC_SIZE: usize = 64;
init_queue!(U8_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u8);
@@ -174,6 +176,7 @@ init_queue!(U16_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u16);
init_queue!(U32_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u32);
init_queue!(U64_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u64);
init_queue!(U128_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u128);
+init_queue!(U256_GRAND_ALLOC, GRAND_ALLOC_SIZE, {U256(0,0)}, U256);
impl GrandAllocator {
pub fn init(&self) {
@@ -182,6 +185,7 @@ impl GrandAllocator {
U32_GRAND_ALLOC.init();
U64_GRAND_ALLOC.init();
U128_GRAND_ALLOC.init();
+ U256_GRAND_ALLOC.init();
}
}
@@ -190,7 +194,6 @@ unsafe impl GlobalAlloc for GrandAllocator {
///
/// Allocate the fixed size chunks
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
- crate::println!("Size: {}", layout.size());
match layout.size() {
1 => {
match U8_GRAND_ALLOC.alloc() {
@@ -212,7 +215,7 @@ unsafe impl GlobalAlloc for GrandAllocator {
}
}
}
- 3..4 => {
+ 3..=4 => {
match U32_GRAND_ALLOC.alloc() {
None => {
panic!("No cells to allocate!");
@@ -222,7 +225,7 @@ unsafe impl GlobalAlloc for GrandAllocator {
}
}
}
- 5..8 => {
+ 5..=8 => {
match U64_GRAND_ALLOC.alloc() {
None => {
panic!("No cells to allocate!");
@@ -232,7 +235,7 @@ unsafe impl GlobalAlloc for GrandAllocator {
}
}
}
- 9..16 => {
+ 9..=16 => {
match U128_GRAND_ALLOC.alloc() {
None => {
panic!("No cells to allocate!");
@@ -242,6 +245,16 @@ unsafe impl GlobalAlloc for GrandAllocator {
}
}
}
+ 17..=32 => {
+ match U256_GRAND_ALLOC.alloc() {
+ None => {
+ panic!("No cells to allocate!");
+ }
+ Some(elem) => {
+ return (*elem).ptr();
+ }
+ }
+ }
_ => {
panic!("No allocators for size {}!", layout.size());
}
@@ -256,7 +269,7 @@ unsafe impl GlobalAlloc for GrandAllocator {
1 => {
U8_GRAND_ALLOC.inner.lock(|pool| {
for idx in 2..pool.len() {
- if pool[idx].inner() as *mut u8 == ptr {
+ if pool[idx].ptr() == ptr {
U8_GRAND_ALLOC.free(&mut pool[idx]);
return;
}
@@ -275,7 +288,7 @@ unsafe impl GlobalAlloc for GrandAllocator {
panic!("Didn't deallocate!");
});
}
- 3..4 => {
+ 3..=4 => {
U32_GRAND_ALLOC.inner.lock(|pool| {
for idx in 2..pool.len() {
if pool[idx].ptr() == ptr {
@@ -286,7 +299,7 @@ unsafe impl GlobalAlloc for GrandAllocator {
panic!("Didn't deallocate!");
});
}
- 5..8 => {
+ 5..=8 => {
U64_GRAND_ALLOC.inner.lock(|pool| {
for idx in 2..pool.len() {
if pool[idx].ptr() == ptr {
@@ -297,7 +310,7 @@ unsafe impl GlobalAlloc for GrandAllocator {
panic!("Didn't deallocate!");
});
}
- 9..16 => {
+ 9..=16 => {
U128_GRAND_ALLOC.inner.lock(|pool| {
for idx in 2..pool.len() {
if pool[idx].ptr() == ptr {
@@ -308,6 +321,17 @@ unsafe impl GlobalAlloc for GrandAllocator {
panic!("Didn't deallocate!");
});
}
+ 17..=32 => {
+ U256_GRAND_ALLOC.inner.lock(|pool| {
+ for idx in 2..pool.len() {
+ if pool[idx].ptr() == ptr {
+ U256_GRAND_ALLOC.free(&mut pool[idx]);
+ return;
+ }
+ }
+ panic!("Didn't deallocate!");
+ });
+ }
_ => {
panic!("No deallocators for size {}!", layout.size());
}