Skip to content

Commit c9bcb24

Browse files
committed
userspace: Implement heap
1 parent 5b05db7 commit c9bcb24

File tree

5 files changed

+377
-12
lines changed

5 files changed

+377
-12
lines changed

src/common/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
#![no_std]
22
#![allow(dead_code)]
33
#![allow(unused_variables)]
4+
#![feature(custom_test_frameworks)]
5+
#![test_runner(test_runner)]
6+
#![reexport_test_harness_main = "test_main"]
47

58
pub mod mutex;
69
pub mod syscalls;
10+
11+
// Inspired by https://os.phil-opp.com/testing/
12+
13+
pub trait Testable {
14+
fn run(&self);
15+
}
16+
17+
impl<T> Testable for T
18+
where
19+
T: Fn(),
20+
{
21+
fn run(&self) {
22+
// print!("TEST: {}\n", core::any::type_name::<T>());
23+
self();
24+
}
25+
}
26+
27+
#[allow(dead_code)]
28+
fn test_runner(tests: &[&dyn Testable]) {
29+
// println!("Running {} tests", tests.len());
30+
for test in tests {
31+
test.run();
32+
}
33+
}
34+
35+
#[cfg(test)]
36+
pub fn mytest() {
37+
test_main();
38+
}

src/common/src/mutex.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,11 @@ impl<'a, T> DerefMut for MutexGuard<'a, T> {
7272
unsafe { &mut *self.mutex.data.get() }
7373
}
7474
}
75+
76+
#[cfg(test)]
77+
mod tests {
78+
#[test_case]
79+
fn fail_lib_tets() {
80+
assert!(1 == 2, "THIS MUST FAIL");
81+
}
82+
}

src/userspace/src/bin/shell.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#![no_std]
22
#![no_main]
33

4-
use common::{mutex::Mutex, syscalls::SYSCALL_WAIT};
4+
use alloc::string::String;
5+
use common::syscalls::SYSCALL_WAIT;
56
use userspace::{print, println, util::wait};
67

8+
extern crate alloc;
79
extern crate userspace;
810

9-
static INPUT_BUFFER: Mutex<[u8; 1024]> = Mutex::new([0; 1024]);
1011
const DELETE: u8 = 127;
1112

1213
#[no_mangle]
@@ -16,8 +17,7 @@ fn main() {
1617
println!("Type 'help' for a list of available commands.");
1718
loop {
1819
print!("$ ");
19-
let mut input_buffer = INPUT_BUFFER.lock();
20-
let mut buffer_index = 0;
20+
let mut input = String::new();
2121
loop {
2222
let mut result: isize;
2323
loop {
@@ -35,26 +35,23 @@ fn main() {
3535
break;
3636
}
3737
DELETE => {
38-
if buffer_index > 0 {
39-
buffer_index -= 1;
38+
if input.pop().is_some() {
4039
print!("{}{}{}", 8 as char, ' ', 8 as char);
4140
}
4241
}
4342
_ => {
44-
input_buffer[buffer_index] = next_char;
45-
buffer_index += 1;
43+
input.push(next_char as char);
4644
print!("{}", next_char as char);
4745
}
4846
}
4947
}
5048
// Parse input and execute
51-
let command = core::str::from_utf8(&input_buffer[0..buffer_index]).unwrap();
52-
parse_command_and_execute(command);
49+
parse_command_and_execute(input);
5350
}
5451
}
5552

56-
fn parse_command_and_execute(command: &str) {
57-
match command {
53+
fn parse_command_and_execute(command: String) {
54+
match command.as_str() {
5855
"" => {}
5956
"exit" => {
6057
println!("Exiting...");

0 commit comments

Comments
 (0)