Skip to content

Commit 3d28b8b

Browse files
committed
std: Migrate to the new libc
* Delete `sys::unix::{c, sync}` as these are now all folded into libc itself * Update all references to use `libc` as a result. * Update all references to the new flat namespace. * Moves all windows bindings into sys::c
1 parent c8a29c2 commit 3d28b8b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1895
-2437
lines changed

mk/crates.mk

+3
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,6 @@ $(foreach crate,$(TOOLS),$(eval $(call RUST_TOOL,$(crate))))
174174
ifdef CFG_DISABLE_ELF_TLS
175175
RUSTFLAGS_std := --cfg no_elf_tls
176176
endif
177+
178+
CRATEFILE_libc := $(SREL)src/liblibc/src/lib.rs
179+
RUSTFLAGS_libc := --cfg stdbuild

mk/tests.mk

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $(eval $(call RUST_CRATE,coretest))
2222
DEPS_collectionstest :=
2323
$(eval $(call RUST_CRATE,collectionstest))
2424

25-
TEST_TARGET_CRATES = $(filter-out core rustc_unicode alloc_system \
25+
TEST_TARGET_CRATES = $(filter-out core rustc_unicode alloc_system libc \
2626
alloc_jemalloc,$(TARGET_CRATES)) \
2727
collectionstest coretest
2828
TEST_DOC_CRATES = $(DOC_CRATES)
@@ -283,6 +283,7 @@ tidy-binaries:
283283
| grep '^$(S)src/compiler-rt' -v \
284284
| grep '^$(S)src/libbacktrace' -v \
285285
| grep '^$(S)src/rust-installer' -v \
286+
| grep '^$(S)src/liblibc' -v \
286287
| xargs $(CFG_PYTHON) $(S)src/etc/check-binaries.py
287288

288289
.PHONY: tidy-errors

src/etc/tidy.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ def interesting_file(f):
108108
'src/rustllvm',
109109
'src/rt/valgrind',
110110
'src/rt/msvc',
111-
'src/rust-installer'
111+
'src/rust-installer',
112+
'src/liblibc',
112113
}
113114

114115
if any(d in dirpath for d in skippable_dirs):

src/liballoc_jemalloc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![staged_api]
1515
#![no_std]
1616
#![cfg_attr(not(stage0), allocator)]
17+
#![cfg_attr(stage0, allow(improper_ctypes))]
1718
#![unstable(feature = "alloc_jemalloc",
1819
reason = "this library is unlikely to be stabilized in its current \
1920
form or name",

src/liballoc_system/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![staged_api]
1515
#![no_std]
1616
#![cfg_attr(not(stage0), allocator)]
17+
#![cfg_attr(stage0, allow(improper_ctypes))]
1718
#![unstable(feature = "alloc_system",
1819
reason = "this library is unlikely to be stabilized in its current \
1920
form or name",
@@ -141,10 +142,16 @@ mod imp {
141142
}
142143

