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 7 bytes which end up in the 8 byte size-class as long as 15 // jemalloc's default size classes are used: 16 let orig_sz = 7; 17 let orig_l = Layout::from_size_align(orig_sz, 1).unwrap(); 18 let ptr = Jemalloc.alloc(orig_l).unwrap(); 19 20 // try to grow it in place by 1 byte - it should grow without problems: 21 let new_sz = orig_sz + 1; 22 assert!(Jemalloc.grow_in_place(ptr, orig_l, new_sz).is_ok()); 23 let new_l = Layout::from_size_align(orig_sz + 1, 1).unwrap(); 24 25 // trying to do it again fails because it would require moving the 26 // allocation to a different size class which jemalloc's xallocx does not 27 // do: 28 let new_sz = new_sz + 1; 29 assert!(Jemalloc.grow_in_place(ptr, new_l, new_sz).is_err()); 30 31 Jemalloc.dealloc(ptr, new_l) 32 } 33 } 34