@@ -14,16 +14,16 @@ use rand::{thread_rng, Rng};
1414
1515#[ cfg( feature = "jemalloc" ) ]
1616mod alloc {
17- use std:: alloc:: Layout ;
1817 use jemallocator:: Jemalloc ;
18+ use std:: alloc:: Layout ;
1919
2020 #[ global_allocator]
2121 static ALLOCATOR : Jemalloc = Jemalloc ;
2222}
2323
2424#[ cfg( feature = "memshred" ) ]
2525mod alloc {
26- use std:: alloc:: { System , Layout } ;
26+ use std:: alloc:: { Layout , System } ;
2727
2828 #[ global_allocator]
2929 static ALLOCATOR : Alloc = Alloc ;
@@ -42,11 +42,35 @@ mod alloc {
4242 unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
4343 std:: ptr:: write_bytes ( ptr, 0xde , layout. size ( ) ) ;
4444 System . dealloc ( ptr, layout)
45-
4645 }
4746 }
4847}
4948
49+ #[ cfg( feature = "measure_allocs" ) ]
50+ mod alloc {
51+ use std:: alloc:: { Layout , System } ;
52+ use std:: sync:: atomic:: { AtomicUsize , Ordering :: Release } ;
53+
54+ pub static ALLOCATIONS : AtomicUsize = AtomicUsize :: new ( 0 ) ;
55+ pub static ALLOCATED_BYTES : AtomicUsize = AtomicUsize :: new ( 0 ) ;
56+
57+ #[ global_allocator]
58+ static ALLOCATOR : Alloc = Alloc ;
59+
60+ #[ derive( Default , Debug , Clone , Copy ) ]
61+ struct Alloc ;
62+
63+ unsafe impl std:: alloc:: GlobalAlloc for Alloc {
64+ unsafe fn alloc ( & self , layout : Layout ) -> * mut u8 {
65+ ALLOCATIONS . fetch_add ( 1 , Release ) ;
66+ ALLOCATED_BYTES . fetch_add ( layout. size ( ) , Release ) ;
67+ System . alloc ( layout)
68+ }
69+ unsafe fn dealloc ( & self , ptr : * mut u8 , layout : Layout ) {
70+ System . dealloc ( ptr, layout)
71+ }
72+ }
73+ }
5074
5175#[ global_allocator]
5276#[ cfg( feature = "dh" ) ]
@@ -385,6 +409,17 @@ fn main() {
385409 ( ( ops * 1_000 ) / ( time * 1_000 ) ) . to_formatted_string( & Locale :: en)
386410 ) ;
387411
412+ #[ cfg( feature = "measure_allocs" ) ]
413+ println ! (
414+ "allocated {} bytes in {} allocations" ,
415+ alloc:: ALLOCATED_BYTES
416+ . load( Ordering :: Acquire )
417+ . to_formatted_string( & Locale :: en) ,
418+ alloc:: ALLOCATIONS
419+ . load( Ordering :: Acquire )
420+ . to_formatted_string( & Locale :: en) ,
421+ ) ;
422+
388423 #[ cfg( feature = "metrics" ) ]
389424 sled:: print_profile ( ) ;
390425}
0 commit comments