-
Notifications
You must be signed in to change notification settings - Fork 747
Allowed Process Slice Buffered API #4023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d79e214
first iteration of a ringbuffer api
alexandruradovici 7b2c829
Update kernel/src/processbuffer.rs
alexandruradovici ce6d6d1
updates
alexandruradovici 080bdf5
use a seperate structure and rename the buffer
alexandruradovici ea53b0f
format doc
alexandruradovici 27f578c
make clippy happy
alexandruradovici ecc265f
use constant
alexandruradovici dd553c3
use inspect_err
alexandruradovici File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,12 +26,18 @@ | |
|
||
use core::cell::Cell; | ||
use core::marker::PhantomData; | ||
use core::mem::size_of; | ||
use core::ops::{Deref, Index, Range, RangeFrom, RangeTo}; | ||
|
||
use crate::capabilities; | ||
use crate::process::{self, ProcessId}; | ||
use crate::ErrorCode; | ||
|
||
// the offset where the buffer data starts | ||
// - skip the flags field (1 byte) | ||
// - skip the len field (size_of::<u32>() bytes) | ||
const BUFFER_OFFSET: usize = 1 + size_of::<u32>(); | ||
|
||
/// Convert a process buffer's internal representation to a | ||
/// [`ReadableProcessSlice`]. | ||
/// | ||
|
@@ -886,6 +892,96 @@ pub struct WriteableProcessSlice { | |
slice: [Cell<u8>], | ||
} | ||
|
||
#[repr(transparent)] | ||
pub struct ProcessSliceBuffer<'a> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need comment. |
||
slice: &'a WriteableProcessSlice, | ||
} | ||
|
||
impl<'a> ProcessSliceBuffer<'a> { | ||
pub fn new(slice: &'a WriteableProcessSlice) -> ProcessSliceBuffer { | ||
ProcessSliceBuffer { slice } | ||
} | ||
|
||
/// Checks if the process buffer respects the buffer contract | ||
/// | ||
/// The buffer data format is described below: | ||
/// ```text,ignore | ||
/// 0 1 2 3 4 | ||
/// +----------+----------+----------+----------+-------------------... | ||
/// | flags | buffer length | slice | ||
/// +----------+----------+----------+----------+-------------------... | ||
/// | 00000000 | 32 bits little endian | data | ||
/// ``` | ||
/// | ||
/// - the first byte is reserved to store flags, for this version of the buffer | ||
/// the value has to be 0 | ||
/// - the next 4 bytes store the used space in a 32 bit little endian format | ||
/// | ||
/// This function fails with | ||
/// - `INVALID` if the flags field is not set to 0 | ||
/// - `SIZE` if the underlying slice is not large enough top fit the | ||
/// flags field and the len field (5 bytes) | pub fn len(&self) -> Result<usize, ErrorCode> { | |
// check if the slice can actually hold a buffer | ||
// - the slice has to be able to fit the flags (1 byte) and the size (4 bytes) | ||
if self.slice.len() >= BUFFER_OFFSET { | ||
if self.slice[0].get() == 0 { | ||
let len = self.slice[1].get() as u32 | ||
| (self.slice[2].get() as u32 >> 8) | ||
| (self.slice[3].get() as u32 >> 16) | ||
| (self.slice[4].get() as u32 >> 24); | ||
Ok(len as usize) | ||
} else { | ||
Err(ErrorCode::INVAL) | ||
} | ||
} else { | ||
Err(ErrorCode::SIZE) | ||
} | ||
} | ||
|
||
fn set_len(&self, len: usize) -> Result<(), ErrorCode> { | ||
if self.slice.len() >= len + BUFFER_OFFSET { | ||
// set the flags | ||
self.slice[0].set(0); | ||
|
||
// set the length | ||
self.slice[1].set((len & 0xff) as u8); | ||
self.slice[2].set(((len << 8) & 0xff) as u8); | ||
self.slice[3].set(((len << 16) & 0xff) as u8); | ||
self.slice[4].set(((len << 24) & 0xff) as u8); | ||
Ok(()) | ||
} else { | ||
Err(ErrorCode::SIZE) | ||
} | ||
} | ||
|
||
/// Append a slice of data to the buffer | ||
/// | ||
/// This function fails with: | ||
/// - `INVALID` - if the buffer's flags field is not set to 0 | ||
/// - `SIZE` - if the data slice is too large to fit into the buffer | ||
pub fn append(&self, data: &[u8]) -> Result<(), ErrorCode> { | ||
let len = self.len()?; | ||
if data.len() + len <= self.slice.len() - BUFFER_OFFSET { | ||
self.slice[BUFFER_OFFSET + len..BUFFER_OFFSET + len + data.len()].copy_from_slice(data); | ||
self.set_len(len + data.len())?; | ||
Ok(()) | ||
} else { | ||
Err(ErrorCode::SIZE) | ||
} | ||
} | ||
|
||
/// Resets the buffer's length t0 and flags to 0 | ||
/// | ||
/// This function fails with | ||
/// - `SIZE` - if the slice underneeths the buffer is too small to fit | ||
/// the flags field and tyhe len field (a total of 5 bytes) | ||
#[inline(always)] | ||
pub fn reset(&self) -> Result<(), ErrorCode> { | ||
self.set_len(0) | ||
} | ||
} | ||
|
||
fn cast_cell_slice_to_process_slice(cell_slice: &[Cell<u8>]) -> &WriteableProcessSlice { | ||
// # Safety | ||
// | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this into the impl ala https://github.com/tock/tock/blob/master/kernel/src/process_standard.rs#L1300?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm working on a small refactor that includes this change.