-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move allocator into separate crate, add test stage
- Loading branch information
1 parent
c8d5cf9
commit c9b7315
Showing
28 changed files
with
144 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[unstable] | ||
build-std-features = ["compiler-builtins-mem"] | ||
build-std = ["core", "alloc", "compiler_builtins"] | ||
build-std = ["core", "alloc", "compiler_builtins"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,5 @@ | ||
[package] | ||
name = "kernel" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[dependencies] | ||
bitflags = "2.4.0" | ||
lazy_static = { version = "1.4.0", features = ["spin_no_std"] } | ||
multiboot2 = { version = "0.19.0", default-features = false } | ||
spin = "0.9.8" | ||
uart_16550 = "0.3.0" | ||
x86_64 = "0.14.11" | ||
[workspace] | ||
resolver = "2" | ||
members = [ | ||
"crates/*" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "allocator" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#![cfg_attr(not(test), no_std)] | ||
|
||
pub use self::allocator::LinkedListAllocator; | ||
pub use self::linked_list::{LinkedAllocatorIter, LinkedAllocatorNode}; | ||
|
||
pub(crate) mod allocator; | ||
pub(crate) mod linked_list; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use allocator::*; | ||
use core::alloc::{GlobalAlloc, Layout}; | ||
|
||
pub unsafe fn print_nodes(alloc: &LinkedListAllocator) { | ||
if alloc.nodes.is_none() { | ||
panic!("LinkedListAllocator has not been initialized yet"); | ||
} | ||
|
||
let start_node = &mut *alloc.nodes.unwrap(); | ||
for node in start_node.as_iter() { | ||
println!("0x{:x}: Node: {node:?}", node as *const _ as usize); | ||
} | ||
} | ||
|
||
fn create_node(ptr: usize, size: usize) -> *mut LinkedAllocatorNode { | ||
let node: &mut LinkedAllocatorNode = unsafe { std::mem::transmute(ptr) }; | ||
node.next = None; | ||
node.size = size - size_of::<LinkedAllocatorNode>(); | ||
node.free = true; | ||
node as *mut _ | ||
} | ||
|
||
#[test] | ||
fn should_initialize_allocator_once() { | ||
const SIZE: usize = 256; | ||
let mut buf = [0u8; SIZE]; | ||
let node = unsafe { &mut *create_node(&mut buf[0] as *const _ as usize, SIZE) }; | ||
|
||
let mut allocator = LinkedListAllocator::new(); | ||
allocator.init(node); | ||
|
||
unsafe { | ||
let ptr = allocator.alloc(Layout::from_size_align_unchecked(32, 1)); | ||
allocator.dealloc(ptr, Layout::from_size_align_unchecked(32, 1)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn should_merge_with_previous_node() { | ||
const SIZE: usize = 256; | ||
let mut buf = [0u8; SIZE]; | ||
let node = create_node(&mut buf[0] as *const _ as usize, SIZE); | ||
|
||
let init_node = unsafe { &mut *node }; | ||
let node = unsafe { &mut *node }; | ||
|
||
let mut allocator = LinkedListAllocator::new(); | ||
allocator.init(init_node); | ||
|
||
assert!(node.free); | ||
assert_eq!(node.size, 256 - size_of::<LinkedAllocatorNode>()); | ||
assert_eq!(node.next, None); | ||
|
||
unsafe { | ||
let ptr = allocator.alloc(Layout::from_size_align_unchecked(32, 1)); | ||
allocator.dealloc(ptr, Layout::from_size_align_unchecked(32, 1)); | ||
} | ||
|
||
assert!(node.free); | ||
assert_eq!(node.size, 256 - size_of::<LinkedAllocatorNode>()); | ||
assert_eq!(node.next, None); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "kernel" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[dependencies] | ||
bitflags = "2.4.0" | ||
lazy_static = { version = "1.4.0", features = ["spin_no_std"] } | ||
multiboot2 = { version = "0.19.0", default-features = false } | ||
spin = "0.9.8" | ||
uart_16550 = "0.3.0" | ||
x86_64 = "0.14.11" | ||
|
||
allocator = { path = "../allocator" } |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
#![no_std] | ||
#![no_main] | ||
#![feature(const_mut_refs)] | ||
#![feature(abi_x86_interrupt)] | ||
#![feature(let_chains)] | ||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.