6 #![feature(allocator_api)]
10 use std::alloc::{self, Global, AllocRef, System, Layout};
11 use std::sync::atomic::{AtomicUsize, Ordering};
13 static HITS: AtomicUsize = AtomicUsize::new(0);
17 unsafe impl alloc::GlobalAlloc for A {
18 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
19 HITS.fetch_add(1, Ordering::SeqCst);
23 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
24 HITS.fetch_add(1, Ordering::SeqCst);
25 System.dealloc(ptr, layout)
35 let n = HITS.load(Ordering::SeqCst);
38 let layout = Layout::from_size_align(4, 2).unwrap();
40 let ptr = Global.alloc(layout.clone()).unwrap();
41 helper::work_with(&ptr);
42 assert_eq!(HITS.load(Ordering::SeqCst), n + 1);
43 Global.dealloc(ptr, layout.clone());
44 assert_eq!(HITS.load(Ordering::SeqCst), n + 2);
46 let s = String::with_capacity(10);
47 helper::work_with(&s);
48 assert_eq!(HITS.load(Ordering::SeqCst), n + 3);
50 assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
52 let ptr = System.alloc(layout.clone()).unwrap();
53 assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
54 helper::work_with(&ptr);
55 System.dealloc(ptr, layout);
56 assert_eq!(HITS.load(Ordering::SeqCst), n + 4);