-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #42727 - alexcrichton:allocators-new, r=eddyb
rustc: Implement the #[global_allocator] attribute 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]: rust-lang/rfcs#1974 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
- Loading branch information
Showing
115 changed files
with
2,828 additions
and
1,169 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
src/doc/unstable-book/src/language-features/allocator-internals.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# `allocator_internals` | ||
|
||
This feature does not have a tracking issue, it is an unstable implementation | ||
detail of the `global_allocator` feature not intended for use outside the | ||
compiler. | ||
|
||
------------------------ |
119 changes: 0 additions & 119 deletions
119
src/doc/unstable-book/src/language-features/allocator.md
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
src/doc/unstable-book/src/language-features/global-allocator.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# `global_allocator` | ||
|
||
The tracking issue for this feature is: [#27389] | ||
|
||
[#27389]: https://github.com/rust-lang/rust/issues/27389 | ||
|
||
------------------------ | ||
|
||
Rust programs may need to change the allocator that they're running with from | ||
time to time. This use case is distinct from an allocator-per-collection (e.g. a | ||
`Vec` with a custom allocator) and instead is more related to changing the | ||
global default allocator, e.g. what `Vec<T>` uses by default. | ||
|
||
Currently Rust programs don't have a specified global allocator. The compiler | ||
may link to a version of [jemalloc] on some platforms, but this is not | ||
guaranteed. Libraries, however, like cdylibs and staticlibs are guaranteed | ||
to use the "system allocator" which means something like `malloc` on Unixes and | ||
`HeapAlloc` on Windows. | ||
|
||
[jemalloc]: https://github.com/jemalloc/jemalloc | ||
|
||
The `#[global_allocator]` attribute, however, allows configuring this choice. | ||
You can use this to implement a completely custom global allocator to route all | ||
default allocation requests to a custom object. Defined in [RFC 1974] usage | ||
looks like: | ||
|
||
[RFC 1974]: https://github.com/rust-lang/rfcs/pull/1974 | ||
|
||
```rust | ||
#![feature(global_allocator, heap_api)] | ||
|
||
use std::heap::{Alloc, System, Layout, AllocErr}; | ||
|
||
struct MyAllocator; | ||
|
||
unsafe impl<'a> Alloc for &'a MyAllocator { | ||
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { | ||
System.alloc(layout) | ||
} | ||
|
||
unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { | ||
System.dealloc(ptr, layout) | ||
} | ||
} | ||
|
||
#[global_allocator] | ||
static GLOBAL: MyAllocator = MyAllocator; | ||
|
||
fn main() { | ||
// This `Vec` will allocate memory through `GLOBAL` above | ||
let mut v = Vec::new(); | ||
v.push(1); | ||
} | ||
``` | ||
|
||
And that's it! The `#[global_allocator]` attribute is applied to a `static` | ||
which implements the `Alloc` trait in the `std::heap` module. Note, though, | ||
that the implementation is defined for `&MyAllocator`, not just `MyAllocator`. | ||
You may wish, however, to also provide `Alloc for MyAllocator` for other use | ||
cases. | ||
|
||
A crate can only have one instance of `#[global_allocator]` and this instance | ||
may be loaded through a dependency. For example `#[global_allocator]` above | ||
could have been placed in one of the dependencies loaded through `extern crate`. | ||
|
||
Note that `Alloc` itself is an `unsafe` trait, with much documentation on the | ||
trait itself about usage and for implementors. Extra care should be taken when | ||
implementing a global allocator as well as the allocator may be called from many | ||
portions of the standard library, such as the panicking routine. As a result it | ||
is highly recommended to not panic during allocation and work in as many | ||
situations with as few dependencies as possible as well. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.