Skip to content

Commit bd8fb60

Browse files
authored
Rollup merge of #142727 - hkBst:rm-static-mut-wasm, r=ChrisDenton
wasm: rm static mut More #125035. I'm not sure this is correct, but it compiles.
2 parents b416342 + ed81a11 commit bd8fb60

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

library/std/src/sys/alloc/wasm.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
//! The crate itself provides a global allocator which on wasm has no
1717
//! synchronization as there are no threads!
1818
19-
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
20-
#![allow(static_mut_refs)]
19+
use core::cell::SyncUnsafeCell;
2120

2221
use crate::alloc::{GlobalAlloc, Layout, System};
2322

24-
static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::Dlmalloc::new();
23+
struct SyncDlmalloc(dlmalloc::Dlmalloc);
24+
unsafe impl Sync for SyncDlmalloc {}
25+
26+
static DLMALLOC: SyncUnsafeCell<SyncDlmalloc> =
27+
SyncUnsafeCell::new(SyncDlmalloc(dlmalloc::Dlmalloc::new()));
2528

2629
#[stable(feature = "alloc_system_type", since = "1.28.0")]
2730
unsafe impl GlobalAlloc for System {
@@ -30,31 +33,31 @@ unsafe impl GlobalAlloc for System {
3033
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
3134
// Calling malloc() is safe because preconditions on this function match the trait method preconditions.
3235
let _lock = lock::lock();
33-
unsafe { DLMALLOC.malloc(layout.size(), layout.align()) }
36+
unsafe { (*DLMALLOC.get()).0.malloc(layout.size(), layout.align()) }
3437
}
3538

3639
#[inline]
3740
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
3841
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
3942
// Calling calloc() is safe because preconditions on this function match the trait method preconditions.
4043
let _lock = lock::lock();
41-
unsafe { DLMALLOC.calloc(layout.size(), layout.align()) }
44+
unsafe { (*DLMALLOC.get()).0.calloc(layout.size(), layout.align()) }
4245
}
4346

4447
#[inline]
4548
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
4649
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
4750
// Calling free() is safe because preconditions on this function match the trait method preconditions.
4851
let _lock = lock::lock();
49-
unsafe { DLMALLOC.free(ptr, layout.size(), layout.align()) }
52+
unsafe { (*DLMALLOC.get()).0.free(ptr, layout.size(), layout.align()) }
5053
}
5154

5255
#[inline]
5356
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
5457
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
5558
// Calling realloc() is safe because preconditions on this function match the trait method preconditions.
5659
let _lock = lock::lock();
57-
unsafe { DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size) }
60+
unsafe { (*DLMALLOC.get()).0.realloc(ptr, layout.size(), layout.align(), new_size) }
5861
}
5962
}
6063

0 commit comments

Comments
 (0)