Example use of Box, Vec, and Rc in kernel_main

This commit is contained in:
Philipp Oppermann
2019-06-26 15:06:40 +02:00
parent d7484ab48b
commit f429a8ab03

View File

@@ -9,7 +9,7 @@ extern crate alloc;
use blog_os::println;
use bootloader::{entry_point, BootInfo};
use core::panic::PanicInfo;
use alloc::boxed::Box;
use alloc::{boxed::Box, vec, vec::Vec, rc::Rc};
entry_point!(kernel_main);
@@ -26,7 +26,23 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
allocator::init_heap(&mut mapper, &mut frame_allocator)
.expect("heap initialization failed");
let x = Box::new(41);
// allocate a number on the heap
let heap_value = Box::new(41);
println!("heap_value at {:p}", heap_value);
// create a dynamically sized vector
let mut vec = Vec::new();
for i in 0..500 {
vec.push(i);
}
println!("vec at {:p}", vec.as_slice());
// create a reference counted vector -> will be deallocated when count reaches 0
let reference_counted = Rc::new(vec![1, 2, 3]);
let cloned_reference = reference_counted.clone();
println!("current reference count is {}", Rc::strong_count(&cloned_reference));
core::mem::drop(reference_counted);
println!("reference count is {} now", Rc::strong_count(&cloned_reference));
#[cfg(test)]
test_main();