Skip to content

Commit a73fcdb

Browse files
alexcrichtonischeinkman
authored andcommitted
std: Delete the alloc_system crate
This commit deletes the `alloc_system` crate from the standard distribution. This unstable crate is no longer needed in the modern stable global allocator world, but rather its functionality is folded directly into the standard library. The standard library was already the only stable location to access this crate, and as a result this should not affect any stable code.
1 parent 198a3f4 commit a73fcdb

File tree

11 files changed

+271
-112
lines changed

11 files changed

+271
-112
lines changed

src/libstd/sys/wasm/alloc.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! This is an implementation of a global allocator on the wasm32 platform when
12+
//! emscripten is not in use. In that situation there's no actual runtime for us
13+
//! to lean on for allocation, so instead we provide our own!
14+
//!
15+
//! The wasm32 instruction set has two instructions for getting the current
16+
//! amount of memory and growing the amount of memory. These instructions are the
17+
//! foundation on which we're able to build an allocator, so we do so! Note that
18+
//! the instructions are also pretty "global" and this is the "global" allocator
19+
//! after all!
20+
//!
21+
//! The current allocator here is the `dlmalloc` crate which we've got included
22+
//! in the rust-lang/rust repository as a submodule. The crate is a port of
23+
//! dlmalloc.c from C to Rust and is basically just so we can have "pure Rust"
24+
//! for now which is currently technically required (can't link with C yet).
25+
//!
26+
//! The crate itself provides a global allocator which on wasm has no
27+
//! synchronization as there are no threads!
28+
29+
extern crate dlmalloc;
30+
31+
use alloc::{GlobalAlloc, Layout, System};
32+
33+
static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::DLMALLOC_INIT;
34+
35+
#[stable(feature = "alloc_system_type", since = "1.28.0")]
36+
unsafe impl GlobalAlloc for System {
37+
#[inline]
38+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
39+
let _lock = lock::lock();
40+
DLMALLOC.malloc(layout.size(), layout.align())
41+
}
42+
43+
#[inline]
44+
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
45+
let _lock = lock::lock();
46+
DLMALLOC.calloc(layout.size(), layout.align())
47+
}
48+
49+
#[inline]
50+
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
51+
let _lock = lock::lock();
52+
DLMALLOC.free(ptr, layout.size(), layout.align())
53+
}
54+
55+
#[inline]
56+
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
57+
let _lock = lock::lock();
58+
DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size)
59+
}
60+
}
61+
62+
#[cfg(target_feature = "atomics")]
63+
mod lock {
64+
use arch::wasm32;
65+
use sync::atomic::{AtomicI32, Ordering::SeqCst};
66+
67+
static LOCKED: AtomicI32 = AtomicI32::new(0);
68+
69+
pub struct DropLock;
70+
71+
pub fn lock() -> DropLock {
72+
loop {
73+
if LOCKED.swap(1, SeqCst) == 0 {
74+
return DropLock
75+
}
76+
unsafe {
77+
let r = wasm32::atomic::wait_i32(
78+
&LOCKED as *const AtomicI32 as *mut i32,
79+
1, // expected value
80+
-1, // timeout
81+
);
82+
debug_assert!(r == 0 || r == 1);
83+
}
84+
}
85+
}
86+
87+
impl Drop for DropLock {
88+
fn drop(&mut self) {
89+
let r = LOCKED.swap(0, SeqCst);
90+
debug_assert_eq!(r, 1);
91+
unsafe {
92+
wasm32::atomic::wake(
93+
&LOCKED as *const AtomicI32 as *mut i32,
94+
1, // only one thread
95+
);
96+
}
97+
}
98+
}
99+
}
100+
101+
#[cfg(not(target_feature = "atomics"))]
102+
mod lock {
103+
#[inline]
104+
pub fn lock() {} // no atomics, no threads, that's easy!
105+
}

