Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,7 @@ pub mod arch {
#[stable(feature = "simd_x86", since = "1.27.0")]
pub use std_detect::is_x86_feature_detected;

// Platform-abstraction modules
mod sys;
mod sys_common;

pub mod alloc;

Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/args/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use crate::ffi::{OsStr, OsString};
use crate::num::NonZero;
use crate::os::windows::prelude::*;
use crate::path::{Path, PathBuf};
use crate::sys::helpers::WStrUnits;
use crate::sys::pal::os::current_exe;
use crate::sys::pal::{ensure_no_nuls, fill_utf16_buf};
use crate::sys::path::get_long_path;
use crate::sys::{AsInner, c, to_u16s};
use crate::sys_common::wstr::WStrUnits;
use crate::{io, iter, ptr};

pub fn args() -> Args {
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/configure_builtins.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//! The configure builtins provides runtime support compiler-builtin features
//! which require dynamic initialization to work as expected, e.g. aarch64
//! outline-atomics.

/// Enable LSE atomic operations at startup, if supported.
///
/// Linker sections are based on what [`ctor`] does, with priorities to run slightly before user
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/env/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::io;
use crate::os::raw::{c_char, c_int};
use crate::os::solid::ffi::{OsStrExt, OsStringExt};
use crate::sync::{PoisonError, RwLock};
use crate::sys::common::small_c_string::run_with_cstr;
use crate::sys::helpers::run_with_cstr;

static ENV_LOCK: RwLock<()> = RwLock::new(());

Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/env/uefi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod uefi_env {
use crate::io;
use crate::os::uefi::ffi::OsStringExt;
use crate::ptr::NonNull;
use crate::sys::{helpers, unsupported_err};
use crate::sys::pal::{helpers, unsupported_err};

pub(crate) fn get(key: &OsStr) -> Option<OsString> {
let shell = helpers::open_shell()?;
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/env/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::ffi::{CStr, OsStr, OsString};
use crate::io;
use crate::os::unix::prelude::*;
use crate::sync::{PoisonError, RwLock};
use crate::sys::common::small_c_string::run_with_cstr;
use crate::sys::cvt;
use crate::sys::helpers::run_with_cstr;

// Use `_NSGetEnviron` on Apple platforms.
//
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/env/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use super::common::Env;
use crate::ffi::{CStr, OsStr, OsString};
use crate::io;
use crate::os::wasi::prelude::*;
use crate::sys::common::small_c_string::run_with_cstr;
use crate::sys::helpers::run_with_cstr;
use crate::sys::pal::os::{cvt, libc};

cfg_select! {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::fs;
use crate::io::{self, Error, ErrorKind};
use crate::path::Path;
use crate::sys_common::ignore_notfound;
use crate::sys::helpers::ignore_notfound;

pub(crate) const NOT_FILE_ERROR: Error = io::const_error!(
ErrorKind::InvalidInput,
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/hermit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crate::os::hermit::hermit_abi::{
use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use crate::path::{Path, PathBuf};
use crate::sync::Arc;
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::fd::FileDesc;
pub use crate::sys::fs::common::{copy, exists};
use crate::sys::helpers::run_path_with_cstr;
use crate::sys::time::SystemTime;
use crate::sys::{AsInner, AsInnerMut, FromInner, IntoInner, cvt, unsupported, unsupported_err};
use crate::{fmt, mem};
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cfg_select! {
pub(crate) use unix::debug_assert_fd_is_open;
#[cfg(any(target_os = "linux", target_os = "android"))]
pub(super) use unix::CachedFileMetadata;
use crate::sys::common::small_c_string::run_path_with_cstr as with_native_path;
use crate::sys::helpers::run_path_with_cstr as with_native_path;
}
target_os = "windows" => {
mod windows;
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use crate::os::solid::ffi::OsStrExt;
use crate::path::{Path, PathBuf};
use crate::sync::Arc;
pub use crate::sys::fs::common::exists;
use crate::sys::helpers::ignore_notfound;
use crate::sys::pal::{abi, error};
use crate::sys::time::SystemTime;
use crate::sys::{unsupported, unsupported_err};
use crate::sys_common::ignore_notfound;

type CIntNotMinusOne = core::num::niche_types::NotAllOnes<c_int>;

Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sys/fs/uefi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::fs::TryLockError;
use crate::hash::Hash;
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
use crate::path::{Path, PathBuf};
use crate::sys::pal::{helpers, unsupported};
use crate::sys::time::SystemTime;
use crate::sys::{helpers, unsupported};

#[expect(dead_code)]
const FILE_PERMISSIONS_MASK: u64 = r_efi::protocols::file::READ_ONLY;
Expand Down Expand Up @@ -440,7 +440,7 @@ mod uefi_fs {
use crate::io;
use crate::path::Path;
use crate::ptr::NonNull;
use crate::sys::helpers::{self, UefiBox};
use crate::sys::pal::helpers::{self, UefiBox};
use crate::sys::time::{self, SystemTime};

pub(crate) struct File(NonNull<file::Protocol>);
Expand Down
5 changes: 2 additions & 3 deletions library/std/src/sys/fs/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ use crate::os::unix::prelude::*;
use crate::os::wasi::prelude::*;
use crate::path::{Path, PathBuf};
use crate::sync::Arc;
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::fd::FileDesc;
pub use crate::sys::fs::common::exists;
use crate::sys::helpers::run_path_with_cstr;
use crate::sys::time::SystemTime;
#[cfg(all(target_os = "linux", target_env = "gnu"))]
use crate::sys::weak::syscall;
Expand Down Expand Up @@ -2488,9 +2488,8 @@ mod remove_dir_impl {
use crate::ffi::CStr;
use crate::io;
use crate::path::{Path, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::helpers::{ignore_notfound, run_path_with_cstr};
use crate::sys::{cvt, cvt_r};
use crate::sys_common::ignore_notfound;

pub fn openat_nofollow_dironly(parent_fd: Option<RawFd>, p: &CStr) -> io::Result<OwnedFd> {
let fd = cvt_r(|| unsafe {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/vexos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::fs::TryLockError;
use crate::hash::Hash;
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
use crate::path::{Path, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::helpers::run_path_with_cstr;
use crate::sys::time::SystemTime;
use crate::sys::{unsupported, unsupported_err};

Expand Down
38 changes: 38 additions & 0 deletions library/std/src/sys/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! Small helper functions used inside `sys`.
//!
//! If any of these have uses outside of `sys`, please move them to a different
//! module.

#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
mod small_c_string;
#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
mod wstr;

#[cfg(test)]
mod tests;

#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
pub use small_c_string::{run_path_with_cstr, run_with_cstr};
#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
pub use wstr::WStrUnits;

// Computes (value*numerator)/denom without overflow, as long as both (numerator*denom) and the
// overall result fit into i64 (which is the case for our time conversions).
Comment on lines +19 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Computes (value*numerator)/denom without overflow, as long as both (numerator*denom) and the
// overall result fit into i64 (which is the case for our time conversions).
/// Computes (value*numerator)/denom without overflow, as long as both (numerator*denom) and the
/// overall result fit into i64 (which is the case for our time conversions).

?

#[cfg_attr(not(target_os = "windows"), allow(unused))] // Not used on all platforms.
pub fn mul_div_u64(value: u64, numerator: u64, denom: u64) -> u64 {
let q = value / denom;
let r = value % denom;
// Decompose value as (value/denom*denom + value%denom),
// substitute into (value*numerator)/denom and simplify.
// r < denom, so (denom*numerator) is the upper bound of (r*numerator)
q * numerator + r * numerator / denom
}

#[cfg_attr(not(target_os = "linux"), allow(unused))] // Not used on all platforms.
pub fn ignore_notfound<T>(result: crate::io::Result<T>) -> crate::io::Result<()> {
match result {
Err(err) if err.kind() == crate::io::ErrorKind::NotFound => Ok(()),
Ok(_) => Ok(()),
Err(err) => Err(err),
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use core::iter::repeat;

use super::mul_div_u64;
use super::small_c_string::run_path_with_cstr;
use crate::ffi::CString;
use crate::hint::black_box;
use crate::path::Path;
use crate::sys::common::small_c_string::run_path_with_cstr;

#[test]
fn stack_allocation_works() {
Expand Down Expand Up @@ -65,3 +66,8 @@ fn bench_heap_path_alloc(b: &mut test::Bencher) {
.unwrap();
});
}

#[test]
fn test_muldiv() {
assert_eq!(mul_div_u64(1_000_000_000_001, 1_000_000_000, 1_000_000), 1_000_000_000_001_000);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! This module contains constructs to work with 16-bit characters (UCS-2 or UTF-16)
#![allow(dead_code)]

use crate::marker::PhantomData;
use crate::num::NonZero;
Expand Down
11 changes: 2 additions & 9 deletions library/std/src/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
#![allow(unsafe_op_in_unsafe_fn)]

/// The configure builtins provides runtime support compiler-builtin features
/// which require dynamic initialization to work as expected, e.g. aarch64
/// outline-atomics.
mod alloc;
mod configure_builtins;

/// The PAL (platform abstraction layer) contains platform-specific abstractions
/// for implementing the features in the other submodules, e.g. UNIX file
/// descriptors.
mod helpers;
mod pal;

mod alloc;
mod personality;

pub mod args;
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/net/connection/socket/hermit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ impl Socket {
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}

// This is used by sys_common code to abstract over Windows and Unix.
pub fn as_raw(&self) -> RawFd {
self.0.as_raw_fd()
}
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/net/connection/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::mem::MaybeUninit;
use crate::net::{
Ipv4Addr, Ipv6Addr, Shutdown, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs,
};
use crate::sys::common::small_c_string::run_with_cstr;
use crate::sys::helpers::run_with_cstr;
use crate::sys::net::connection::each_addr;
use crate::sys::{AsInner, FromInner};
use crate::time::Duration;
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/net/connection/socket/solid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ impl Socket {
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}

// This method is used by sys_common code to abstract over targets.
pub fn as_raw(&self) -> c_int {
self.as_raw_fd()
}
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/net/connection/socket/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,6 @@ impl Socket {
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}

// This is used by sys_common code to abstract over Windows and Unix.
pub fn as_raw(&self) -> RawFd {
self.as_raw_fd()
}
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/net/connection/socket/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@ impl Socket {
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
}

// This is used by sys_common code to abstract over Windows and Unix.
pub fn as_raw(&self) -> c::SOCKET {
debug_assert_eq!(size_of::<c::SOCKET>(), size_of::<RawSocket>());
debug_assert_eq!(align_of::<c::SOCKET>(), align_of::<RawSocket>());
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/net/connection/uefi/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::tcp4;
use crate::io::{self, IoSlice, IoSliceMut};
use crate::net::SocketAddr;
use crate::ptr::NonNull;
use crate::sys::{helpers, unsupported};
use crate::sys::pal::{helpers, unsupported};
use crate::time::Duration;

pub(crate) enum Tcp {
Expand Down
16 changes: 0 additions & 16 deletions library/std/src/sys/pal/common/mod.rs

This file was deleted.

25 changes: 2 additions & 23 deletions library/std/src/sys/pal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
//! Platform-dependent platform abstraction.
//!
//! The `std::sys` module is the abstracted interface through which
//! `std` talks to the underlying operating system. It has different
//! implementations for different operating system families, today
//! just Unix and Windows, and initial support for Redox.
//!
//! The centralization of platform-specific code in this module is
//! enforced by the "platform abstraction layer" tidy script in
//! `tools/tidy/src/pal.rs`.
//!
//! This module is closely related to the platform-independent system
//! integration code in `std::sys_common`. See that module's
//! documentation for details.
//!
//! In the future it would be desirable for the independent
//! implementations of this module to be extracted to their own crates
//! that `std` can link to, thus enabling their implementation
//! out-of-tree via crate replacement. Though due to the complex
//! inter-dependencies within `std` that will be a challenging goal to
//! achieve.
//! The PAL (platform abstraction layer) contains platform-specific abstractions
//! for implementing the features in the other submodules, such as e.g. bindings.

#![allow(missing_debug_implementations)]

pub mod common;

cfg_select! {
unix => {
mod unix;
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/uefi/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::path::Path;
use crate::ptr::NonNull;
use crate::slice;
use crate::sync::atomic::{Atomic, AtomicPtr, Ordering};
use crate::sys_common::wstr::WStrUnits;
use crate::sys::helpers::WStrUnits;

type BootInstallMultipleProtocolInterfaces =
unsafe extern "efiapi" fn(_: *mut r_efi::efi::Handle, _: ...) -> r_efi::efi::Status;
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/uefi/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ pub(crate) mod instant_internal {
use crate::mem::MaybeUninit;
use crate::ptr::NonNull;
use crate::sync::atomic::{Atomic, AtomicPtr, Ordering};
use crate::sys_common::mul_div_u64;
use crate::sys::helpers::mul_div_u64;

const NS_PER_SEC: u64 = 1_000_000_000;

Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use libc::{c_char, c_int, c_void};
use crate::ffi::{CStr, OsStr, OsString};
use crate::os::unix::prelude::*;
use crate::path::{self, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::cvt;
use crate::sys::helpers::run_path_with_cstr;
use crate::{fmt, io, iter, mem, ptr, slice, str};

const TMPBUF_SZ: usize = 128;
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/wasi/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::ffi::{CStr, OsStr, OsString};
use crate::marker::PhantomData;
use crate::os::wasi::prelude::*;
use crate::path::{self, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::helpers::run_path_with_cstr;
use crate::sys::unsupported;
use crate::{fmt, io, str};

Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/pal/windows/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ fn intervals2dur(intervals: u64) -> Duration {
mod perf_counter {
use super::NANOS_PER_SEC;
use crate::sync::atomic::{Atomic, AtomicU64, Ordering};
use crate::sys::helpers::mul_div_u64;
use crate::sys::{c, cvt};
use crate::sys_common::mul_div_u64;
use crate::time::Duration;

pub struct PerformanceCounterInstant {
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/path/cygwin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::ffi::OsString;
use crate::os::unix::ffi::OsStringExt;
use crate::path::{Path, PathBuf};
use crate::sys::common::small_c_string::run_path_with_cstr;
use crate::sys::cvt;
use crate::sys::helpers::run_path_with_cstr;
use crate::{io, ptr};

#[inline]
Expand Down
Loading
Loading