@@ -83,8 +83,66 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
8383}
8484
8585// Globals
86+ #[ cfg( feature = "mem_profile" ) ]
87+ struct ProfiledLockedHeap < const ORDER : usize > ( LockedHeap < ORDER > ) ;
88+ #[ cfg( feature = "mem_profile" ) ]
89+ unsafe impl < const ORDER : usize > alloc:: alloc:: GlobalAlloc for ProfiledLockedHeap < ORDER > {
90+ unsafe fn alloc ( & self , layout : core:: alloc:: Layout ) -> * mut u8 {
91+ let addr = self . 0 . alloc ( layout) ;
92+ unsafe {
93+ core:: arch:: asm!( "out dx, al" ,
94+ in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
95+ in( "rax" ) layout. size( ) as u64 ,
96+ in( "rcx" ) addr as u64 ) ;
97+ }
98+ addr
99+ }
100+ unsafe fn dealloc ( & self , ptr : * mut u8 , layout : core:: alloc:: Layout ) {
101+ unsafe {
102+ core:: arch:: asm!( "out dx, al" ,
103+ in( "dx" ) OutBAction :: TraceMemoryFree as u16 ,
104+ in( "rax" ) layout. size( ) as u64 ,
105+ in( "rcx" ) ptr as u64 ) ;
106+ }
107+ self . 0 . dealloc ( ptr, layout)
108+ }
109+ unsafe fn alloc_zeroed ( & self , layout : core:: alloc:: Layout ) -> * mut u8 {
110+ let addr = self . 0 . alloc_zeroed ( layout) ;
111+ unsafe {
112+ core:: arch:: asm!( "out dx, al" ,
113+ in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
114+ in( "rax" ) layout. size( ) as u64 ,
115+ in( "rcx" ) addr as u64 ) ;
116+ }
117+ addr
118+ }
119+ unsafe fn realloc (
120+ & self ,
121+ ptr : * mut u8 ,
122+ layout : core:: alloc:: Layout ,
123+ new_size : usize ,
124+ ) -> * mut u8 {
125+ let new_ptr = self . 0 . realloc ( ptr, layout, new_size) ;
126+ unsafe {
127+ core:: arch:: asm!( "out dx, al" ,
128+ in( "dx" ) OutBAction :: TraceMemoryFree as u16 ,
129+ in( "rax" ) layout. size( ) as u64 ,
130+ in( "rcx" ) ptr) ;
131+ core:: arch:: asm!( "out dx, al" ,
132+ in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
133+ in( "rax" ) new_size as u64 ,
134+ in( "rcx" ) new_ptr) ;
135+ }
136+ new_ptr
137+ }
138+ }
139+ #[ cfg( not( feature = "mem_profile" ) ) ]
86140#[ global_allocator]
87141pub ( crate ) static HEAP_ALLOCATOR : LockedHeap < 32 > = LockedHeap :: < 32 > :: empty ( ) ;
142+ #[ cfg( feature = "mem_profile" ) ]
143+ #[ global_allocator]
144+ pub ( crate ) static HEAP_ALLOCATOR : ProfiledLockedHeap < 32 > =
145+ ProfiledLockedHeap ( LockedHeap :: < 32 > :: empty ( ) ) ;
88146
89147///cbindgen:ignore
90148#[ no_mangle]
0 commit comments