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
2221use 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" ) ]
2730unsafe 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