1
1
use crate :: alloc:: { GlobalAlloc , Layout , System } ;
2
+ use crate :: ptr;
3
+ use crate :: sys:: sgx:: abi:: mem as sgx_mem;
4
+ use core:: sync:: atomic:: { AtomicBool , Ordering } ;
2
5
3
6
use super :: waitqueue:: SpinMutex ;
4
7
@@ -10,7 +13,48 @@ use super::waitqueue::SpinMutex;
10
13
// dlmalloc.c from C to Rust.
11
14
#[ cfg_attr( test, linkage = "available_externally" ) ]
12
15
#[ export_name = "_ZN16__rust_internals3std3sys3sgx5alloc8DLMALLOCE" ]
13
- static DLMALLOC : SpinMutex < dlmalloc:: Dlmalloc > = SpinMutex :: new ( dlmalloc:: DLMALLOC_INIT ) ;
16
+ static DLMALLOC : SpinMutex < dlmalloc:: Dlmalloc < Sgx > > =
17
+ SpinMutex :: new ( dlmalloc:: Dlmalloc :: new_with_allocator ( Sgx { } ) ) ;
18
+
19
+ struct Sgx ;
20
+
21
+ unsafe impl dlmalloc:: Allocator for Sgx {
22
+ /// Allocs system resources
23
+ fn alloc ( & self , _size : usize ) -> ( * mut u8 , usize , u32 ) {
24
+ static INIT : AtomicBool = AtomicBool :: new ( false ) ;
25
+
26
+ // No ordering requirement since this function is protected by the global lock.
27
+ if !INIT . swap ( true , Ordering :: Relaxed ) {
28
+ ( sgx_mem:: heap_base ( ) as _ , sgx_mem:: heap_size ( ) , 0 )
29
+ } else {
30
+ ( ptr:: null_mut ( ) , 0 , 0 )
31
+ }
32
+ }
33
+
34
+ fn remap ( & self , _ptr : * mut u8 , _oldsize : usize , _newsize : usize , _can_move : bool ) -> * mut u8 {
35
+ ptr:: null_mut ( )
36
+ }
37
+
38
+ fn free_part ( & self , _ptr : * mut u8 , _oldsize : usize , _newsize : usize ) -> bool {
39
+ false
40
+ }
41
+
42
+ fn free ( & self , _ptr : * mut u8 , _size : usize ) -> bool {
43
+ return false ;
44
+ }
45
+
46
+ fn can_release_part ( & self , _flags : u32 ) -> bool {
47
+ false
48
+ }
49
+
50
+ fn allocates_zeros ( & self ) -> bool {
51
+ false
52
+ }
53
+
54
+ fn page_size ( & self ) -> usize {
55
+ 0x1000
56
+ }
57
+ }
14
58
15
59
#[ stable( feature = "alloc_system_type" , since = "1.28.0" ) ]
16
60
unsafe impl GlobalAlloc for System {
0 commit comments