2018-09-06 07:41:12 -05:00
|
|
|
// run-pass
|
|
|
|
|
2017-06-03 16:54:08 -05:00
|
|
|
// 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 helper;
|
|
|
|
|
2020-12-04 07:47:15 -06:00
|
|
|
use std::alloc::{self, Allocator, Global, Layout, System};
|
2019-01-26 10:14:49 -06:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2017-06-03 16:54:08 -05:00
|
|
|
|
2019-01-26 10:14:49 -06:00
|
|
|
static HITS: AtomicUsize = AtomicUsize::new(0);
|
2017-06-03 16:54:08 -05:00
|
|
|
|
|
|
|
struct A;
|
|
|
|
|
2018-04-03 10:12:57 -05:00
|
|
|
unsafe impl alloc::GlobalAlloc for A {
|
2018-05-31 01:57:43 -05:00
|
|
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
|
2017-06-03 16:54:08 -05:00
|
|
|
HITS.fetch_add(1, Ordering::SeqCst);
|
2020-09-22 23:04:31 -05:00
|
|
|
alloc::GlobalAlloc::alloc(&System, layout)
|
2017-06-03 16:54:08 -05:00
|
|
|
}
|
|
|
|
|
2018-05-31 01:57:43 -05:00
|
|
|
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
|
2017-06-03 16:54:08 -05:00
|
|
|
HITS.fetch_add(1, Ordering::SeqCst);
|
2020-12-04 07:47:15 -06:00
|
|
|
alloc::GlobalAlloc::dealloc(&System, ptr, layout)
|
2017-06-03 16:54:08 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[global_allocator]
|
|
|
|
static GLOBAL: A = A;
|
|
|
|
|
|
|
|
fn main() {
|
2017-10-22 22:01:00 -05:00
|
|
|
println!("hello!");
|
2017-06-03 16:54:08 -05:00
|
|
|
|
|
|
|
let n = HITS.load(Ordering::SeqCst);
|
|
|
|
assert!(n > 0);
|
|
|
|
unsafe {
|
|
|
|
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!(HITS.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!(HITS.load(Ordering::SeqCst), n + 2);
|
|
|
|
|
|
|
|
let s = String::with_capacity(10);
|
|
|
|
helper::work_with(&s);
|
|
|
|
assert_eq!(HITS.load(Ordering::SeqCst), n + 3);
|
|
|
|
drop(s);
|
|
|
|
assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
|
|
|
|
|
2020-12-04 07:47:15 -06:00
|
|
|
let memory = System.allocate(layout.clone()).unwrap();
|
2020-08-04 11:03:34 -05:00
|
|
|
helper::work_with(&memory);
|
2020-12-04 07:47:15 -06:00
|
|
|
assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
|
|
|
|
System.deallocate(memory.as_non_null_ptr(), layout);
|
2017-06-03 16:54:08 -05:00
|
|
|
assert_eq!(HITS.load(Ordering::SeqCst), n + 4);
|
|
|
|
}
|
|
|
|
}
|