use crate::types::{AbsoluteOffset, ReservationId};
pub trait ReservationAllocator: Send + 'static {
fn next_reservation_details(&mut self, size: usize) -> (ReservationId, AbsoluteOffset);
fn rollback_offset_allocation(&mut self, size: usize);
}
pub struct DefaultReservationAllocator {
next_reservation_id: ReservationId,
next_allocation_offset: AbsoluteOffset,
}
impl DefaultReservationAllocator {
pub fn new() -> Self {
DefaultReservationAllocator {
next_reservation_id: 0, next_allocation_offset: 0, }
}
}
impl ReservationAllocator for DefaultReservationAllocator {
fn next_reservation_details(&mut self, size: usize) -> (ReservationId, AbsoluteOffset) {
let reservation_id = self.next_reservation_id;
self.next_reservation_id += 1;
let offset = self.next_allocation_offset;
self.next_allocation_offset += size;
(reservation_id, offset)
}
fn rollback_offset_allocation(&mut self, size: usize) {
self.next_allocation_offset = self.next_allocation_offset.saturating_sub(size);
}
}