use std::{
collections::HashMap,
fmt::{Debug, Display},
};
use petgraph::stable_graph::NodeIndex;
use talc::{ErrOnOom, Talc};
use crate::{engines::Backing, library::LibraryId, tls::TlsInfo};
mod tls;
#[repr(C)]
pub struct Compartment {
pub name: String,
pub id: CompartmentId,
pub(crate) library_names: HashMap<String, NodeIndex>,
pub(super) allocator: Talc<ErrOnOom>,
pub(super) alloc_objects: Vec<Backing>,
pub(crate) tls_info: HashMap<u64, TlsInfo>,
pub(crate) tls_gen: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash)]
#[repr(transparent)]
pub struct CompartmentId(pub(crate) usize);
impl Display for CompartmentId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl CompartmentId {
pub fn raw(&self) -> usize {
self.0
}
}
pub const MONITOR_COMPARTMENT_ID: CompartmentId = CompartmentId(0);
impl Compartment {
pub(crate) fn new(name: String, id: CompartmentId) -> Self {
Self {
name,
id,
library_names: HashMap::new(),
allocator: Talc::new(ErrOnOom),
alloc_objects: vec![],
tls_info: HashMap::new(),
tls_gen: 0,
}
}
pub fn library_ids(&self) -> impl Iterator<Item = LibraryId> + '_ {
self.library_names.values().map(|idx| LibraryId(*idx))
}
}
impl core::fmt::Display for Compartment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)
}
}
impl Debug for Compartment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Compartment[{}]", self.name)
}
}