src/libstd/sys/wasm/cmath.rs

Lines changed: 14 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,85 +8,32 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#[inline]
12-
pub unsafe fn cbrtf(n: f32) -> f32 {
13-
f64::cbrt(n as f64) as f32
14-
}
15-
16-
#[inline]
17-
pub unsafe fn expm1f(n: f32) -> f32 {
18-
f64::exp_m1(n as f64) as f32
19-
}
20-
21-
#[inline]
22-
#[allow(deprecated)]
23-
pub unsafe fn fdimf(a: f32, b: f32) -> f32 {
24-
f64::abs_sub(a as f64, b as f64) as f32
25-
}
26-
27-
#[inline]
28-
pub unsafe fn log1pf(n: f32) -> f32 {
29-
f64::ln_1p(n as f64) as f32
30-
}
31-
32-
#[inline]
33-
pub unsafe fn hypotf(x: f32, y: f32) -> f32 {
34-
f64::hypot(x as f64, y as f64) as f32
35-
}
36-
37-
#[inline]
38-
pub unsafe fn acosf(n: f32) -> f32 {
39-
f64::acos(n as f64) as f32
40-
}
41-
42-
#[inline]
43-
pub unsafe fn asinf(n: f32) -> f32 {
44-
f64::asin(n as f64) as f32
45-
}
46-
47-
#[inline]
48-
pub unsafe fn atan2f(n: f32, b: f32) -> f32 {
49-
f64::atan2(n as f64, b as f64) as f32
50-
}
51-
52-
#[inline]
53-
pub unsafe fn atanf(n: f32) -> f32 {
54-
f64::atan(n as f64) as f32
55-
}
56-
57-
#[inline]
58-
pub unsafe fn coshf(n: f32) -> f32 {
59-
f64::cosh(n as f64) as f32
60-
}
61-
62-
#[inline]
63-
pub unsafe fn sinhf(n: f32) -> f32 {
64-
f64::sinh(n as f64) as f32
65-
}
66-
67-
#[inline]
68-
pub unsafe fn tanf(n: f32) -> f32 {
69-
f64::tan(n as f64) as f32
70-
}
71-
72-
#[inline]
73-
pub unsafe fn tanhf(n: f32) -> f32 {
74-
f64::tanh(n as f64) as f32
75-
}
76-
7711
// These symbols are all defined in `compiler-builtins`
7812
extern {
7913
pub fn acos(n: f64) -> f64;
14+
pub fn acosf(n: f32) -> f32;
8015
pub fn asin(n: f64) -> f64;
16+
pub fn asinf(n: f32) -> f32;
8117
pub fn atan(n: f64) -> f64;
8218
pub fn atan2(a: f64, b: f64) -> f64;
19+
pub fn atan2f(a: f32, b: f32) -> f32;
20+
pub fn atanf(n: f32) -> f32;
8321
pub fn cbrt(n: f64) -> f64;
22+
pub fn cbrtf(n: f32) -> f32;
8423
pub fn cosh(n: f64) -> f64;
24+
pub fn coshf(n: f32) -> f32;
8525
pub fn expm1(n: f64) -> f64;
26+
pub fn expm1f(n: f32) -> f32;
8627
pub fn fdim(a: f64, b: f64) -> f64;
28+
pub fn fdimf(a: f32, b: f32) -> f32;
29+
pub fn hypot(x: f64, y: f64) -> f64;
30+
pub fn hypotf(x: f32, y: f32) -> f32;
8731
pub fn log1p(n: f64) -> f64;
32+
pub fn log1pf(n: f32) -> f32;
8833
pub fn sinh(n: f64) -> f64;
34+
pub fn sinhf(n: f32) -> f32;
8935
pub fn tan(n: f64) -> f64;
36+
pub fn tanf(n: f32) -> f32;
9037
pub fn tanh(n: f64) -> f64;
91-
pub fn hypot(x: f64, y: f64) -> f64;
38+
pub fn tanhf(n: f32) -> f32;
9239
}

