mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Update to use the new API for custom allocators (#348)
* Update to new allocator API * Change linked_list_allocator dependency to link directly to git repository * Add Cargo.lock to gitignore
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
# Generated by Cargo
|
# Generated by Cargo
|
||||||
/target/
|
/target/
|
||||||
|
Cargo.lock
|
||||||
|
|
||||||
# Build directory
|
# Build directory
|
||||||
/build/
|
/build/
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ name = "hole_list_allocator"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
linked_list_allocator = "0.2.0"
|
linked_list_allocator = { git = "https://github.com/phil-opp/linked-list-allocator.git"}
|
||||||
spin = "0.4.5"
|
spin = "0.4.5"
|
||||||
|
|
||||||
[dependencies.lazy_static]
|
[dependencies.lazy_static]
|
||||||
|
|||||||
@@ -7,62 +7,51 @@
|
|||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
#![feature(allocator)]
|
|
||||||
#![feature(const_fn)]
|
#![feature(const_fn)]
|
||||||
|
#![feature(allocator_api)]
|
||||||
#![allocator]
|
#![feature(alloc)]
|
||||||
|
#![feature(global_allocator)]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
#![deny(warnings)]
|
||||||
|
|
||||||
use spin::Mutex;
|
extern crate alloc;
|
||||||
use linked_list_allocator::Heap;
|
|
||||||
|
|
||||||
extern crate spin;
|
extern crate spin;
|
||||||
extern crate linked_list_allocator;
|
extern crate linked_list_allocator;
|
||||||
#[macro_use]
|
|
||||||
extern crate lazy_static;
|
use alloc::heap::{Alloc, AllocErr, Layout};
|
||||||
|
use spin::Mutex;
|
||||||
|
use linked_list_allocator::Heap;
|
||||||
|
|
||||||
pub const HEAP_START: usize = 0o_000_001_000_000_0000;
|
pub const HEAP_START: usize = 0o_000_001_000_000_0000;
|
||||||
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
|
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
|
||||||
|
|
||||||
lazy_static! {
|
static HEAP: Mutex<Option<Heap>> = Mutex::new(None);
|
||||||
static ref HEAP: Mutex<Heap> = Mutex::new(unsafe {
|
|
||||||
Heap::new(HEAP_START, HEAP_SIZE)
|
//Set up the heap
|
||||||
});
|
pub unsafe fn init(offset: usize, size: usize) {
|
||||||
|
*HEAP.lock() = Some(Heap::new(offset, size));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
pub struct Allocator;
|
||||||
pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
|
|
||||||
HEAP.lock().allocate_first_fit(size, align).expect("out of memory")
|
unsafe impl<'a> Alloc for &'a Allocator {
|
||||||
|
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
|
||||||
|
if let Some(ref mut heap) = *HEAP.lock() {
|
||||||
|
heap.allocate_first_fit(layout)
|
||||||
|
} else {
|
||||||
|
panic!("Heap not initialized!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
|
||||||
|
if let Some(ref mut heap) = *HEAP.lock() {
|
||||||
|
heap.deallocate(ptr, layout)
|
||||||
|
} else {
|
||||||
|
panic!("heap not initalized");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
//Our allocator static
|
||||||
pub extern fn __rust_deallocate(ptr: *mut u8, size: usize, align: usize) {
|
#[global_allocator]
|
||||||
unsafe { HEAP.lock().deallocate(ptr, size, align) };
|
static GLOBAL_ALLOC: Allocator = Allocator;
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern fn __rust_usable_size(size: usize, _align: usize) -> usize {
|
|
||||||
size
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern fn __rust_reallocate_inplace(_ptr: *mut u8, size: usize,
|
|
||||||
_new_size: usize, _align: usize) -> usize
|
|
||||||
{
|
|
||||||
size
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern fn __rust_reallocate(ptr: *mut u8, size: usize, new_size: usize,
|
|
||||||
align: usize) -> *mut u8 {
|
|
||||||
use core::{ptr, cmp};
|
|
||||||
|
|
||||||
// from: https://github.com/rust-lang/rust/blob/
|
|
||||||
// c66d2380a810c9a2b3dbb4f93a830b101ee49cc2/
|
|
||||||
// src/liballoc_system/lib.rs#L98-L101
|
|
||||||
|
|
||||||
let new_ptr = __rust_allocate(new_size, align);
|
|
||||||
unsafe { ptr::copy(ptr, new_ptr, cmp::min(size, new_size)) };
|
|
||||||
__rust_deallocate(ptr, size, align);
|
|
||||||
new_ptr
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ extern crate bit_field;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
|
|
||||||
extern crate hole_list_allocator;
|
extern crate hole_list_allocator as allocator;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ pub extern "C" fn rust_main(multiboot_information_address: usize) {
|
|||||||
|
|
||||||
// initialize our IDT
|
// initialize our IDT
|
||||||
interrupts::init(&mut memory_controller);
|
interrupts::init(&mut memory_controller);
|
||||||
|
|
||||||
fn stack_overflow() {
|
fn stack_overflow() {
|
||||||
stack_overflow(); // for each recursion, the return address is pushed
|
stack_overflow(); // for each recursion, the return address is pushed
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ pub use self::paging::remap_the_kernel;
|
|||||||
pub use self::stack_allocator::Stack;
|
pub use self::stack_allocator::Stack;
|
||||||
use self::paging::PhysicalAddress;
|
use self::paging::PhysicalAddress;
|
||||||
use multiboot2::BootInformation;
|
use multiboot2::BootInformation;
|
||||||
|
use allocator;
|
||||||
|
|
||||||
mod area_frame_allocator;
|
mod area_frame_allocator;
|
||||||
mod paging;
|
mod paging;
|
||||||
@@ -62,7 +63,7 @@ pub fn init(boot_info: &BootInformation) -> MemoryController {
|
|||||||
let mut active_table = paging::remap_the_kernel(&mut frame_allocator, boot_info);
|
let mut active_table = paging::remap_the_kernel(&mut frame_allocator, boot_info);
|
||||||
|
|
||||||
use self::paging::Page;
|
use self::paging::Page;
|
||||||
use hole_list_allocator::{HEAP_START, HEAP_SIZE};
|
use allocator::{HEAP_START, HEAP_SIZE};
|
||||||
|
|
||||||
let heap_start_page = Page::containing_address(HEAP_START);
|
let heap_start_page = Page::containing_address(HEAP_START);
|
||||||
let heap_end_page = Page::containing_address(HEAP_START + HEAP_SIZE - 1);
|
let heap_end_page = Page::containing_address(HEAP_START + HEAP_SIZE - 1);
|
||||||
@@ -71,6 +72,11 @@ pub fn init(boot_info: &BootInformation) -> MemoryController {
|
|||||||
active_table.map(page, paging::WRITABLE, &mut frame_allocator);
|
active_table.map(page, paging::WRITABLE, &mut frame_allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Init the heap
|
||||||
|
unsafe {
|
||||||
|
allocator::init(HEAP_START, HEAP_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
let stack_allocator = {
|
let stack_allocator = {
|
||||||
let stack_alloc_start = heap_end_page + 1;
|
let stack_alloc_start = heap_end_page + 1;
|
||||||
let stack_alloc_end = stack_alloc_start + 100;
|
let stack_alloc_end = stack_alloc_start + 100;
|
||||||
|
|||||||
Reference in New Issue
Block a user