Skip to content

Commit 2f42ac4

Browse files
committed
std: Remove rust_builtin C support library
All these definitions can now be written in Rust, so do so!
1 parent 2343a92 commit 2f42ac4

File tree

9 files changed

+156
-666
lines changed

9 files changed

+156
-666
lines changed

mk/crates.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ DEPS_rustc_bitflags := core
7171
DEPS_rustc_unicode := core
7272

7373
DEPS_std := core libc rand alloc collections rustc_unicode \
74-
native:rust_builtin native:backtrace \
74+
native:backtrace \
7575
alloc_system
7676
DEPS_arena := std
7777
DEPS_glob := std

mk/rt.mk

+1-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# that's per-target so you're allowed to conditionally add files based on the
3636
# target.
3737
################################################################################
38-
NATIVE_LIBS := rust_builtin hoedown miniz rust_test_helpers
38+
NATIVE_LIBS := hoedown miniz rust_test_helpers
3939

4040
# $(1) is the target triple
4141
define NATIVE_LIBRARIES
@@ -50,8 +50,6 @@ NATIVE_DEPS_hoedown_$(1) := hoedown/src/autolink.c \
5050
hoedown/src/stack.c \
5151
hoedown/src/version.c
5252
NATIVE_DEPS_miniz_$(1) = miniz.c
53-
NATIVE_DEPS_rust_builtin_$(1) := rust_builtin.c \
54-
rust_android_dummy.c
5553
NATIVE_DEPS_rust_test_helpers_$(1) := rust_test_helpers.c
5654

5755
################################################################################

src/liballoc_jemalloc/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,14 @@ pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
108108
let flags = align_to_flags(align);
109109
unsafe { je_nallocx(size as size_t, flags) as usize }
110110
}
111+
112+
// These symbols are used by jemalloc on android but the really old android
113+
// we're building on doesn't have them defined, so just make sure the symbols
114+
// are available.
115+
#[no_mangle]
116+
#[cfg(target_os = "android")]
117+
pub extern fn pthread_atfork(_prefork: *mut u8,
118+
_postfork_parent: *mut u8,
119+
_postfork_child: *mut u8) -> i32 {
120+
0
121+
}

src/libstd/rtdeps.rs

-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
//! the standard library This varies per-platform, but these libraries are
1313
//! necessary for running libstd.
1414
15-
// A few small shims in C that haven't been translated to Rust yet
16-
#[cfg(all(not(test), not(windows)))]
17-
#[link(name = "rust_builtin", kind = "static")]
18-
extern {}
19-
2015
// LLVM implements the `frem` instruction as a call to `fmod`, which lives in
2116
// libm. Hence, we must explicitly link to it.
2217
//

src/libstd/sys/unix/fs.rs

+65-45
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use os::unix::prelude::*;
1414
use ffi::{CString, CStr, OsString, OsStr};
1515
use fmt;
1616
use io::{self, Error, ErrorKind, SeekFrom};
17-
use libc::{self, c_int, off_t, c_char, mode_t};
17+
use libc::{dirent, readdir_r};
18+
use libc::{self, c_int, off_t, mode_t};
1819
use mem;
1920
use path::{Path, PathBuf};
2021
use ptr;
@@ -43,7 +44,7 @@ unsafe impl Send for Dir {}
4344
unsafe impl Sync for Dir {}
4445

4546
pub struct DirEntry {
46-
buf: Vec<u8>, // actually *mut libc::dirent
47+
entry: dirent,
4748
root: Arc<PathBuf>,
4849
}
4950

