mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Create hole_list_allocator crate
This commit is contained in:
2
libs/hole_list_allocator/.gitignore
vendored
Normal file
2
libs/hole_list_allocator/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Generated by Cargo
|
||||||
|
/target/
|
||||||
9
libs/hole_list_allocator/Cargo.toml
Normal file
9
libs/hole_list_allocator/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
authors = ["Philipp Oppermann <dev@phil-opp.com>"]
|
||||||
|
name = "hole_list_allocator"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
linked_list_allocator = "0.2.0"
|
||||||
|
spin = "0.3.5"
|
||||||
|
once = "0.2.0"
|
||||||
59
libs/hole_list_allocator/src/lib.rs
Normal file
59
libs/hole_list_allocator/src/lib.rs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
#![feature(allocator)]
|
||||||
|
#![feature(const_fn)]
|
||||||
|
|
||||||
|
#![allocator]
|
||||||
|
#![no_std]
|
||||||
|
|
||||||
|
use spin::Mutex;
|
||||||
|
use linked_list_allocator::Heap;
|
||||||
|
|
||||||
|
extern crate spin;
|
||||||
|
extern crate linked_list_allocator;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate once;
|
||||||
|
|
||||||
|
pub const HEAP_START: usize = 0o_000_001_000_000_0000;
|
||||||
|
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref HEAP: Mutex<Heap> = Mutex::new(unsafe {
|
||||||
|
Heap::new(HEAP_START, HEAP_SIZE)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
|
||||||
|
HEAP.lock().allocate_first_fit(size, align).expect("out of memory")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern fn __rust_deallocate(ptr: *mut u8, size: usize, align: usize) {
|
||||||
|
unsafe { HEAP.lock().deallocate(ptr, size, align) };
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user