11use 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 } ;
25
36use super :: waitqueue:: SpinMutex ;
47
@@ -10,7 +13,48 @@ use super::waitqueue::SpinMutex;
1013// dlmalloc.c from C to Rust.
1114#[ cfg_attr( test, linkage = "available_externally" ) ]
1215#[ 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+ }
1458
1559#[ stable( feature = "alloc_system_type" , since = "1.28.0" ) ]
1660unsafe impl GlobalAlloc for System {
0 commit comments