ableos/kernel/src/ipc/buffer.rs
koniifer 3df6f18c85 fix buffers
update hblang
auto cpu feature detection for repbuild
less verbose buffer api
add peony's tetris stub
2024-10-12 13:35:49 +01:00

73 lines
1.9 KiB
Rust

//!
use {
super::{message::Message, protocol::Protocol},
crossbeam_queue::{ArrayQueue, SegQueue},
};
pub enum BufferTypes {
Unbound(SegQueue<Message>),
Bound(ArrayQueue<Message>),
}
/// Interproccess buffer
pub struct IpcBuffer<'a> {
pub protocol: Protocol<'a>,
pub buffer: BufferTypes,
}
impl<'a> IpcBuffer<'a> {
pub fn new(bounded: bool, length: u64) -> Self {
log::debug!(
"New IPCBuffer\r
bounded: {}\r
length: {:?}\r",
bounded,
length
);
match (bounded, length) {
(false, ..) => {
let buftype = BufferTypes::Unbound(SegQueue::new());
Self {
protocol: Protocol::void(),
buffer: buftype,
}
}
(true, length) => {
let buftype = BufferTypes::Bound(ArrayQueue::new(length as usize));
Self {
protocol: Protocol::void(),
buffer: buftype,
}
}
}
}
/// Validate a message to match the `IPC.protocol`
pub fn validate_messages(&mut self) -> Result<(), IpcError> {
Ok(())
}
pub fn push(&mut self, msg: Message) {
match &self.buffer {
BufferTypes::Unbound(buff) => buff.push(msg),
BufferTypes::Bound(buff) => buff.push(msg).unwrap(),
};
}
pub fn pop(&mut self) -> Result<Message, IpcError> {
let msg = match &self.buffer {
BufferTypes::Unbound(buff) => buff.pop(),
BufferTypes::Bound(buff) => buff.pop(),
};
match msg {
Some(msg) => return Ok(msg),
None => return Err(IpcError::NoMessagesInBuffer),
}
}
}
/// Interprocess Communication Errors
#[derive(Debug)]
pub enum IpcError {
/// An invalid message error returned to the sender
InvalidMessage,
NoMessagesInBuffer,
}