use crate::LinearMemory;
use crate::prelude::*;
use crate::runtime::vm::{MemoryImageSlot, MmapOffset, SendSyncPtr};
pub struct StaticMemory {
base: SendSyncPtr<u8>,
capacity: usize,
size: usize,
image_slot: MemoryImageSlot,
}
impl StaticMemory {
pub fn new(
base: MmapOffset,
base_capacity: usize,
initial_size: usize,
maximum_size: Option<usize>,
image_slot: MemoryImageSlot,
) -> Result<Self> {
if base_capacity < initial_size {
bail!(
"initial memory size of {initial_size} exceeds the pooling allocator's \
configured maximum memory size of {base_capacity} bytes",
);
}
let base_capacity = match maximum_size {
Some(max) if max < base_capacity => max,
_ => base_capacity,
};
Ok(Self {
base: base.as_non_null().into(),
capacity: base_capacity,
size: initial_size,
image_slot,
})
}
pub fn into_image_slot(self) -> MemoryImageSlot {
self.image_slot
}
}
unsafe impl LinearMemory for StaticMemory {
fn byte_size(&self) -> usize {
self.size
}
fn byte_capacity(&self) -> usize {
self.capacity
}
fn grow_to(&mut self, new_byte_size: usize) -> Result<()> {
assert!(new_byte_size <= self.capacity);
self.image_slot.set_heap_limit(new_byte_size)?;
self.size = new_byte_size;
Ok(())
}
fn as_ptr(&self) -> *mut u8 {
self.base.as_ptr()
}
}