alnyan/yggdrasil: update allocator code
This commit is contained in:
@@ -1,13 +1,17 @@
|
||||
use crate::alloc::{GlobalAlloc, Layout, System};
|
||||
use crate::ptr::NonNull;
|
||||
use crate::sync::Mutex;
|
||||
use libyalloc::Allocator;
|
||||
use libyalloc::allocator::BucketAllocator;
|
||||
|
||||
struct Global {
|
||||
inner: Mutex<BucketAllocator>,
|
||||
}
|
||||
|
||||
#[stable(feature = "alloc_system_type", since = "1.28.0")]
|
||||
unsafe impl GlobalAlloc for System {
|
||||
#[inline]
|
||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
||||
let ptr = ALLOC.lock().expect("Could not lock allocator mutex").alloc(layout);
|
||||
let ptr = ALLOC.inner.lock().expect("Could not lock allocator mutex").allocate(layout);
|
||||
|
||||
if let Some(ptr) = ptr { ptr.as_ptr() } else { crate::ptr::null_mut() }
|
||||
}
|
||||
@@ -16,8 +20,19 @@ unsafe impl GlobalAlloc for System {
|
||||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
||||
let ptr = NonNull::new(ptr).expect("Invalid pointer");
|
||||
|
||||
ALLOC.lock().expect("Could not lock allocator mutex").free(ptr, layout);
|
||||
ALLOC.inner.lock().expect("Could not lock allocator mutex").free(ptr, layout);
|
||||
}
|
||||
}
|
||||
|
||||
static ALLOC: Mutex<Allocator> = Mutex::new(Allocator::new());
|
||||
impl Global {
|
||||
const fn new() -> Self {
|
||||
Self { inner: Mutex::new(BucketAllocator::new()) }
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Send for Global {}
|
||||
unsafe impl Sync for Global {}
|
||||
|
||||
static ALLOC: Global = Global::new();
|
||||
|
||||
// static ALLOC: Mutex<BucketAllocator> = Mutex::new(BucketAllocator::new());
|
||||
|
||||
Reference in New Issue
Block a user