Skip to content

Commit

Permalink
Use allocator for early tags
Browse files Browse the repository at this point in the history
Signed-off-by: Graham MacDonald <[email protected]>
  • Loading branch information
gmacd committed Aug 17, 2024
1 parent f2b408d commit 74e3701
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
12 changes: 3 additions & 9 deletions aarch64/src/vmalloc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloc::sync::Arc;
use core::{alloc::Layout, mem::MaybeUninit, ptr::addr_of};
use core::{alloc::Layout, mem::MaybeUninit};
use port::{
mcslock::{Lock, LockNode},
mem::{VirtRange, PAGE_SIZE_4K},
Expand All @@ -15,8 +15,6 @@ static VMALLOC: Lock<Option<&'static mut VmAlloc>> = Lock::new("vmalloc", None);
// referening them from VmAlloc, from where they can be used in the global allocator.
//static mut MAYBE_HEAP_ARENA: MaybeUninit<Arena> = MaybeUninit::uninit();

static mut EARLY_TAGS_PAGE: [u8; 4096] = [0; 4096];

/// VmAlloc is an attempt to write a Bonwick vmem-style allocator. It currently
/// expects another allocator to exist beforehand.
/// TODO Use the allocator api trait.
Expand All @@ -27,18 +25,14 @@ struct VmAlloc {

impl VmAlloc {
fn new(early_allocator: &'static dyn core::alloc::Allocator, heap_range: VirtRange) -> Self {
let early_tags_ptr = addr_of!(EARLY_TAGS_PAGE) as usize;
let early_tags_size = unsafe { EARLY_TAGS_PAGE.len() };
let early_tags_range = VirtRange::with_len(early_tags_ptr, early_tags_size);

let heap_arena = Arc::new_in(
Lock::new(
"heap_arena",
Arena::new_with_static_range(
Arena::new_with_allocator(
"heap",
Some(Boundary::from(heap_range)),
PAGE_SIZE_4K,
early_tags_range,
early_allocator,
),
),
early_allocator,
Expand Down
1 change: 1 addition & 0 deletions port/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![cfg_attr(not(any(test)), no_std)]
#![feature(allocator_api)]
#![feature(maybe_uninit_slice)]
#![feature(slice_ptr_get)]
#![feature(step_trait)]
#![forbid(unsafe_op_in_unsafe_fn)]

Expand Down
17 changes: 11 additions & 6 deletions port/src/vmem.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::mcslock::Lock;
use crate::mem::VirtRange;
use crate::{mcslock::Lock, mem::PAGE_SIZE_4K};
use alloc::sync::Arc;
use core::{ops::Range, ptr::null_mut, slice};
use core::{alloc::Layout, ops::Range, ptr::null_mut, slice};

#[cfg(not(test))]
use crate::println;
Expand Down Expand Up @@ -326,15 +326,20 @@ impl Arena {
/// Only to be used for creation of initial heap
/// Create a new arena, assuming there is no dynamic allocation available,
/// and all free tags come from the free_tags provided.
pub fn new_with_static_range(
pub fn new_with_allocator(
name: &'static str,
initial_span: Option<Boundary>,
quantum: usize,
static_range: VirtRange,
allocator: &'static dyn core::alloc::Allocator,
) -> Self {
let tags_addr = unsafe { &mut *(static_range.start() as *mut TagItem) };
let layout = unsafe { Layout::from_size_align_unchecked(PAGE_SIZE_4K, PAGE_SIZE_4K) };
let tags_buffer =
allocator.allocate_zeroed(layout).expect("unable to allocate initial vmem tags");
let tags = unsafe {
slice::from_raw_parts_mut(tags_addr, static_range.size() / size_of::<TagItem>())
slice::from_raw_parts_mut(
tags_buffer.as_mut_ptr() as *mut TagItem,
layout.size() / size_of::<TagItem>(),
)
};

Self::new_with_tags(name, initial_span, quantum, tags)
Expand Down

0 comments on commit 74e3701

Please sign in to comment.