pub const MAX_WIDTH: u32 = 4096;
pub const MAX_HEIGHT: u32 = 4096;
pub const MAX_GLYPHS: usize = 65_536;
pub const MAX_TOTAL_ALLOCATION: usize = 256 * 1024 * 1024;
pub fn validate_dimensions(width: u32, height: u32) -> Result<usize, String> {
if width > MAX_WIDTH {
return Err(format!("Width {} exceeds maximum {}", width, MAX_WIDTH));
}
if height > MAX_HEIGHT {
return Err(format!("Height {} exceeds maximum {}", height, MAX_HEIGHT));
}
let w = width as usize;
let h = height as usize;
w.checked_mul(h)
.ok_or_else(|| format!("Dimensions {}x{} overflow", width, height))
}
#[derive(Debug, Default)]
pub struct AllocationTracker {
total: usize,
limit: usize,
}
impl AllocationTracker {
pub fn new() -> Self {
Self {
total: 0,
limit: MAX_TOTAL_ALLOCATION,
}
}
pub fn reserve(&mut self, bytes: usize) -> Result<(), String> {
let new_total = self
.total
.checked_add(bytes)
.ok_or_else(|| "Allocation overflow".to_string())?;
if new_total > self.limit {
return Err(format!(
"Total allocation {} exceeds limit {} bytes",
new_total, self.limit
));
}
self.total = new_total;
Ok(())
}
}