695dee063b
This PR is an implementation of [RFC 1974] which specifies a new method of defining a global allocator for a program. This obsoletes the old `#![allocator]` attribute and also removes support for it. [RFC 1974]: https://github.com/rust-lang/rfcs/pull/197 The new `#[global_allocator]` attribute solves many issues encountered with the `#![allocator]` attribute such as composition and restrictions on the crate graph itself. The compiler now has much more control over the ABI of the allocator and how it's implemented, allowing much more freedom in terms of how this feature is implemented. cc #27389
58 lines
2.0 KiB
Rust
58 lines
2.0 KiB
Rust
// 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;
|
|
|
|
use std::env;
|
|
use std::heap::{Heap, Alloc, System, Layout};
|
|
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
|
|
let ptr = Heap.alloc(layout.clone()).unwrap();
|
|
helper::work_with(&ptr);
|
|
assert_eq!(custom_as_global::get(), n + 1);
|
|
Heap.dealloc(ptr, layout.clone());
|
|
assert_eq!(custom_as_global::get(), n + 2);
|
|
|
|
// Usage of the system allocator avoids all globals
|
|
let ptr = System.alloc(layout.clone()).unwrap();
|
|
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
|
|
let ptr = (&GLOBAL).alloc(layout.clone()).unwrap();
|
|
helper::work_with(&ptr);
|
|
assert_eq!(custom_as_global::get(), n + 2);
|
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), 1);
|
|
(&GLOBAL).dealloc(ptr, layout);
|
|
assert_eq!(custom_as_global::get(), n + 2);
|
|
assert_eq!(GLOBAL.0.load(Ordering::SeqCst), 2);
|
|
}
|
|
}
|
|
|