1 #![cfg_attr(feature = "alloc_trait", feature(allocator_api))] 2 3 use tikv_jemallocator::Jemalloc; 4 5 #[global_allocator] 6 static A: Jemalloc = Jemalloc; 7 8 #[test] 9 #[cfg(feature = "alloc_trait")] shrink_in_place()10fn shrink_in_place() { 11 unsafe { 12 use std::alloc::{Alloc, Layout}; 13 14 // allocate a "large" block of memory: 15 let orig_sz = 10 * 4096; 16 let orig_l = Layout::from_size_align(orig_sz, 1).unwrap(); 17 let ptr = Jemalloc.alloc(orig_l).unwrap(); 18 19 // try to shrink it in place to 1 byte - if this succeeds, 20 // the size-class of the new allocation should be different 21 // than that of the original allocation: 22 let new_sz = 1; 23 if let Ok(()) = Jemalloc.shrink_in_place(ptr, orig_l, new_sz) { 24 // test that deallocating with the new layout succeeds: 25 let new_l = Layout::from_size_align(new_sz, 1).unwrap(); 26 Jemalloc.dealloc(ptr, new_l); 27 } else { 28 // if shrink in place failed - deallocate with the old layout 29 Jemalloc.dealloc(ptr, orig_l); 30 } 31 } 32 } 33