@@ -126,32 +127,22 @@ impl Iterator for ReadDir {
126127
type Item = io::Result<DirEntry>;
127128

128129
fn next(&mut self) -> Option<io::Result<DirEntry>> {
129-
extern {
130-
fn rust_dirent_t_size() -> libc::size_t;
131-
}
132-
133-
let mut buf: Vec<u8> = Vec::with_capacity(unsafe {
134-
rust_dirent_t_size()
135-
});
136-
let ptr = buf.as_mut_ptr() as *mut libc::dirent;
137-
138-
let mut entry_ptr = ptr::null_mut();
139-
loop {
140-
if unsafe { libc::readdir_r(self.dirp.0, ptr, &mut entry_ptr) != 0 } {
141-
return Some(Err(Error::last_os_error()))
142-
}
143-
if entry_ptr.is_null() {
144-
return None
145-
}
146-
147-
let entry = DirEntry {
148-
buf: buf,
130+
unsafe {
131+
let mut ret = DirEntry {
132+
entry: mem::zeroed(),
149133
root: self.root.clone()
150134
};
151-
if entry.name_bytes() == b"." || entry.name_bytes() == b".." {
152-
buf = entry.buf;
153-
} else {
154-
return Some(Ok(entry))
135+
let mut entry_ptr = ptr::null_mut();
136+
loop {
137+
if readdir_r(self.dirp.0, &mut ret.entry, &mut entry_ptr) != 0 {
138+
return Some(Err(Error::last_os_error()))
139+
}
140+
if entry_ptr.is_null() {
141+
return None
142+
}
143+
if ret.name_bytes() != b"." && ret.name_bytes() != b".." {
144+
return Some(Ok(ret))
145+
}
155146
}
156147
}
157148
}
@@ -166,7 +157,7 @@ impl Drop for Dir {
166157

167158
impl DirEntry {
168159
pub fn path(&self) -> PathBuf {
169-
self.root.join(<OsStr as OsStrExt>::from_bytes(self.name_bytes()))
160+
self.root.join(OsStr::from_bytes(self.name_bytes()))
170161
}
171162

172163
pub fn file_name(&self) -> OsString {
@@ -178,35 +169,64 @@ impl DirEntry {
178169
}
179170

180171
pub fn file_type(&self) -> io::Result<FileType> {
181-
extern {
182-
fn rust_dir_get_mode(ptr: *mut libc::dirent) -> c_int;
183-
}
184-
unsafe {
185-
match rust_dir_get_mode(self.dirent()) {
186-
-1 => lstat(&self.path()).map(|m| m.file_type()),
187-
n => Ok(FileType { mode: n as mode_t }),
188-
}
172+
match self.entry.d_type {
173+
libc::DT_CHR => Ok(FileType { mode: libc::S_IFCHR }),
174+
libc::DT_FIFO => Ok(FileType { mode: libc::S_IFIFO }),
175+
libc::DT_LNK => Ok(FileType { mode: libc::S_IFLNK }),
176+
libc::DT_REG => Ok(FileType { mode: libc::S_IFREG }),
177+
libc::DT_SOCK => Ok(FileType { mode: libc::S_IFSOCK }),
178+
libc::DT_DIR => Ok(FileType { mode: libc::S_IFDIR }),
179+
libc::DT_BLK => Ok(FileType { mode: libc::S_IFBLK }),
180+
_ => lstat(&self.path()).map(|m| m.file_type()),
189181
}
190182
}
191183

184+
#[cfg(any(target_os = "macos",
185+
target_os = "ios",
186+
target_os = "linux"))]
192187
pub fn ino(&self) -> raw::ino_t {
193-
extern {
194-
fn rust_dir_get_ino(ptr: *mut libc::dirent) -> raw::ino_t;
195-
}
196-
unsafe { rust_dir_get_ino(self.dirent()) }
188+
self.entry.d_ino
189+
}
190+
191+
#[cfg(target_os = "android")]
192+
pub fn ino(&self) -> raw::ino_t {
193+
self.entry.d_ino as raw::ino_t
194+
}
195+
196+
#[cfg(any(target_os = "freebsd",
197+
target_os = "openbsd",
198+
target_os = "bitrig",
199+
target_os = "netbsd",
200+
target_os = "dragonfly"))]
201+
pub fn ino(&self) -> raw::ino_t {
202+
self.entry.d_fileno
197203
}
198204

205+
#[cfg(any(target_os = "macos",
206+
target_os = "ios",
207+
target_os = "netbsd"))]
199208
fn name_bytes(&self) -> &[u8] {
200-
extern {
201-
fn rust_list_dir_val(ptr: *mut libc::dirent) -> *const c_char;
209+
unsafe {
210+
::slice::from_raw_parts(self.entry.d_name.as_ptr() as *const u8,
211+
self.entry.d_namlen as usize)
202212
}
213+
}
214+
#[cfg(any(target_os = "freebsd",
215+
target_os = "dragonfly",
216+
target_os = "bitrig",
217+
target_os = "openbsd"))]
218+
fn name_bytes(&self) -> &[u8] {
203219
unsafe {
204-
CStr::from_ptr(rust_list_dir_val(self.dirent())).to_bytes()
220+
::slice::from_raw_parts(self.entry.d_name.as_ptr() as *const u8,
221+
self.entry.d_namelen as usize)
205222
}
206223
}
207-
208-
fn dirent(&self) -> *mut libc::dirent {
209-
self.buf.as_ptr() as *mut _
224+
#[cfg(any(target_os = "android",
225+
target_os = "linux"))]
226+
fn name_bytes(&self) -> &[u8] {
227+
unsafe {
228+
CStr::from_ptr(self.entry.d_name.as_ptr()).to_bytes()
229+
}
210230
}
211231
}
212232

