-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41c446b
commit d583029
Showing
9 changed files
with
101 additions
and
60 deletions.
There are no files selected for viewing
This file contains 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 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 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,44 @@ | ||
pub fn alloc_aligned_byte_vec(size: usize, align: usize) -> Vec<u8> { | ||
unsafe { | ||
let buf_layout = std::alloc::Layout::from_size_align_unchecked(size, align); | ||
let buf_ptr = std::alloc::alloc(buf_layout); | ||
Vec::from_raw_parts(buf_ptr, size, size) | ||
} | ||
} | ||
|
||
pub fn fill_byte_slice(buf: &mut [u8], value: u8) { | ||
// unsafe { | ||
// std::libc::memset( | ||
// buf.as_mut_ptr() as _, | ||
// 0, | ||
// buf.len() | ||
// ); | ||
// }; | ||
buf.iter_mut().map(|x| *x = value).count(); //todo: rewrite | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_aligned_allocation() { | ||
let size = 65536; | ||
let align = 4096; | ||
|
||
let buf = alloc_aligned_byte_vec(size, align); | ||
|
||
assert_eq!(buf.len(), size); | ||
assert_eq!(buf.as_ptr() as usize % align, 0); | ||
} | ||
|
||
#[test] | ||
fn test_vec_fill() { | ||
let mut buf = vec![0x00; 1024]; | ||
|
||
fill_byte_slice(&mut buf, 0xff); | ||
|
||
assert_eq!(buf.iter().filter(|x| **x != 0xff).count(), 0); | ||
} | ||
|
||
} |
This file contains 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 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 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 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 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 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