Skip to content
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

Please allow to construct BytesMut with custom alignment #600

Closed
Wireless4024 opened this issue Feb 22, 2023 · 1 comment
Closed

Please allow to construct BytesMut with custom alignment #600

Wireless4024 opened this issue Feb 22, 2023 · 1 comment

Comments

@Wireless4024
Copy link

I need custom alignment when reading file with O_DIRECT but I didn't see any public api that capable to allocate BytesMut with custom alignment (e.g. 4096).
I did have my api to allocate Vec<u8> with custom alignment but BytesMut::from_vec is private :(

@Wireless4024
Copy link
Author

Here's snippet if someone using O_DIRECT with bytes

let path = "Cargo.lock";
let meta = metadata(path)?;
let mut bytes = BytesMut::with_capacity(meta.st_blksize() * 2); // minimum is st_blksize+st_blksize
let ptr = bytes.as_ptr() as usize;
let blksz = (meta.st_blksize() - 1) as usize;
let shift = (ptr + blksz) & !blksz; // round it up
let blksz = meta.st_blksize() as usize;
let mut bytes = bytes.split_off(shift - ptr);
let _ = bytes.split_off(blksz);
let file = OpenOptions::new()
	.read(true)
	.custom_flags(libc::O_DIRECT)
	.open(path)?;
let file = tokio_uring::fs::File::from_std(file);
let (len, bytes) = file.read_at(bytes, 0).await;
let len = len?;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant