Big restructure pt.1

This commit is contained in:
Mia
2026-02-26 19:40:27 +01:00
parent 02f2ddfc67
commit f4334e79f6
32 changed files with 905 additions and 842 deletions
+19 -19
View File
@@ -8,16 +8,16 @@ use std::{alloc::Layout, mem::MaybeUninit};
pub type DropFn = Option<unsafe fn(*mut u8)>;
pub struct AllocationEntry {
ptr: *mut u8,
layout: Layout,
drop_fn: DropFn,
ptr: *mut u8,
layout: Layout,
drop_fn: DropFn,
}
unsafe impl Send for AllocationEntry {}
unsafe impl Sync for AllocationEntry {}
pub trait Allocator {
unsafe fn alloc_unsafe(&self, data: *const u8, layout: Layout, drop: DropFn) -> *mut u8;
unsafe fn alloc_unsafe(&self, data: *const u8, layout: Layout, drop: DropFn) -> *mut u8;
}
pub trait SyncAllocator: Allocator + Send + Sync {}
@@ -25,22 +25,22 @@ pub trait SyncAllocator: Allocator + Send + Sync {}
impl<T: Allocator + Send + Sync> SyncAllocator for T {}
impl<'l> dyn Allocator + 'l {
pub fn alloc<T>(&'l self, value: T) -> &'l mut T {
unsafe {
let value = MaybeUninit::new(value);
let data = value.as_ptr() as *const u8;
let layout = Layout::new::<T>();
let drop: DropFn = match std::mem::needs_drop::<T>() {
false => None,
true => Some(|ptr: *mut u8| std::ptr::drop_in_place(ptr as *mut T)),
};
&mut *(self.alloc_unsafe(data, layout, drop) as *mut T)
}
}
pub fn alloc<T>(&'l self, value: T) -> &'l mut T {
unsafe {
let value = MaybeUninit::new(value);
let data = value.as_ptr() as *const u8;
let layout = Layout::new::<T>();
let drop: DropFn = match std::mem::needs_drop::<T>() {
false => None,
true => Some(|ptr: *mut u8| std::ptr::drop_in_place(ptr as *mut T)),
};
&mut *(self.alloc_unsafe(data, layout, drop) as *mut T)
}
}
}
impl<'l> dyn SyncAllocator + 'l {
pub fn alloc<T: Send>(&'l self, value: T) -> &'l mut T {
<dyn Allocator>::alloc(self, value)
}
pub fn alloc<T: Send>(&'l self, value: T) -> &'l mut T {
<dyn Allocator>::alloc(self, value)
}
}