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