143144
#[cfg(windows)]
145+
#[allow(bad_style)]
144146
mod imp {
145-
use libc::{BOOL, DWORD, HANDLE, LPVOID, SIZE_T};
146147
use MIN_ALIGN;
147148

149+
type LPVOID = *mut u8;
150+
type HANDLE = LPVOID;
151+
type SIZE_T = usize;
152+
type DWORD = u32;
153+
type BOOL = i32;
154+
148155
extern "system" {
149156
fn GetProcessHeap() -> HANDLE;
150157
fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;

src/libflate/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(staged_api)]
3030
#![feature(unique)]
3131
#![cfg_attr(test, feature(rustc_private, rand, vec_push_all))]
32+
#![cfg_attr(stage0, allow(improper_ctypes))]
3233

3334
#[cfg(test)]
3435
#[macro_use]

src/librustc/util/common.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,30 @@ fn get_resident() -> Option<usize> {
9696
}
9797

9898
#[cfg(windows)]
99+
#[cfg_attr(stage0, allow(improper_ctypes))]
99100
fn get_resident() -> Option<usize> {
100-
use libc::{BOOL, DWORD, HANDLE, SIZE_T, GetCurrentProcess};
101+
type BOOL = i32;
102+
type DWORD = u32;
103+
type HANDLE = *mut u8;
104+
use libc::size_t;
101105
use std::mem;
102106
#[repr(C)] #[allow(non_snake_case)]
103107
struct PROCESS_MEMORY_COUNTERS {
104108
cb: DWORD,
105109
PageFaultCount: DWORD,
106-
PeakWorkingSetSize: SIZE_T,
107-
WorkingSetSize: SIZE_T,
108-
QuotaPeakPagedPoolUsage: SIZE_T,
109-
QuotaPagedPoolUsage: SIZE_T,
110-
QuotaPeakNonPagedPoolUsage: SIZE_T,
111-
QuotaNonPagedPoolUsage: SIZE_T,
112-
PagefileUsage: SIZE_T,
113-
PeakPagefileUsage: SIZE_T,
110+
PeakWorkingSetSize: size_t,
111+
WorkingSetSize: size_t,
112+
QuotaPeakPagedPoolUsage: size_t,
113+
QuotaPagedPoolUsage: size_t,
114+
QuotaPeakNonPagedPoolUsage: size_t,
115+
QuotaNonPagedPoolUsage: size_t,
116+
PagefileUsage: size_t,
117+
PeakPagefileUsage: size_t,
114118
}
115119
type PPROCESS_MEMORY_COUNTERS = *mut PROCESS_MEMORY_COUNTERS;
116120
#[link(name = "psapi")]
117121
extern "system" {
122+
fn GetCurrentProcess() -> HANDLE;
118123
fn GetProcessMemoryInfo(Process: HANDLE,
119124
ppsmemCounters: PPROCESS_MEMORY_COUNTERS,
120125
cb: DWORD) -> BOOL;

src/librustc_llvm/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#![allow(dead_code)]
1717
#![allow(trivial_casts)]
1818

19+
#![cfg_attr(stage0, allow(improper_ctypes))]
20+
1921
#![crate_name = "rustc_llvm"]
2022
#![unstable(feature = "rustc_private", issue = "27812")]
2123
#![staged_api]

src/librustc_trans/back/msvc/registry.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ use std::ffi::{OsString, OsStr};
1313
use std::os::windows::prelude::*;
1414
use std::ops::RangeFrom;
1515
use std::ptr;
16-
use libc::{DWORD, LPCWSTR, LONG, LPDWORD, LPBYTE, ERROR_SUCCESS};
17-
use libc::c_void;
16+
use libc::{c_void, c_long};
17+
18+
type DWORD = u32;
19+
type LPCWSTR = *const u16;
20+
type LONG = c_long;
21+
type LPDWORD = *mut DWORD;
22+
type LPBYTE = *mut u8;
23+
1824

1925
const HKEY_LOCAL_MACHINE: HKEY = 0x80000002 as HKEY;
2026
const KEY_WOW64_32KEY: REGSAM = 0x0200;
@@ -27,6 +33,7 @@ const KEY_ENUMERATE_SUB_KEYS: REGSAM = 0x0008;
2733
const KEY_NOTIFY: REGSAM = 0x0010;
2834
const SYNCHRONIZE: REGSAM = 0x00100000;
2935
const REG_SZ: DWORD = 1;
36+
const ERROR_SUCCESS: i32 = 0;
3037
const ERROR_NO_MORE_ITEMS: DWORD = 259;
3138

3239
enum __HKEY__ {}

src/librustdoc/flock.rs

+29-51
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ mod imp {
121121
let buf = CString::new(os.as_bytes()).unwrap();
122122
let fd = unsafe {
123123
libc::open(buf.as_ptr(), libc::O_RDWR | libc::O_CREAT,
124-
libc::S_IRWXU)
124+
libc::S_IRWXU as libc::c_int)
125125
};
126126
assert!(fd > 0, "failed to open lockfile: {}",
127127
io::Error::last_os_error());
@@ -164,77 +164,55 @@ mod imp {
164164
}
165165

166166
#[cfg(windows)]
167+
#[allow(bad_style)]
167168
mod imp {
168-
use libc;
169169
use std::io;
170170
use std::mem;
171-
use std::ffi::OsStr;
172171
use std::os::windows::prelude::*;
172+
use std::os::windows::raw::HANDLE;
173173
use std::path::Path;
174-
use std::ptr;
175-
176-
const LOCKFILE_EXCLUSIVE_LOCK: libc::DWORD = 0x00000002;
174+
use std::fs::{File, OpenOptions};
175+
176+
type DWORD = u32;
177+
type LPOVERLAPPED = *mut OVERLAPPED;
178+
type BOOL = i32;
179+
const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x00000002;
180+
181+
#[repr(C)]
182+
struct OVERLAPPED {
183+
Internal: usize,
184+
InternalHigh: usize,
185+
Pointer: *mut u8,
186+
hEvent: *mut u8,
187+
}
177188

178-
#[allow(non_snake_case)]
179189
extern "system" {
180-
fn LockFileEx(hFile: libc::HANDLE,
181-
dwFlags: libc::DWORD,
182-
dwReserved: libc::DWORD,
183-
nNumberOfBytesToLockLow: libc::DWORD,
184-
nNumberOfBytesToLockHigh: libc::DWORD,
185-
lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
186-
fn UnlockFileEx(hFile: libc::HANDLE,
187-
dwReserved: libc::DWORD,
188-
nNumberOfBytesToLockLow: libc::DWORD,
189-
nNumberOfBytesToLockHigh: libc::DWORD,
190-
lpOverlapped: libc::LPOVERLAPPED) -> libc::BOOL;
190+
fn LockFileEx(hFile: HANDLE,
191+
dwFlags: DWORD,
192+
dwReserved: DWORD,
193+
nNumberOfBytesToLockLow: DWORD,
194+
nNumberOfBytesToLockHigh: DWORD,
195+
lpOverlapped: LPOVERLAPPED) -> BOOL;
191196
}
192197

193198
pub struct Lock {
194-
handle: libc::HANDLE,
199+
_file: File,
195200
}
196201

197202
impl Lock {
198203
pub fn new(p: &Path) -> Lock {
199-
let os: &OsStr = p.as_ref();
200-
let mut p_16: Vec<_> = os.encode_wide().collect();
201-
p_16.push(0);
202-
let handle = unsafe {
203-
libc::CreateFileW(p_16.as_ptr(),
204-
libc::FILE_GENERIC_READ |
205-
libc::FILE_GENERIC_WRITE,
206-
libc::FILE_SHARE_READ |
207-
libc::FILE_SHARE_DELETE |
208-
libc::FILE_SHARE_WRITE,
209-
ptr::null_mut(),
210-
libc::CREATE_ALWAYS,
211-
libc::FILE_ATTRIBUTE_NORMAL,
212-
ptr::null_mut())
213-
};
214-
if handle == libc::INVALID_HANDLE_VALUE {
215-
panic!("create file error: {}", io::Error::last_os_error());
216-
}
217-
let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
204+
let f = OpenOptions::new().read(true).write(true).create(true)
205+
.open(p).unwrap();
218206
let ret = unsafe {
219-
LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0,
207+
let mut overlapped: OVERLAPPED = mem::zeroed();
208+
LockFileEx(f.as_raw_handle(), LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0,
220209
&mut overlapped)
221210
};
222211
if ret == 0 {
223212
let err = io::Error::last_os_error();
224-
unsafe { libc::CloseHandle(handle); }
225213
panic!("could not lock `{}`: {}", p.display(), err);
226214
}
227-
Lock { handle: handle }
228-
}
229-
}
230-
231-
impl Drop for Lock {
232-
fn drop(&mut self) {
233-
let mut overlapped: libc::OVERLAPPED = unsafe { mem::zeroed() };
234-
unsafe {
235-
UnlockFileEx(self.handle, 0, 100, 0, &mut overlapped);
236-
libc::CloseHandle(self.handle);
237-
}
215+
Lock { _file: f }
238216
}
239217
}
240218
}

0 commit comments

Comments
 (0)