use crate::{memory_management::ManagedMemoryBinding, server::IoError, storage_id_type};
use core::fmt::Debug;
storage_id_type!(StorageId);
impl core::fmt::Display for StorageId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!("StorageId({})", self.value))
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StorageUtilization {
pub offset: u64,
pub size: u64,
}
#[derive(new, Clone, Debug)]
pub struct StorageHandle {
pub id: StorageId,
pub utilization: StorageUtilization,
}
impl StorageHandle {
pub fn size(&self) -> u64 {
self.utilization.size
}
pub fn offset(&self) -> u64 {
self.utilization.offset
}
pub fn offset_start(&self, offset_bytes: u64) -> Self {
let utilization = StorageUtilization {
offset: self.offset() + offset_bytes,
size: self.size() - offset_bytes,
};
Self {
id: self.id,
utilization,
}
}
pub fn offset_end(&self, offset_bytes: u64) -> Self {
let utilization = StorageUtilization {
offset: self.offset(),
size: self.size() - offset_bytes,
};
Self {
id: self.id,
utilization,
}
}
}
pub trait ComputeStorage: Send {
type Resource: Send;
fn alignment(&self) -> usize;
fn get(&mut self, handle: &StorageHandle) -> Self::Resource;
fn alloc(&mut self, size: u64) -> Result<StorageHandle, IoError>;
fn dealloc(&mut self, id: StorageId);
fn flush(&mut self);
}
#[derive(new, Debug)]
pub struct ManagedResource<Resource: Send> {
#[allow(unused)]
binding: ManagedMemoryBinding,
resource: Resource,
}
impl<Resource: Send> ManagedResource<Resource> {
pub fn resource(&self) -> &Resource {
&self.resource
}
}