aboutsummaryrefslogtreecommitdiff
path: root/src/alloc.rs
blob: fd22c67ee74b83eac119871d1061a7578d6f5e9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use crate::sync::NullLock;
use crate::sync::interface::Mutex;

const CHAR_COUNT: usize = 20;
const CHAR_POOL_SIZE: usize = CHAR_COUNT + 2;

#[derive(Copy,Clone)]
pub struct CharCell {
	pub data: char,
	next: Option<*mut CharCell>,
}

impl CharCell {
	pub const fn new() -> Self {
		Self {
			data: '\0',
			next: None,
		}
	}
}

use core::fmt::{Debug,Formatter,Result};
impl Debug for CharCell {
	fn fmt(&self, f: &mut Formatter<'_>) -> Result {
		write!(f, "{}", self.data)
	}
}

unsafe impl Send for CharCell {}

pub struct CharAllocator(NullLock<[CharCell; CHAR_POOL_SIZE]>);

impl CharAllocator {
	pub const fn new() -> Self {
		Self(NullLock::new([CharCell::new(); CHAR_POOL_SIZE]))
	}

	pub fn init(&self) {
		self.0.lock(|char_pool| {
			for idx in 2..CHAR_POOL_SIZE {
				if idx != CHAR_POOL_SIZE - 1 {
					char_pool[idx].next = Some(&mut char_pool[idx+1] as *mut CharCell);
				} else {
					char_pool[idx].next = None;
				}
			}
			char_pool[0].next = Some(&mut char_pool[2] as *mut CharCell);
			char_pool[1].next = Some(&mut char_pool[CHAR_POOL_SIZE-1] as *mut CharCell);
		});
	}

	pub fn alloc(&self) -> Option<&mut CharCell> {
		return self.0.lock(|char_pool| {
			if let Some(char_cell) = char_pool[0].next {
				char_pool[0].next = unsafe{(*char_cell).next};
				unsafe {
					(*char_cell).next = None;
				}
				return Some(unsafe{&mut *char_cell as &mut CharCell});
			} else {
				return None;
			}
		});
	}

	pub fn free(&self, cell: &mut CharCell) {
		self.0.lock(|char_pool| {
			cell.next = None;
			match char_pool[1].next {
				None => {
					char_pool[0].next = Some(cell as *mut CharCell);
				}
				Some(ocell) => {
					unsafe {
						(*ocell).next = Some(cell as *mut CharCell);
					}
				}
			}
			char_pool[1].next = Some(cell as *mut CharCell);
		});
	}
}

impl Debug for CharAllocator {
	fn fmt(&self, f: &mut Formatter<'_>) -> Result {
		self.0.lock(|pool| {
			write!(f, "{:?}", pool)
		})
	}
}

#[link_section = ".data.alloc"]
pub static CHAR_ALLOCATOR: CharAllocator = CharAllocator::new();