2018-09-06 12:41:12 +00:00
|
|
|
//@ run-pass
|
|
|
|
|
2017-06-03 21:54:08 +00:00
|
|
|
//@ aux-build:custom.rs
|
|
|
|
//@ aux-build:helper.rs
|
|
|
|
//@ no-prefer-dynamic
|
|
|
|
|
2018-05-31 17:31:00 +00:00
|
|
|
#![feature(allocator_api)]
|
2020-08-04 16:03:34 +00:00
|
|
|
#![feature(slice_ptr_get)]
|
2017-06-03 21:54:08 +00:00
|
|
|
|
|
|
|
extern crate custom;
|
|
|
|
extern crate helper;
|
|
|
|
|
2020-12-04 13:47:15 +00:00
|
|
|
use std::alloc::{Allocator, Global, Layout, System};
|
2020-03-24 10:45:38 +00:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2017-06-03 21:54:08 +00:00
|
|
|
|
|
|
|
#[global_allocator]
|
2019-01-26 16:14:49 +00:00
|
|
|
static GLOBAL: custom::A = custom::A(AtomicUsize::new(0));
|
2017-06-03 21:54:08 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
|
|
|
let n = GLOBAL.0.load(Ordering::SeqCst);
|
|
|
|
let layout = Layout::from_size_align(4, 2).unwrap();
|
|
|
|
|
2020-12-04 13:47:15 +00:00
|
|
|
let memory = Global.allocate(layout.clone()).unwrap();
|
2020-08-04 16:03:34 +00:00
|
|
|
helper::work_with(&memory);
|
2017-06-03 21:54:08 +00:00
|
|
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 1);
|
2020-12-04 13:47:15 +00:00
|
|
|
Global.deallocate(memory.as_non_null_ptr(), layout);
|
2017-06-03 21:54:08 +00:00
|
|
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
|
|
|
|
|
2020-12-04 13:47:15 +00:00
|
|
|
let memory = System.allocate(layout.clone()).unwrap();
|
2017-06-03 21:54:08 +00:00
|
|
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
|
2020-08-04 16:03:34 +00:00
|
|
|
helper::work_with(&memory);
|
2020-12-04 13:47:15 +00:00
|
|
|
System.deallocate(memory.as_non_null_ptr(), layout);
|
2017-06-03 21:54:08 +00:00
|
|
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), n + 2);
|
|
|
|
}
|
|
|
|
}
|