16
16
//! The crate itself provides a global allocator which on wasm has no
17
17
//! synchronization as there are no threads!
18
18
19
- // FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
20
- #![ allow( static_mut_refs) ]
19
+ use core:: cell:: SyncUnsafeCell ;
21
20
22
21
use crate :: alloc:: { GlobalAlloc , Layout , System } ;
23
22
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 ( ) ) ) ;
25
28
26
29
#[ stable( feature = "alloc_system_type" , since = "1.28.0" ) ]
27
30
unsafe impl GlobalAlloc for System {
@@ -30,31 +33,31 @@ unsafe impl GlobalAlloc for System {
30
33
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
31
34
// Calling malloc() is safe because preconditions on this function match the trait method preconditions.
32
35
let _lock = lock:: lock ( ) ;
33
- unsafe { DLMALLOC . malloc ( layout. size ( ) , layout. align ( ) ) }
36
+ unsafe { ( * DLMALLOC . get ( ) ) . 0 . malloc ( layout. size ( ) , layout. align ( ) ) }
34
37
}
35
38
36
39
#[ inline]
37
40
unsafe fn alloc_zeroed ( & self , layout : Layout ) -> * mut u8 {
38
41
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
39
42
// Calling calloc() is safe because preconditions on this function match the trait method preconditions.
40
43
let _lock = lock:: lock ( ) ;
41
- unsafe { DLMALLOC . calloc ( layout. size ( ) , layout. align ( ) ) }
44
+ unsafe { ( * DLMALLOC . get ( ) ) . 0 . calloc ( layout. size ( ) , layout. align ( ) ) }
42
45
}
43
46
44
47
#[ inline]
45
48
unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
46
49
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
47
50
// Calling free() is safe because preconditions on this function match the trait method preconditions.
48
51
let _lock = lock:: lock ( ) ;
49
- unsafe { DLMALLOC . free ( ptr, layout. size ( ) , layout. align ( ) ) }
52
+ unsafe { ( * DLMALLOC . get ( ) ) . 0 . free ( ptr, layout. size ( ) , layout. align ( ) ) }
50
53
}
51
54
52
55
#[ inline]
53
56
unsafe fn realloc ( & self , ptr : * mut u8 , layout : Layout , new_size : usize ) -> * mut u8 {
54
57
// SAFETY: DLMALLOC access is guaranteed to be safe because the lock gives us unique and non-reentrant access.
55
58
// Calling realloc() is safe because preconditions on this function match the trait method preconditions.
56
59
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) }
58
61
}
59
62
}
60
63
0 commit comments