Skip to content

Commit

Permalink
dio fallback on mac
Browse files Browse the repository at this point in the history
Signed-off-by: coldWater <[email protected]>
  • Loading branch information
forsaken628 committed Oct 9, 2024
1 parent 0c550c5 commit 4e7c35e
Showing 1 changed file with 57 additions and 20 deletions.
77 changes: 57 additions & 20 deletions src/common/base/src/base/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use std::path::Path;
use std::ptr::Alignment;
use std::ptr::NonNull;

use rustix::fs::OFlags;
use tokio::fs::File;
use tokio::io::AsyncSeekExt;

Expand Down Expand Up @@ -109,31 +108,69 @@ struct DmaFile {
buf: Option<DmaBuffer>,
}

impl DmaFile {
/// Attempts to open a file in read-only mode.
async fn open(path: impl AsRef<Path>) -> io::Result<DmaFile> {
let file = File::options()
.read(true)
.custom_flags(OFlags::DIRECT.bits() as i32)
.open(path)
.await?;
#[cfg(target_os = "linux")]
pub mod linux {
use rustix::fs::OFlags;

use super::*;

impl DmaFile {
/// Attempts to open a file in read-only mode.
pub(super) async fn open(path: impl AsRef<Path>) -> io::Result<DmaFile> {
let file = File::options()
.read(true)
.custom_flags(OFlags::DIRECT.bits() as i32)
.open(path)
.await?;

open_dma(file).await
open_dma(file).await
}

/// Opens a file in write-only mode.
pub(super) async fn create(path: impl AsRef<Path>) -> io::Result<DmaFile> {
let file = File::options()
.write(true)
.create(true)
.truncate(true)
.custom_flags((OFlags::DIRECT | OFlags::EXCL).bits() as i32)
.open(path)
.await?;

open_dma(file).await
}
}
}

/// Opens a file in write-only mode.
async fn create(path: impl AsRef<Path>) -> io::Result<DmaFile> {
let file = File::options()
.write(true)
.create(true)
.truncate(true)
.custom_flags((OFlags::DIRECT | OFlags::EXCL).bits() as i32)
.open(path)
.await?;
#[cfg(not(target_os = "linux"))]
pub mod not_linux {
use rustix::fs::OFlags;

use super::*;

open_dma(file).await
impl DmaFile {
/// Attempts to open a file in read-only mode.
pub(super) async fn open(path: impl AsRef<Path>) -> io::Result<DmaFile> {
let file = File::options().read(true).open(path).await?;

open_dma(file).await
}

/// Opens a file in write-only mode.
pub(super) async fn create(path: impl AsRef<Path>) -> io::Result<DmaFile> {
let file = File::options()
.write(true)
.create(true)
.truncate(true)
.custom_flags(OFlags::EXCL.bits() as i32)
.open(path)
.await?;

open_dma(file).await
}
}
}

impl DmaFile {
fn set_buffer(&mut self, buf: DmaBuffer) {
self.buf = Some(buf)
}
Expand Down

0 comments on commit 4e7c35e

Please sign in to comment.