-
-
Notifications
You must be signed in to change notification settings - Fork 170
helpers: Add AlignedBuffer #1600
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
Merged
Merged
Changes from all commits
Commits
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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
use alloc::alloc::{alloc, dealloc, Layout, LayoutError}; | ||
use core::error::Error; | ||
use core::fmt; | ||
use core::ptr::NonNull; | ||
|
||
/// Helper class to maintain the lifetime of a memory region allocated with a non-standard alignment. | ||
/// Facilitates RAII to properly deallocate when lifetime of the object ends. | ||
/// | ||
/// Note: This uses the global Rust allocator under the hood. | ||
#[derive(Debug)] | ||
pub struct AlignedBuffer { | ||
ptr: NonNull<u8>, | ||
layout: Layout, | ||
} | ||
|
||
impl AlignedBuffer { | ||
/// Allocate a new memory region with the requested len and alignment. | ||
/// | ||
/// # Panics | ||
/// This method panics when the allocation fails (e.g. due to an out of memory situation). | ||
pub fn from_size_align(len: usize, alignment: usize) -> Result<Self, LayoutError> { | ||
seijikun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let layout = Layout::from_size_align(len, alignment)?; | ||
Ok(Self::from_layout(layout)) | ||
} | ||
|
||
/// Allocate a new memory region with the requested layout. | ||
/// | ||
/// # Panics | ||
/// This method panics when the allocation fails (e.g. due to an out of memory situation). | ||
#[must_use] | ||
pub fn from_layout(layout: Layout) -> Self { | ||
let ptr = unsafe { alloc(layout) }; | ||
seijikun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let ptr = NonNull::new(ptr).expect("Allocation failed"); | ||
seijikun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Self { ptr, layout } | ||
} | ||
|
||
// TODO: Add non-panicking method variants as soon as alloc::AllocError was stabilized (#32838). | ||
// - try_from_layout(layout: Layout) -> Result<Self, AllocError>; | ||
|
||
/// Get a pointer to the aligned memory region managed by this instance. | ||
#[must_use] | ||
pub const fn ptr(&self) -> *const u8 { | ||
self.ptr.as_ptr().cast_const() | ||
} | ||
|
||
/// Get a mutable pointer to the aligned memory region managed by this instance. | ||
#[must_use] | ||
pub fn ptr_mut(&mut self) -> *mut u8 { | ||
self.ptr.as_ptr() | ||
} | ||
|
||
/// Get the size of the aligned memory region managed by this instance. | ||
#[must_use] | ||
pub const fn size(&self) -> usize { | ||
self.layout.size() | ||
} | ||
|
||
/// Fill the aligned memory region with data from the given buffer. | ||
/// | ||
/// The length of `src` must be the same as `self`. | ||
pub fn copy_from_slice(&mut self, src: &[u8]) { | ||
assert_eq!(self.size(), src.len()); | ||
unsafe { | ||
self.ptr_mut().copy_from(src.as_ptr(), src.len()); | ||
} | ||
} | ||
|
||
/// Check the buffer's alignment against the `required_alignment`. | ||
pub fn check_alignment(&self, required_alignment: usize) -> Result<(), AlignmentError> { | ||
seijikun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//TODO: use bfr.addr() when it's available | ||
if (self.ptr() as usize) % required_alignment != 0 { | ||
return Err(AlignmentError); //TODO: use >is_aligned_to< when it's available | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Drop for AlignedBuffer { | ||
fn drop(&mut self) { | ||
unsafe { | ||
dealloc(self.ptr_mut(), self.layout); | ||
} | ||
} | ||
} | ||
|
||
/// The `AlignmentError` is returned if a user-provided buffer doesn't fulfill alignment requirements. | ||
#[derive(Clone, PartialEq, Eq, Debug)] | ||
pub struct AlignmentError; | ||
impl Error for AlignmentError {} | ||
impl fmt::Display for AlignmentError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str("Buffer alignment does not fulfill requirements.") | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::AlignedBuffer; | ||
|
||
#[test] | ||
fn test_invalid_arguments() { | ||
// invalid alignments, valid len | ||
for request_alignment in [0, 3, 5, 7, 9] { | ||
for request_len in [1, 32, 64, 128, 1024] { | ||
assert!(AlignedBuffer::from_size_align(request_len, request_alignment).is_err()); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_allocation_alignment() { | ||
for request_alignment in [1, 2, 4, 8, 16, 32, 64, 128] { | ||
for request_len in [1 as usize, 32, 64, 128, 1024] { | ||
let buffer = | ||
AlignedBuffer::from_size_align(request_len, request_alignment).unwrap(); | ||
assert_eq!(buffer.ptr() as usize % request_alignment, 0); | ||
assert_eq!(buffer.size(), request_len); | ||
} | ||
} | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.