diff options
| author | Simon Sapin <simon.sapin@exyr.org> | 2018-05-31 20:22:59 +0200 |
|---|---|---|
| committer | Simon Sapin <simon.sapin@exyr.org> | 2018-06-11 13:48:57 -0700 |
| commit | a24924f6834ea6e5bd813d006a12aef8e5dbd5e9 (patch) | |
| tree | 8af929586f9bc74184b8682ebaa486c2a97a8dfa /src/libstd | |
| parent | fd6e08a1e6bbccd00e70b23ac72dd9a9a633be30 (diff) | |
| download | rust-a24924f6834ea6e5bd813d006a12aef8e5dbd5e9.tar.gz rust-a24924f6834ea6e5bd813d006a12aef8e5dbd5e9.zip | |
Move Unstable Book sections for #[global_allocator] and System to std::alloc docs
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/alloc.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/src/libstd/alloc.rs b/src/libstd/alloc.rs index 6cae8aaa3db..ae74a71dd06 100644 --- a/src/libstd/alloc.rs +++ b/src/libstd/alloc.rs @@ -9,6 +9,68 @@ // except according to those terms. //! Memory allocation APIs +//! +//! In a given program, the standard library has one “global” memory allocator +//! that is used for example by `Box<T>` and `Vec<T>`. +//! +//! Currently the default global allocator is unspecified. +//! The compiler may link to a version of [jemalloc] on some platforms, +//! but this is not guaranteed. +//! Libraries, however, like `cdylib`s and `staticlib`s are guaranteed +//! to use the [`System`] by default. +//! +//! [jemalloc]: https://github.com/jemalloc/jemalloc +//! [`System`]: struct.System.html +//! +//! # The `#[global_allocator]` attribute +//! +//! This attribute allows configuring the choice of global allocator. +//! You can use this to implement a completely custom global allocator +//! to route all default allocation requests to a custom object. +//! +//! ```rust +//! use std::alloc::{GlobalAlloc, System, Layout}; +//! +//! struct MyAllocator; +//! +//! unsafe impl GlobalAlloc for MyAllocator { +//! unsafe fn alloc(&self, layout: Layout) -> *mut u8 { +//! System.alloc(layout) +//! } +//! +//! unsafe fn dealloc(&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); +//! } +//! ``` +//! +//! The attribute is used on a `static` item whose type implements the +//! [`GlobalAlloc`] trait. This type can be provided by an external library: +//! +//! [`GlobalAlloc`]: ../../core/alloc/trait.GlobalAlloc.html +//! +//! ```rust,ignore (demonstrates crates.io usage) +//! extern crate jemallocator; +//! +//! use jemallacator::Jemalloc; +//! +//! #[global_allocator] +//! static GLOBAL: Jemalloc = Jemalloc; +//! +//! fn main() {} +//! ``` +//! +//! The `#[global_allocator]` can only be used once in a crate +//! or its recursive dependencies. #![stable(feature = "alloc_module", since = "1.28.0")] |
