]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/allocator/xcrate-use.rs
rustc: Implement the #[global_allocator] attribute
[rust.git] / src / test / run-pass / allocator / xcrate-use.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // aux-build:custom.rs
12 // aux-build:helper.rs
13 // no-prefer-dynamic
14
15 #![feature(global_allocator, heap_api, allocator_api)]
16
17 extern crate custom;
18 extern crate helper;
19
20 use std::env;
21 use std::heap::{Heap, Alloc, System, Layout};
22 use std::sync::atomic::{Ordering, ATOMIC_USIZE_INIT};
23
24 #[global_allocator]
25 static GLOBAL: custom::A = custom::A(ATOMIC_USIZE_INIT);
26
27 fn main() {
28     unsafe {
29         let n = GLOBAL.0.load(Ordering::SeqCst);
30         let layout = Layout::from_size_align(4, 2).unwrap();
31
32         let ptr = Heap.alloc(layout.clone()).unwrap();
33         helper::work_with(&ptr);
34         assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1);
35         Heap.dealloc(ptr, layout.clone());
36         assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
37
38         let ptr = System.alloc(layout.clone()).unwrap();
39         assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
40         helper::work_with(&ptr);
41         System.dealloc(ptr, layout);
42         assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
43     }
44 }