Remove old unused virtual_memory/hole.rs file
This commit is contained in:
parent
fc8c2c5748
commit
01187de0cf
@ -1,163 +0,0 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use core::{
|
||||
cell::UnsafeCell,
|
||||
marker::PhantomData,
|
||||
mem,
|
||||
ops::{Deref, DerefMut},
|
||||
ptr::{addr_of, NonNull},
|
||||
sync::atomic::{AtomicU16, AtomicU8, Ordering},
|
||||
};
|
||||
|
||||
use intrusive_collections::{
|
||||
container_of, offset_of, Adapter, DefaultLinkOps, LinkOps, LinkedListLink, PointerOps,
|
||||
};
|
||||
use static_assertions::const_assert;
|
||||
|
||||
struct Hole {
|
||||
start: u64,
|
||||
size: u64,
|
||||
link: LinkedListLink,
|
||||
}
|
||||
|
||||
unsafe impl Sync for HolePage {}
|
||||
|
||||
#[repr(align(4096))]
|
||||
struct HolePage {
|
||||
holes: UnsafeCell<[Hole; 127]>,
|
||||
ptr_bitmap: [AtomicU8; 16],
|
||||
ptr_counter: AtomicU16,
|
||||
}
|
||||
|
||||
impl Drop for HolePage {
|
||||
fn drop(&mut self) {
|
||||
assert!(
|
||||
self.ptr_counter.load(Ordering::Relaxed) == 0,
|
||||
"HolePage cannot be dropped when HolePtrs to it exist!"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const_assert!(mem::size_of::<HolePage>() <= 4096);
|
||||
|
||||
impl HolePage {
|
||||
fn get_ptr(&self, idx: u8) -> HolePtr {
|
||||
let byte = idx / 8;
|
||||
let bit = idx % 8;
|
||||
let mask = 1 << bit;
|
||||
assert!(
|
||||
self.ptr_bitmap[byte as usize].load(Ordering::Relaxed) & mask != 0,
|
||||
"Attempted to get a HolePtr to a Hole with an existing pointer"
|
||||
);
|
||||
self.ptr_bitmap[byte as usize].fetch_or(mask, Ordering::Relaxed);
|
||||
self.ptr_counter.fetch_add(1, Ordering::Relaxed);
|
||||
HolePtr { page: self as *const _, idx }
|
||||
}
|
||||
fn get_raw_ptr(&self, idx: u8) -> *const Hole {
|
||||
assert!(idx < 127);
|
||||
unsafe { (self.holes.get().cast::<Hole>()).add(idx as usize) }
|
||||
}
|
||||
fn get_mut_raw_ptr(&self, idx: u8) -> *mut Hole {
|
||||
assert!(idx < 127);
|
||||
unsafe { (self.holes.get().cast::<Hole>()).add(idx as usize) }
|
||||
}
|
||||
}
|
||||
|
||||
struct HolePtr {
|
||||
page: *const HolePage,
|
||||
idx: u8,
|
||||
}
|
||||
|
||||
impl Deref for HolePtr {
|
||||
type Target = Hole;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { &*((*self.page).get_raw_ptr(self.idx)) }
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for HolePtr {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
unsafe { &mut *((*self.page).get_mut_raw_ptr(self.idx)) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for HolePtr {
|
||||
fn drop(&mut self) {
|
||||
let byte = self.idx / 8;
|
||||
let bit = self.idx % 8;
|
||||
let mask = !(1 << bit);
|
||||
let page = unsafe { &*self.page };
|
||||
page.ptr_bitmap[byte as usize].fetch_and(mask, Ordering::Relaxed);
|
||||
let old_ctr = page.ptr_counter.fetch_sub(1, Ordering::Relaxed);
|
||||
if old_ctr - 1 == 0 {
|
||||
|
||||
// TODO: Deallocate page
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct HolePtrOps(PhantomData<HolePtr>);
|
||||
|
||||
unsafe impl PointerOps for HolePtrOps {
|
||||
type Value = Hole;
|
||||
type Pointer = HolePtr;
|
||||
|
||||
unsafe fn from_raw(&self, value: *const Self::Value) -> Self::Pointer {
|
||||
let page = (value as usize & !0xFFFusize) as *const HolePage;
|
||||
let holes = unsafe { addr_of!((*page).holes).cast::<Hole>() };
|
||||
let idx = unsafe { value.offset_from(holes) };
|
||||
HolePtr { page: unsafe { &*page }, idx: idx as u8 }
|
||||
}
|
||||
|
||||
fn into_raw(&self, ptr: Self::Pointer) -> *const Self::Value {
|
||||
unsafe { &*ptr.page }.get_raw_ptr(ptr.idx)
|
||||
}
|
||||
}
|
||||
|
||||
struct HoleAdapter {
|
||||
link_ops: <LinkedListLink as DefaultLinkOps>::Ops,
|
||||
ptr_ops: HolePtrOps,
|
||||
}
|
||||
|
||||
impl HoleAdapter {
|
||||
fn new() -> Self {
|
||||
Self { link_ops: <LinkedListLink as DefaultLinkOps>::NEW, ptr_ops: HolePtrOps(PhantomData) }
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl Adapter for HoleAdapter {
|
||||
type LinkOps = <LinkedListLink as DefaultLinkOps>::Ops;
|
||||
|
||||
type PointerOps = HolePtrOps;
|
||||
|
||||
unsafe fn get_value(
|
||||
&self,
|
||||
link: <Self::LinkOps as LinkOps>::LinkPtr,
|
||||
) -> *const <Self::PointerOps as PointerOps>::Value {
|
||||
container_of!(link.as_ptr(), Hole, link)
|
||||
}
|
||||
|
||||
unsafe fn get_link(
|
||||
&self,
|
||||
value: *const <Self::PointerOps as PointerOps>::Value,
|
||||
) -> <Self::LinkOps as LinkOps>::LinkPtr {
|
||||
// We need to do this instead of just accessing the field directly
|
||||
// to strictly follow the stack borrow rules.
|
||||
unsafe {
|
||||
NonNull::new_unchecked((value.cast::<u8>()).add(offset_of!(Hole, link)) as *mut _)
|
||||
}
|
||||
}
|
||||
|
||||
fn link_ops(&self) -> &Self::LinkOps {
|
||||
&self.link_ops
|
||||
}
|
||||
|
||||
fn link_ops_mut(&mut self) -> &mut Self::LinkOps {
|
||||
&mut self.link_ops
|
||||
}
|
||||
|
||||
fn pointer_ops(&self) -> &Self::PointerOps {
|
||||
&self.ptr_ops
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user