src/libstd/sys/wasm/env.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
// except according to those terms.
1010

1111
pub mod os {
12-
pub const FAMILY: &'static str = "";
13-
pub const OS: &'static str = "";
14-
pub const DLL_PREFIX: &'static str = "";
15-
pub const DLL_SUFFIX: &'static str = ".wasm";
16-
pub const DLL_EXTENSION: &'static str = "wasm";
17-
pub const EXE_SUFFIX: &'static str = ".wasm";
18-
pub const EXE_EXTENSION: &'static str = "wasm";
12+
pub const FAMILY: &str = "";
13+
pub const OS: &str = "";
14+
pub const DLL_PREFIX: &str = "";
15+
pub const DLL_SUFFIX: &str = ".wasm";
16+
pub const DLL_EXTENSION: &str = "wasm";
17+
pub const EXE_SUFFIX: &str = ".wasm";
18+
pub const EXE_EXTENSION: &str = "wasm";
1919
}

src/libstd/sys/wasm/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use sys_common::{AsInner, FromInner};
3232
use ffi::{OsString, OsStr};
3333
use time::Duration;
3434

35+
pub mod alloc;
3536
pub mod args;
3637
#[cfg(feature = "backtrace")]
3738
pub mod backtrace;

src/libstd/sys/wasm/mutex_atomics.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use arch::wasm32::atomic;
1212
use cell::UnsafeCell;
1313
use mem;
14-
use sync::atomic::{AtomicUsize, AtomicU64, Ordering::SeqCst};
14+
use sync::atomic::{AtomicUsize, AtomicU32, Ordering::SeqCst};
15+
use sys::thread;
1516

