]> git.lizzy.rs Git - rust.git/blob - src/test/ui/allocator/custom.rs
Update const_forget.rs
[rust.git] / src / test / ui / allocator / custom.rs
1 // run-pass
2
3 // aux-build:helper.rs
4 // no-prefer-dynamic
5
6 #![feature(allocator_api)]
7
8 extern crate helper;
9
10 use std::alloc::{self, Global, AllocRef, System, Layout};
11 use std::sync::atomic::{AtomicUsize, Ordering};
12
13 static HITS: AtomicUsize = AtomicUsize::new(0);
14
15 struct A;
16
17 unsafe impl alloc::GlobalAlloc for A {
18     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
19         HITS.fetch_add(1, Ordering::SeqCst);
20         System.alloc(layout)
21     }
22
23     unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
24         HITS.fetch_add(1, Ordering::SeqCst);
25         System.dealloc(ptr, layout)
26     }
27 }
28
29 #[global_allocator]
30 static GLOBAL: A = A;
31
32 fn main() {
33     println!("hello!");
34
35     let n = HITS.load(Ordering::SeqCst);
36     assert!(n > 0);
37     unsafe {
38         let layout = Layout::from_size_align(4, 2).unwrap();
39
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);
45
46         let s = String::with_capacity(10);
47         helper::work_with(&s);
48         assert_eq!(HITS.load(Ordering::SeqCst), n + 3);
49         drop(s);
50         assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
51
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);
57     }
58 }