Skip to content

Commit eb0d413

Browse files
committed
Replace uses of std with libc
1 parent 0ba9870 commit eb0d413

File tree

9 files changed

+59
-48
lines changed

9 files changed

+59
-48
lines changed

Diff for: crates/std_detect/src/detect/cache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
44
#![allow(dead_code)] // not used on all platforms
55

6-
use crate::sync::atomic::Ordering;
6+
use core::sync::atomic::Ordering;
77

8-
use crate::sync::atomic::AtomicUsize;
8+
use core::sync::atomic::AtomicUsize;
99

1010
/// Sets the `bit` of `x`.
1111
#[inline]

Diff for: crates/std_detect/src/detect/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub fn features() -> impl Iterator<Item = (&'static str, bool)> {
140140
target_arch = "mips64",
141141
))] {
142142
(0_u8..Feature::_last as u8).map(|discriminant: u8| {
143-
let f: Feature = unsafe { crate::mem::transmute(discriminant) };
143+
let f: Feature = unsafe { core::mem::transmute(discriminant) };
144144
let name: &'static str = f.to_str();
145145
let enabled: bool = check_for(f);
146146
(name, enabled)

Diff for: crates/std_detect/src/detect/os/freebsd/auxvec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
4242

4343
/// Tries to read the `key` from the auxiliary vector.
4444
fn archauxv(key: usize) -> Result<usize, ()> {
45-
use crate::mem;
45+
use core::mem;
4646

4747
#[derive(Copy, Clone)]
4848
#[repr(C)]

Diff for: crates/std_detect/src/detect/os/linux/auxvec.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Parses ELF auxiliary vectors.
22
#![cfg_attr(not(target_arch = "aarch64"), allow(dead_code))]
33

4-
#[cfg(feature = "std_detect_file_io")]
5-
use crate::{fs::File, io::Read};
6-
74
/// Key to access the CPU Hardware capabilities bitfield.
85
pub(crate) const AT_HWCAP: usize = 16;
96
/// Key to access the CPU Hardware capabilities 2 bitfield.
@@ -139,7 +136,7 @@ fn getauxval(key: usize) -> Result<usize, ()> {
139136
return Err(());
140137
}
141138

142-
let ffi_getauxval: F = crate::mem::transmute(ptr);
139+
let ffi_getauxval: F = core::mem::transmute(ptr);
143140
Ok(ffi_getauxval(key))
144141
}
145142
}
@@ -148,19 +145,19 @@ fn getauxval(key: usize) -> Result<usize, ()> {
148145
/// function returns `Err`.
149146
#[cfg(feature = "std_detect_file_io")]
150147
fn auxv_from_file(file: &str) -> Result<AuxVec, ()> {
151-
let mut file = File::open(file).map_err(|_| ())?;
148+
let file = super::read_file(file)?;
152149

153150
// See <https://github.com/torvalds/linux/blob/v3.19/include/uapi/linux/auxvec.h>.
154151
//
155152
// The auxiliary vector contains at most 32 (key,value) fields: from
156153
// `AT_EXECFN = 31` to `AT_NULL = 0`. That is, a buffer of
157154
// 2*32 `usize` elements is enough to read the whole vector.
158155
let mut buf = [0_usize; 64];
159-
{
160-
let raw: &mut [u8; 64 * crate::mem::size_of::<usize>()] =
161-
unsafe { crate::mem::transmute(&mut buf) };
162-
file.read(raw).map_err(|_| ())?;
156+
let len = core::mem::size_of_val(&buf).max(file.len());
157+
unsafe {
158+
core::ptr::copy_nonoverlapping(file.as_ptr(), buf.as_mut_ptr() as *mut u8, len);
163159
}
160+
164161
auxv_from_buf(&buf)
165162
}
166163

Diff for: crates/std_detect/src/detect/os/linux/cpuinfo.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//! Parses /proc/cpuinfo
22
#![cfg_attr(not(target_arch = "arm"), allow(dead_code))]
33

4-
extern crate std;
5-
use self::std::{fs::File, io, io::Read, prelude::v1::*};
4+
use alloc::string::String;
65

76
/// cpuinfo
87
pub(crate) struct CpuInfo {
@@ -11,11 +10,11 @@ pub(crate) struct CpuInfo {
1110

1211
impl CpuInfo {
1312
/// Reads /proc/cpuinfo into CpuInfo.
14-
pub(crate) fn new() -> Result<Self, io::Error> {
15-
let mut file = File::open("/proc/cpuinfo")?;
16-
let mut cpui = Self { raw: String::new() };
17-
file.read_to_string(&mut cpui.raw)?;
18-
Ok(cpui)
13+
pub(crate) fn new() -> Result<Self, ()> {
14+
let raw = super::read_file("/proc/cpuinfo")?;
15+
Ok(Self {
16+
raw: String::from_utf8(raw).map_err(|_| ())?,
17+
})
1918
}
2019
/// Returns the value of the cpuinfo `field`.
2120
pub(crate) fn field(&self, field: &str) -> CpuInfoField {
@@ -34,7 +33,7 @@ impl CpuInfo {
3433
}
3534

3635
#[cfg(test)]
37-
fn from_str(other: &str) -> Result<Self, ::std::io::Error> {
36+
fn from_str(other: &str) -> Result<Self, ()> {
3837
Ok(Self {
3938
raw: String::from(other),
4039
})

Diff for: crates/std_detect/src/detect/os/linux/mod.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
11
//! Run-time feature detection on Linux
2+
//!
3+
#[cfg(feature = "std_detect_file_io")]
4+
use alloc::vec::Vec;
25

36
mod auxvec;
47

58
#[cfg(feature = "std_detect_file_io")]
69
mod cpuinfo;
710

11+
#[cfg(feature = "std_detect_file_io")]
12+
fn read_file(path: &str) -> Result<Vec<u8>, ()> {
13+
let mut path = Vec::from(path.as_bytes());
14+
path.push(0);
15+
16+
unsafe {
17+
let file = libc::open(path.as_ptr() as *const libc::c_char, libc::O_RDONLY);
18+
if file == -1 {
19+
return Err(());
20+
}
21+
22+
let mut data = Vec::new();
23+
loop {
24+
data.reserve(4096);
25+
let spare = data.spare_capacity_mut();
26+
match libc::read(file, spare.as_mut_ptr() as *mut _, spare.len()) {
27+
-1 => {
28+
libc::close(file);
29+
return Err(());
30+
}
31+
0 => break,
32+
n => data.set_len(data.len() + n as usize),
33+
}
34+
}
35+
36+
libc::close(file);
37+
Ok(data)
38+
}
39+
}
40+
841
cfg_if::cfg_if! {
942
if #[cfg(target_arch = "aarch64")] {
1043
mod aarch64;

Diff for: crates/std_detect/src/detect/os/x86.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! x86 run-time feature detection is OS independent.
22
33
#[cfg(target_arch = "x86")]
4-
use crate::arch::x86::*;
4+
use core::arch::x86::*;
55
#[cfg(target_arch = "x86_64")]
6-
use crate::arch::x86_64::*;
6+
use core::arch::x86_64::*;
77

8-
use crate::mem;
8+
use core::mem;
99

1010
use crate::detect::{bit, cache, Feature};
1111

Diff for: crates/std_detect/src/lib.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,17 @@
1515
#![feature(const_fn, staged_api, stdsimd, doc_cfg, allow_internal_unstable)]
1616
#![allow(clippy::shadow_reuse)]
1717
#![deny(clippy::missing_inline_in_public_items)]
18-
#![cfg_attr(target_os = "linux", feature(linkage))]
1918
#![cfg_attr(all(target_os = "freebsd", target_arch = "aarch64"), feature(llvm_asm))]
2019
#![cfg_attr(test, allow(unused_imports))]
20+
#![cfg_attr(feature = "std_detect_file_io", feature(vec_spare_capacity))]
2121
#![no_std]
2222

23-
cfg_if::cfg_if! {
24-
if #[cfg(feature = "std_detect_file_io")] {
25-
#[cfg_attr(test, macro_use(println))]
26-
extern crate std;
23+
#[cfg(feature = "std_detect_file_io")]
24+
extern crate alloc;
2725

28-
#[allow(unused_imports)]
29-
use std::{arch, env, fs, io, mem, sync};
30-
} else {
31-
#[cfg(test)]
32-
#[macro_use(println)]
33-
extern crate std;
34-
35-
#[allow(unused_imports)]
36-
use core::{arch, mem, sync};
37-
}
38-
}
39-
40-
#[cfg(feature = "std_detect_dlsym_getauxval")]
41-
extern crate libc;
26+
#[cfg(test)]
27+
#[macro_use]
28+
extern crate std;
4229

4330
#[doc(hidden)]
4431
#[unstable(feature = "stdsimd", issue = "27731")]

Diff for: crates/std_detect/src/mod.rs

-5
This file was deleted.

0 commit comments

Comments
 (0)