1617
pub struct Mutex {
1718
locked: AtomicUsize,
@@ -70,7 +71,7 @@ impl Mutex {
7071
}
7172

7273
pub struct ReentrantMutex {
73-
owner: AtomicU64,
74+
owner: AtomicU32,
7475
recursions: UnsafeCell<u32>,
7576
}
7677

@@ -91,7 +92,7 @@ unsafe impl Sync for ReentrantMutex {}
9192
impl ReentrantMutex {
9293
pub unsafe fn uninitialized() -> ReentrantMutex {
9394
ReentrantMutex {
94-
owner: AtomicU64::new(0),
95+
owner: AtomicU32::new(0),
9596
recursions: UnsafeCell::new(0),
9697
}
9798
}
@@ -101,20 +102,20 @@ impl ReentrantMutex {
101102
}
102103

103104
pub unsafe fn lock(&self) {
104-
let me = thread_id();
105+
let me = thread::my_id();
105106
while let Err(owner) = self._try_lock(me) {
106-
let val = atomic::wait_i64(self.ptr(), owner as i64, -1);
107+
let val = atomic::wait_i32(self.ptr(), owner as i32, -1);
107108
debug_assert!(val == 0 || val == 1);
108109
}
109110
}
110111

111112
#[inline]
112113
pub unsafe fn try_lock(&self) -> bool {
113-
self._try_lock(thread_id()).is_ok()
114+
self._try_lock(thread::my_id()).is_ok()
114115
}
115116

116117
#[inline]
117-
unsafe fn _try_lock(&self, id: u64) -> Result<(), u64> {
118+
unsafe fn _try_lock(&self, id: u32) -> Result<(), u32> {
118119
let id = id.checked_add(1).unwrap(); // make sure `id` isn't 0
119120
match self.owner.compare_exchange(0, id, SeqCst, SeqCst) {
120121
// we transitioned from unlocked to locked
@@ -153,11 +154,7 @@ impl ReentrantMutex {
153154
}
154155

155156
#[inline]
156-
fn ptr(&self) -> *mut i64 {
157-
&self.owner as *const AtomicU64 as *mut i64
157+
fn ptr(&self) -> *mut i32 {
158+
&self.owner as *const AtomicU32 as *mut i32
158159
}
159160
}
160-
161-
fn thread_id() -> u64 {
162-
panic!("thread ids not implemented on wasm with atomics yet")
163-
}

src/libstd/sys/wasm/net.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ use io;
1313
use net::{SocketAddr, Shutdown, Ipv4Addr, Ipv6Addr};
1414
use time::Duration;
1515
use sys::{unsupported, Void};
16+
use convert::TryFrom;
1617

1718
pub struct TcpStream(Void);
1819

1920
impl TcpStream {
20-
pub fn connect(_: &SocketAddr) -> io::Result<TcpStream> {
21+
pub fn connect(_: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
2122
unsupported()
2223
}
2324

@@ -103,7 +104,7 @@ impl fmt::Debug for TcpStream {
103104
pub struct TcpListener(Void);
104105

105106
impl TcpListener {
106-
pub fn bind(_: &SocketAddr) -> io::Result<TcpListener> {
107+
pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<TcpListener> {
107108
unsupported()
108109
}
109110

@@ -153,7 +154,7 @@ impl fmt::Debug for TcpListener {
153154
pub struct UdpSocket(Void);
154155

155156
impl UdpSocket {
156-
pub fn bind(_: &SocketAddr) -> io::Result<UdpSocket> {
157+
pub fn bind(_: io::Result<&SocketAddr>) -> io::Result<UdpSocket> {
157158
unsupported()
158159
}
159160

@@ -273,7 +274,7 @@ impl UdpSocket {
273274
match self.0 {}
274275
}
275276

276-
pub fn connect(&self, _: &SocketAddr) -> io::Result<()> {
277+
pub fn connect(&self, _: io::Result<&SocketAddr>) -> io::Result<()> {
277278
match self.0 {}
278279
}
279280
}
@@ -286,15 +287,33 @@ impl fmt::Debug for UdpSocket {
286287

287288
pub struct LookupHost(Void);
288289

290+
impl LookupHost {
291+
pub fn port(&self) -> u16 {
292+
match self.0 {}
293+
}
294+
}
295+
289296
impl Iterator for LookupHost {
290297
type Item = SocketAddr;
291298
fn next(&mut self) -> Option<SocketAddr> {
292299
match self.0 {}
293300
}
294301
}
295302

296-
pub fn lookup_host(_: &str) -> io::Result<LookupHost> {
297-
unsupported()
303+
impl<'a> TryFrom<&'a str> for LookupHost {
304+
type Error = io::Error;
305+
306+
fn try_from(_v: &'a str) -> io::Result<LookupHost> {
307+
unsupported()
308+
}
309+
}
310+
311+
impl<'a> TryFrom<(&'a str, u16)> for LookupHost {
312+
type Error = io::Error;
313+
314+
fn try_from(_v: (&'a str, u16)) -> io::Result<LookupHost> {
315+
unsupported()
316+
}
298317
}
299318

300319
#[allow(nonstandard_style)]

src/libstd/sys/wasm/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ pub fn parse_prefix(_: &OsStr) -> Option<Prefix> {
2525
None
2626
}
2727

28-
pub const MAIN_SEP_STR: &'static str = "/";
28+
pub const MAIN_SEP_STR: &str = "/";
2929
pub const MAIN_SEP: char = '/';

src/libstd/sys/wasm/stdio.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ pub fn is_ebadf(_err: &io::Error) -> bool {
7070
true
7171
}
7272

73-
pub fn stderr_prints_nothing() -> bool {
74-
!cfg!(feature = "wasm_syscall")
73+
pub fn panic_output() -> Option<impl io::Write> {
74+
if cfg!(feature = "wasm_syscall") {
75+
Stderr::new().ok()
76+
} else {
77+
None
78+
}
7579
}

0 commit comments

Comments
 (0)