mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Make the bump allocator lock free and impl Alloc for shared reference
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#![feature(lang_items)]
|
#![feature(lang_items)]
|
||||||
#![feature(const_fn)]
|
#![feature(const_fn)]
|
||||||
#![feature(alloc)]
|
#![feature(alloc)]
|
||||||
#![feature(const_unique_new)]
|
#![feature(const_unique_new, const_atomic_usize_new)]
|
||||||
#![feature(unique)]
|
#![feature(unique)]
|
||||||
#![feature(allocator_api)]
|
#![feature(allocator_api)]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|||||||
@@ -1,29 +1,40 @@
|
|||||||
use alloc::heap::{Alloc, AllocErr, Layout};
|
use alloc::heap::{Alloc, AllocErr, Layout};
|
||||||
|
|
||||||
|
use core::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
/// A simple allocator that allocates memory linearly and ignores freed memory.
|
/// A simple allocator that allocates memory linearly and ignores freed memory.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct BumpAllocator {
|
pub struct BumpAllocator {
|
||||||
heap_start: usize,
|
heap_start: usize,
|
||||||
heap_end: usize,
|
heap_end: usize,
|
||||||
next: usize,
|
next: AtomicUsize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BumpAllocator {
|
impl BumpAllocator {
|
||||||
pub const fn new(heap_start: usize, heap_end: usize) -> Self {
|
pub const fn new(heap_start: usize, heap_end: usize) -> Self {
|
||||||
Self { heap_start, heap_end, next: heap_start }
|
Self { heap_start, heap_end, next: AtomicUsize::new(heap_start) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Alloc for BumpAllocator {
|
unsafe impl<'a> Alloc for &'a BumpAllocator {
|
||||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
|
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
|
||||||
let alloc_start = align_up(self.next, layout.align());
|
loop {
|
||||||
let alloc_end = alloc_start.saturating_add(layout.size());
|
// load current state of the `next` field
|
||||||
|
let current_next = self.next.load(Ordering::Relaxed);
|
||||||
|
let alloc_start = align_up(current_next, layout.align());
|
||||||
|
let alloc_end = alloc_start.saturating_add(layout.size());
|
||||||
|
|
||||||
if alloc_end <= self.heap_end {
|
if alloc_end <= self.heap_end {
|
||||||
self.next = alloc_end;
|
// update the `next` pointer if it still has the value `current_next`
|
||||||
Ok(alloc_start as *mut u8)
|
let next_now = self.next.compare_and_swap(current_next, alloc_end,
|
||||||
} else {
|
Ordering::Relaxed);
|
||||||
Err(AllocErr::Exhausted{ request: layout })
|
if next_now == current_next {
|
||||||
|
// next address was successfully updated, allocation succeeded
|
||||||
|
return Ok(alloc_start as *mut u8);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(AllocErr::Exhausted{ request: layout })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user