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