use core::sync::atomic::{AtomicU32, AtomicU64};
use crate::layout::BLOCK;
use super::region::WINDOW;
pub const MAGIC: u64 = u64::from_le_bytes(*b"paragcP2");
pub const PAGE: usize = 4096;
pub const TYPE_CAP: usize = 1024;
pub const MAP_CAP: usize = 64 * 1024;
pub const BYTES_PER_ENTRY: usize = 16 * 1024;
pub const ATTACH_CAP: usize = 32;
pub const ATTACH_OFF: usize = 1024;
#[repr(C)]
pub struct Super {
pub magic: AtomicU64,
pub window: AtomicU64,
pub heap: AtomicU64,
pub request: AtomicU32,
pub marking: AtomicU32,
pub epoch: AtomicU32,
pub blocks: AtomicU32,
pub collections: AtomicU64,
pub takeovers: AtomicU64,
pub available: AtomicU64,
pub types: AtomicU32,
pub map: AtomicU32,
pub entries: AtomicU32,
_pad: u32,
pub free_entries: AtomicU64,
pub committed: [AtomicU64; Segment::COUNT],
pub live: AtomicU64,
pub allocated: AtomicU64,
}
impl Super {
pub fn new(heap: u64) -> Self {
Self {
magic: AtomicU64::new(MAGIC),
window: AtomicU64::new(WINDOW as u64),
heap: AtomicU64::new(heap),
request: AtomicU32::new(0),
marking: AtomicU32::new(0),
epoch: AtomicU32::new(1),
blocks: AtomicU32::new(0),
collections: AtomicU64::new(0),
takeovers: AtomicU64::new(0),
available: AtomicU64::new(0),
types: AtomicU32::new(0),
map: AtomicU32::new(0),
entries: AtomicU32::new(0),
_pad: 0,
free_entries: AtomicU64::new(0),
committed: core::array::from_fn(|_| AtomicU64::new(0)),
live: AtomicU64::new(0),
allocated: AtomicU64::new(0),
}
}
}
#[repr(C, align(64))]
pub struct Attachment {
pub pid: AtomicU32,
pub inside: AtomicU32,
pub start: AtomicU64,
pub waiting: AtomicU32,
}
const _: () = assert!(size_of::<Super>() <= ATTACH_OFF && ATTACH_OFF + ATTACH_CAP * size_of::<Attachment>() <= PAGE, "挂接表放在超级块那一页里");
#[derive(Clone, Copy)]
pub enum Segment {
Types,
Map,
Entries,
States,
}
impl Segment {
pub const COUNT: usize = 4;
pub fn index(self) -> usize {
self as usize
}
}
#[repr(C)]
pub struct TypeEntry {
pub ready: AtomicU32,
pub edges: AtomicU32,
pub map: AtomicU32,
pub size: AtomicU32,
pub key: [AtomicU64; 2],
}
impl TypeEntry {
pub fn has_key(&self, key: [u64; 2]) -> bool {
self.ready.load(core::sync::atomic::Ordering::Acquire) == 1 && self.key.iter().zip(key).all(|(k, v)| k.load(core::sync::atomic::Ordering::Relaxed) == v)
}
}
#[repr(C)]
pub struct Entry {
pub ptr: AtomicU64,
pub holder: AtomicU32,
pub next: AtomicU32,
}
#[repr(C)]
pub struct BlockState {
pub kind: AtomicU32,
pub next: AtomicU32,
}
#[repr(u32)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Unused = 0,
Small = 1,
LargeHead = 2,
LargeTail = 3,
}
impl From<u32> for Kind {
fn from(v: u32) -> Self {
match v {
1 => Self::Small,
2 => Self::LargeHead,
3 => Self::LargeTail,
_ => Self::Unused,
}
}
}
#[derive(Clone, Copy)]
pub struct Layout {
pub types: usize,
pub map: usize,
pub entries: usize,
pub entry_cap: usize,
pub states: usize,
pub blocks: usize,
pub max_blocks: usize,
}
impl Layout {
pub fn new() -> Self {
let types = size_of::<Super>().next_multiple_of(PAGE);
let map = types + TYPE_CAP * size_of::<TypeEntry>();
let entries = (map + MAP_CAP * size_of::<u32>()).next_multiple_of(PAGE);
let entry_cap = WINDOW / BYTES_PER_ENTRY;
let states = (entries + entry_cap * size_of::<Entry>()).next_multiple_of(PAGE);
let blocks = (states + WINDOW / BLOCK * size_of::<BlockState>()).next_multiple_of(BLOCK);
let max_blocks = (WINDOW - blocks) / BLOCK;
assert!(max_blocks > 0, "窗口放不下任何块");
Self { types, map, entries, entry_cap, states, blocks, max_blocks }
}
pub fn segment(&self, s: Segment) -> (usize, usize) {
match s {
Segment::Types => (self.types, self.map),
Segment::Map => (self.map, self.entries),
Segment::Entries => (self.entries, self.states),
Segment::States => (self.states, self.blocks),
}
}
}