Skip to content

Commit

Permalink
Replace manual errno ffi with errno crate
Browse files Browse the repository at this point in the history
  • Loading branch information
js2xxx committed Dec 2, 2024
1 parent 82df4e1 commit 972e9f9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 75 deletions.
23 changes: 12 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repository = "https://github.com/Js2xxx/ferroc"
version = "1.0.0-pre.4"

[features]
base-mmap = ["dep:memmap2", "dep:libc"]
base-mmap = ["dep:memmap2", "dep:libc", "dep:errno"]
base-static = []
c = ["default"]
c-bindgen = ["c", "dep:cbindgen"]
Expand All @@ -29,6 +29,7 @@ track-valgrind = ["dep:crabgrind"]
[dependencies]
array-macro = "2.1"
crabgrind = {version = "0.1", optional = true}
errno = {version = "0.3", optional = true}
libc = {version = "0.2", optional = true}
log = {version = "0.4", optional = true}
memmap2 = {version = "0.9", optional = true}
Expand Down
77 changes: 14 additions & 63 deletions src/base/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ impl Mmap {
}
}

// Set as `std::io::RawOsError` so as to get rid of direct `std` dependency.
#[cfg(not(target_os = "uefi"))]
type RawOsError = i32;
#[cfg(target_os = "uefi")]
type RawOsError = usize;

unsafe impl BaseAlloc for Mmap {
const IS_ZEROED: bool = true;

type Error = RawOsError;
// The inner type of `Errno` is `i32`, which is incompatible for UEFI targets.
// However, the `mmap` functionalities aren't available on UEFI either, so this
// should be fine.
type Error = errno::Errno;

type Handle = ManuallyDrop<MmapMut>;

Expand All @@ -43,21 +40,21 @@ unsafe impl BaseAlloc for Mmap {
if cfg!(not(miri)) && commit {
options.populate();
}
let mut trial = options
.len(layout.size())
.map_anon()
.map_err(|err| err.raw_os_error().unwrap())?;

let mut mmap = |len| {
let res = options.len(len).map_anon();
res.map_err(|err| errno::Errno(err.raw_os_error().unwrap()))
};

let mut trial = mmap(layout.size())?;
if trial.as_ptr().is_aligned_to(layout.align()) {
let ptr = NonNull::new(trial.as_mut_ptr()).unwrap();
// SAFETY: `Chunk` is allocated from self.
return Ok(unsafe { Chunk::new(ptr, layout, ManuallyDrop::new(trial)) });
}

drop(trial);
let mut a = options
.len(layout.size() + layout.align())
.map_anon()
.map_err(|err| err.raw_os_error().unwrap())?;

let mut a = mmap(layout.size() + layout.align())?;
let ptr = NonNull::new(a.as_mut_ptr().map_addr(|addr| round_up(addr, layout)));

// SAFETY: `Chunk` is allocated from self.
Expand All @@ -78,7 +75,7 @@ unsafe impl BaseAlloc for Mmap {
// SAFETY: The corresponding memory area is going to be used.
match unsafe { libc::madvise(ptr.as_ptr().cast(), len, flags) } {
0 => Ok(()),
_ => Err(errno()),
_ => Err(errno::errno()),
}
}

Expand All @@ -89,49 +86,3 @@ unsafe impl BaseAlloc for Mmap {
unsafe { libc::madvise(ptr.as_ptr().cast(), len, libc::MADV_DONTNEED) };
}
}

#[cfg(all(unix, not(miri)))]
/// Returns the platform-specific value of errno
pub fn errno() -> RawOsError {
unsafe extern "C" {
#[cfg_attr(
any(
target_os = "linux",
target_os = "emscripten",
target_os = "fuchsia",
target_os = "l4re",
target_os = "hurd",
),
link_name = "__errno_location"
)]
#[cfg_attr(
any(
target_os = "netbsd",
target_os = "openbsd",
target_os = "android",
target_os = "redox",
target_env = "newlib"
),
link_name = "__errno"
)]
#[cfg_attr(
any(target_os = "solaris", target_os = "illumos"),
link_name = "___errno"
)]
#[cfg_attr(target_os = "nto", link_name = "__get_errno_ptr")]
#[cfg_attr(
any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "freebsd",
target_os = "watchos"
),
link_name = "__error"
)]
#[cfg_attr(target_os = "haiku", link_name = "_errnop")]
#[cfg_attr(target_os = "aix", link_name = "_Errno")]
fn errno_location() -> *mut RawOsError;
}
unsafe { *errno_location() }
}

0 comments on commit 972e9f9

Please sign in to comment.