Play around with an arena allocator for direntries.

This commit is contained in:
Bas Wiel, van de 2024-05-09 13:50:46 +02:00
parent 896f37800d
commit 058b036286
2 changed files with 66 additions and 0 deletions

View File

@ -8,6 +8,7 @@ use crate::os::OperatingSystem;
use crate::Disk;
pub mod allocationtable;
pub mod arena;
pub mod attributes;
pub mod cluster;
pub mod direntry;

65
src/fat/arena.rs Normal file
View File

@ -0,0 +1,65 @@
struct Node<T> {
value: T,
children: Vec<NodeId>,
parent: Option<NodeId>,
}
struct Arena<T> {
nodes: Vec<Node<T>>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct NodeId(usize);
impl<T> Arena<T> {
fn new() -> Self {
Arena { nodes: Vec::new() }
}
fn new_node(&mut self, value: T) -> NodeId {
let node_id = NodeId(self.nodes.len());
self.nodes.push(Node {
value,
children: Vec::new(),
parent: None,
});
node_id
}
fn insert_node(&mut self, node: Node<T>) -> NodeId {
let node_id = NodeId(self.nodes.len());
self.nodes.push(node);
node_id
}
fn add_child(&mut self, parent_id: NodeId, child_id: NodeId) {
if let Some(node) = self.nodes.get_mut(parent_id.0) {
node.children.push(child_id);
}
if let Some(node) = self.nodes.get_mut(child_id.0) {
node.parent = Some(parent_id);
}
}
fn get_node_mut(&mut self, node_id: NodeId) -> Option<&mut Node<T>> {
self.nodes.get_mut(node_id.0)
}
fn iter_mut(&mut self) -> std::slice::IterMut<Node<T>> {
self.nodes.iter_mut()
}
fn traverse_and_modify(&mut self, start_id: NodeId, modify_fn: &mut dyn FnMut(&mut Node<T>)) {
let mut stack = vec![start_id];
while let Some(node_id) = stack.pop() {
if let Some(node) = self.get_node_mut(node_id) {
modify_fn(node);
for &child_id in &node.children {
stack.push(child_id);
}
}
}
}
}