src/libstd/sys/unix/os.rs

+40-27
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,17 @@ static ENV_LOCK: StaticMutex = StaticMutex::new();
3838
/// Returns the platform-specific value of errno
3939
pub fn errno() -> i32 {
4040
extern {
41-
#[cfg_attr(any(target_os = "linux", target_os = "android"), link_name = "__errno_location")]
42-
#[cfg_attr(any(target_os = "bitrig", target_os = "netbsd", target_os = "openbsd",
41+
#[cfg_attr(any(target_os = "linux"), link_name = "__errno_location")]
42+
#[cfg_attr(any(target_os = "bitrig",
43+
target_os = "netbsd",
44+
target_os = "openbsd",
45+
target_os = "android",
4346
target_env = "newlib"),
4447
link_name = "__errno")]
4548
#[cfg_attr(target_os = "dragonfly", link_name = "__dfly_error")]
46-
#[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "freebsd"),
49+
#[cfg_attr(any(target_os = "macos",
50+
target_os = "ios",
51+
target_os = "freebsd"),
4752
link_name = "__error")]
4853
fn errno_location() -> *const c_int;
4954
}
@@ -173,17 +178,19 @@ pub fn current_exe() -> io::Result<PathBuf> {
173178
libc::KERN_PROC_PATHNAME as c_int,
174179
-1 as c_int];
175180
let mut sz: libc::size_t = 0;
176-
let err = libc::sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint,
177-
ptr::null_mut(), &mut sz, ptr::null_mut(),
178-
0 as libc::size_t);
179-
if err != 0 { return Err(io::Error::last_os_error()); }
180-
if sz == 0 { return Err(io::Error::last_os_error()); }
181+
try!(cvt(libc::sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint,
182+
ptr::null_mut(), &mut sz, ptr::null_mut(),
183+
0 as libc::size_t)));
184+
if sz == 0 {
185+
return Err(io::Error::last_os_error())
186+
}
181187
let mut v: Vec<u8> = Vec::with_capacity(sz as usize);
182-
let err = libc::sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint,
183-
v.as_mut_ptr() as *mut libc::c_void, &mut sz,
184-
ptr::null_mut(), 0 as libc::size_t);
185-
if err != 0 { return Err(io::Error::last_os_error()); }
186-
if sz == 0 { return Err(io::Error::last_os_error()); }
188+
try!(cvt(libc::sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint,
189+
v.as_mut_ptr() as *mut libc::c_void, &mut sz,
190+
ptr::null_mut(), 0 as libc::size_t)));
191+
if sz == 0 {
192+
return Err(io::Error::last_os_error());
193+
}
187194
v.set_len(sz as usize - 1); // chop off trailing NUL
188195
Ok(PathBuf::from(OsString::from_vec(v)))
189196
}
@@ -201,22 +208,28 @@ pub fn current_exe() -> io::Result<PathBuf> {
201208

202209
#[cfg(any(target_os = "bitrig", target_os = "openbsd"))]
203210
pub fn current_exe() -> io::Result<PathBuf> {
204-
use sync::StaticMutex;
205-
static LOCK: StaticMutex = StaticMutex::new();
206-
207-
extern {
208-
fn rust_current_exe() -> *const c_char;
209-
}
210-
211-
let _guard = LOCK.lock();
212-
213211
unsafe {
214-
let v = rust_current_exe();
215-
if v.is_null() {
216-
Err(io::Error::last_os_error())
212+
let mut mib = [libc::CTL_KERN,
213+
libc::KERN_PROC_ARGS,
214+
libc::getpid(),
215+
libc::KERN_PROC_ARGV];
216+
let mib = mib.as_mut_ptr();
217+
let mut argv_len = 0;
218+
try!(cvt(libc::sysctl(mib, 4, 0 as *mut _, &mut argv_len,
219+
0 as *mut _, 0)));
220+
let mut argv = Vec::<*const libc::c_char>::with_capacity(argv_len as usize);
221+
try!(cvt(libc::sysctl(mib, 4, argv.as_mut_ptr() as *mut _,
222+
&mut argv_len, 0 as *mut _, 0)));
223+
argv.set_len(argv_len as usize);
224+
if argv[0].is_null() {
225+
return Err(io::Error::new(io::ErrorKind::Other,
226+
"no current exe available"))
227+
}
228+
let argv0 = CStr::from_ptr(argv[0]).to_bytes();
229+
if argv0[0] == b'.' || argv0.iter().any(|b| *b == b'/') {
230+
::fs::canonicalize(OsStr::from_bytes(argv0))
217231
} else {
218-
let vec = CStr::from_ptr(v).to_bytes().to_vec();
219-
Ok(PathBuf::from(OsString::from_vec(vec)))
232+
Ok(PathBuf::from(OsStr::from_bytes(argv0)))
220233
}
221234
}
222235
}

src/libtest/lib.rs

+38-3
Original file line numberDiff line numberDiff line change
@@ -906,10 +906,45 @@ fn get_concurrency() -> usize {
906906
}
907907
}
908908

909-
#[cfg(unix)]
909+
#[cfg(any(target_os = "linux",
910+
target_os = "macos",
911+
target_os = "ios",
912+
target_os = "android"))]
910913
fn num_cpus() -> usize {
911-
extern { fn rust_get_num_cpus() -> libc::uintptr_t; }
912-
unsafe { rust_get_num_cpus() as usize }
914+
unsafe {
915+
libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize
916+
}
917+
}
918+
919+
#[cfg(any(target_os = "freebsd",
920+
target_os = "dragonfly",
921+
target_os = "bitrig",
922+
target_os = "openbsd",
923+
target_os = "netbsd"))]
924+
fn num_cpus() -> usize {
925+
let mut cpus: libc::c_uint = 0;
926+
let mut CPUS_SIZE = std::mem::size_of_val(&cpus);
927+
let mut mib = [libc::CTL_HW, libc::HW_AVAILCPU, 0, 0];
928+
929+
unsafe {
930+
libc::sysctl(mib.as_mut_ptr(), 2,
931+
&mut cpus as *mut _ as *mut _,
932+
&mut CPUS_SIZE as *mut _ as *mut _,
933+
0 as *mut _, 0);
934+
}
935+
if cpus < 1 {
936+
mib[1] = HW_NCPU;
937+
unsafe {
938+
libc::sysctl(mib.as_mut_ptr(), 2,
939+
&mut cpus as *mut _ as *mut _,
940+
&mut CPUS_SIZE as *mut _ as *mut _,
941+
0 as *mut _, 0);
942+
}
943+
if cpus < 1 {
944+
cpus = 1;
945+
}
946+
}
947+
cpus as usize
913948
}
914949
}
915950

0 commit comments

Comments
 (0)