impl<T> MemoryVec<T> {
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn new(pool_type: PoolType) -> Result<Self> {
let memory_manager = global_memory_manager()?;
Ok(Self {
data: Vec::new(),
pool_type,
memory_manager,
})
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn with_capacity(pool_type: PoolType, capacity: usize) -> Result<Self> {
let memory_manager = global_memory_manager()?;
Ok(Self {
data: Vec::with_capacity(capacity),
pool_type,
memory_manager,
})
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn push(&mut self, item: T) -> Result<()> {
let old_capacity = self.data.capacity();
self.data.push(item);
let new_capacity = self.data.capacity();
if new_capacity > old_capacity {
let growth = (new_capacity - old_capacity) * std::mem::size_of::<T>();
trace!(
"MemoryVec grew by {} bytes for pool {:?}",
growth,
self.pool_type
);
}
Ok(())
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn reserve(&mut self, additional: usize) -> Result<()> {
let old_capacity = self.data.capacity();
self.data.reserve(additional);
let new_capacity = self.data.capacity();
if new_capacity > old_capacity {
let growth = (new_capacity - old_capacity) * std::mem::size_of::<T>();
trace!(
"MemoryVec reserved {} bytes for pool {:?}",
growth,
self.pool_type
);
}
Ok(())
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn memory_usage(&self) -> usize {
self.data.capacity() * std::mem::size_of::<T>()
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn process_with_memory_awareness<F, R>(&self, f: F) -> Result<R>
where
F: FnOnce(&[T]) -> R,
{
let stats = self.memory_manager.stats();
if stats.allocation_pressure > 0.9 {
debug!(
"High memory pressure detected: {:.1}%",
stats.allocation_pressure * 100.0
);
self.memory_manager.cleanup()?;
}
Ok(f(&self.data))
}
}
impl<T> Deref for MemoryVec<T> {
type Target = Vec<T>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<T> DerefMut for MemoryVec<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
impl MemoryString {
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn new(content: &str) -> Result<Self> {
let memory_manager = global_memory_manager()?;
let interned = memory_manager.intern_string(content)?;
Ok(Self {
content: interned,
memory_manager,
})
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn as_str(&self) -> &str {
&self.content
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn shares_memory_with(&self, other: &MemoryString) -> bool {
Arc::ptr_eq(&self.content, &other.content)
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn memory_stats(&self) -> super::memory_manager::MemoryStats {
self.memory_manager.stats()
}
}
impl Deref for MemoryString {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.content
}
}
impl std::fmt::Display for MemoryString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.content)
}
}
impl std::fmt::Debug for MemoryString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "MemoryString({})", self.content)
}
}
impl AstBufferPool {
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn new(pool_type: PoolType) -> Result<Self> {
let memory_manager = global_memory_manager()?;
Ok(Self {
memory_manager,
pool_type,
})
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn get_buffer(&self, min_size: usize) -> Result<PooledBuffer> {
self.memory_manager
.allocate_buffer(self.pool_type, min_size)
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn get_buffer_for_content(&self, content: &str) -> Result<PooledBuffer> {
let size = content.len() * 2; self.memory_manager.allocate_buffer(self.pool_type, size)
}
}
impl InternedStringSet {
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn new() -> Result<Self> {
let memory_manager = global_memory_manager()?;
Ok(Self {
strings: std::collections::HashSet::new(),
memory_manager,
})
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn insert(&mut self, s: &str) -> Result<bool> {
let interned = self.memory_manager.intern_string(s)?;
Ok(self.strings.insert(interned))
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn contains(&self, s: &str) -> bool {
if let Ok(interned) = self.memory_manager.intern_string(s) {
self.strings.contains(&interned)
} else {
false
}
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn iter(&self) -> impl Iterator<Item = &str> {
self.strings.iter().map(std::convert::AsRef::as_ref)
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn memory_usage(&self) -> usize {
self.strings.len() * std::mem::size_of::<Arc<str>>()
}
}
impl<K, V> MemoryAwareCache<K, V>
where
K: std::hash::Hash + Eq + Clone,
V: Clone,
{
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn new(pool_type: PoolType, max_items: usize) -> Result<Self> {
let memory_manager = global_memory_manager()?;
Ok(Self {
cache: std::collections::HashMap::new(),
memory_manager,
pool_type,
max_items,
})
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn insert(&mut self, key: K, value: V) -> Result<Option<V>> {
let stats = self.memory_manager.stats();
if stats.allocation_pressure > 0.85 && self.cache.len() >= self.max_items {
let keys_to_remove: Vec<_> = self
.cache
.keys()
.take(self.cache.len() / 4)
.cloned()
.collect();
for key in keys_to_remove {
self.cache.remove(&key);
}
debug!("Evicted cache entries due to memory pressure");
}
Ok(self.cache.insert(key, value))
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn get(&self, key: &K) -> Option<&V> {
self.cache.get(key)
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn stats(&self) -> CacheStats {
CacheStats {
item_count: self.cache.len(),
max_items: self.max_items,
estimated_memory: self.cache.len()
* (std::mem::size_of::<K>() + std::mem::size_of::<V>()),
}
}
#[must_use]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn pool_type(&self) -> PoolType {
self.pool_type
}
}