2017-06-03 16:54:08 -05:00
|
|
|
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
// aux-build:custom.rs
|
|
|
|
// aux-build:custom-as-global.rs
|
|
|
|
// aux-build:helper.rs
|
|
|
|
// no-prefer-dynamic
|
|
|
|
|
|
|
|
#![feature(heap_api, allocator_api)]
|
|
|
|
|
|
|
|
extern crate custom;
|
|
|
|
extern crate custom_as_global;
|
|
|
|
extern crate helper;
|
|
|
|
|
2018-04-03 10:12:57 -05:00
|
|
|
use std::alloc::{Global, Alloc, GlobalAlloc, System, Layout};
|
2017-06-03 16:54:08 -05:00
|
|
|
use std::sync::atomic::{Ordering, ATOMIC_USIZE_INIT};
|
|
|
|
|
|
|
|
static GLOBAL: custom::A = custom::A(ATOMIC_USIZE_INIT);
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
|
|
|
let n = custom_as_global::get();
|
|
|
|
let layout = Layout::from_size_align(4, 2).unwrap();
|
|
|
|
|
|
|
|
// Global allocator routes to the `custom_as_global` global
|
2018-04-02 18:51:02 -05:00
|
|
|
let ptr = Global.alloc(layout.clone());
|
2017-06-03 16:54:08 -05:00
|
|
|
helper::work_with(&ptr);
|
|
|
|
assert_eq!(custom_as_global::get(), n + 1);
|
2018-04-03 10:12:57 -05:00
|
|
|
Global.dealloc(ptr, layout.clone());
|
2017-06-03 16:54:08 -05:00
|
|
|
assert_eq!(custom_as_global::get(), n + 2);
|
|
|
|
|
|
|
|
// Usage of the system allocator avoids all globals
|
2018-04-03 10:12:57 -05:00
|
|
|
let ptr = System.alloc(layout.clone());
|
2017-06-03 16:54:08 -05:00
|
|
|
helper::work_with(&ptr);
|
|
|
|
assert_eq!(custom_as_global::get(), n + 2);
|
|
|
|
System.dealloc(ptr, layout.clone());
|
|
|
|
assert_eq!(custom_as_global::get(), n + 2);
|
|
|
|
|
|
|
|
// Usage of our personal allocator doesn't affect other instances
|
2018-04-03 10:12:57 -05:00
|
|
|
let ptr = GLOBAL.alloc(layout.clone());
|
2017-06-03 16:54:08 -05:00
|
|
|
helper::work_with(&ptr);
|
|
|
|
assert_eq!(custom_as_global::get(), n + 2);
|
|
|
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), 1);
|
2018-04-03 10:12:57 -05:00
|
|
|
GLOBAL.dealloc(ptr, layout);
|
2017-06-03 16:54:08 -05:00
|
|
|
assert_eq!(custom_as_global::get(), n + 2);
|
|
|
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|