@@ -83,8 +83,60 @@ 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 ( & self , ptr : * mut u8 , layout : core:: alloc:: Layout , new_size : usize ) -> * mut u8 {
120+ let new_ptr = self . 0 . realloc ( ptr, layout, new_size) ;
121+ unsafe {
122+ core:: arch:: asm!( "out dx, al" ,
123+ in( "dx" ) OutBAction :: TraceMemoryFree as u16 ,
124+ in( "rax" ) layout. size( ) as u64 ,
125+ in( "rcx" ) ptr) ;
126+ core:: arch:: asm!( "out dx, al" ,
127+ in( "dx" ) OutBAction :: TraceMemoryAlloc as u16 ,
128+ in( "rax" ) new_size as u64 ,
129+ in( "rcx" ) new_ptr) ;
130+ }
131+ new_ptr
132+ }
133+ }
134+ #[ cfg( not( feature = "mem_profile" ) ) ]
86135#[ global_allocator]
87136pub ( crate ) static HEAP_ALLOCATOR : LockedHeap < 32 > = LockedHeap :: < 32 > :: empty ( ) ;
137+ #[ cfg( feature = "mem_profile" ) ]
138+ #[ global_allocator]
139+ pub ( crate ) static HEAP_ALLOCATOR : ProfiledLockedHeap < 32 > = ProfiledLockedHeap ( LockedHeap :: < 32 > :: empty ( ) ) ;
88140
89141///cbindgen:ignore
90142#[ no_mangle]
0 commit comments