6 #![feature(allocator_api)]
7 #![feature(slice_ptr_get)]
11 use std
::alloc
::{self, Allocator, Global, Layout, System}
;
12 use std
::sync
::atomic
::{AtomicUsize, Ordering}
;
14 static HITS
: AtomicUsize
= AtomicUsize
::new(0);
18 unsafe impl alloc
::GlobalAlloc
for A
{
19 unsafe fn alloc(&self, layout
: Layout
) -> *mut u8 {
20 HITS
.fetch_add(1, Ordering
::SeqCst
);
21 alloc
::GlobalAlloc
::alloc(&System
, layout
)
24 unsafe fn dealloc(&self, ptr
: *mut u8, layout
: Layout
) {
25 HITS
.fetch_add(1, Ordering
::SeqCst
);
26 alloc
::GlobalAlloc
::dealloc(&System
, ptr
, layout
)
36 let n
= HITS
.load(Ordering
::SeqCst
);
39 let layout
= Layout
::from_size_align(4, 2).unwrap();
41 let memory
= Global
.allocate(layout
.clone()).unwrap();
42 helper
::work_with(&memory
);
43 assert_eq
!(HITS
.load(Ordering
::SeqCst
), n
+ 1);
44 Global
.deallocate(memory
.as_non_null_ptr(), layout
);
45 assert_eq
!(HITS
.load(Ordering
::SeqCst
), n
+ 2);
47 let s
= String
::with_capacity(10);
48 helper
::work_with(&s
);
49 assert_eq
!(HITS
.load(Ordering
::SeqCst
), n
+ 3);
51 assert_eq
!(HITS
.load(Ordering
::SeqCst
), n
+ 4);
53 let memory
= System
.allocate(layout
.clone()).unwrap();
54 helper
::work_with(&memory
);
55 assert_eq
!(HITS
.load(Ordering
::SeqCst
), n
+ 4);
56 System
.deallocate(memory
.as_non_null_ptr(), layout
);
57 assert_eq
!(HITS
.load(Ordering
::SeqCst
), n
+ 4);