Skip to content

Commit 5e49341

Browse files
committed
Move Unstable Book sections for #[global_allocator] and System to std::alloc docs
1 parent 54d7daf commit 5e49341

File tree

4 files changed

+85
-148
lines changed

4 files changed

+85
-148
lines changed

src/doc/unstable-book/src/language-features/global-allocator.md

-72
This file was deleted.

src/doc/unstable-book/src/library-features/alloc-system.md

-76
This file was deleted.

src/liballoc_system/lib.rs

+23
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ use core::alloc::{Alloc, GlobalAlloc, AllocErr, Layout};
4444
use core::ptr::NonNull;
4545

4646
/// The default memory allocator provided by the operating system.
47+
///
48+
/// This is based on `malloc` on Unix platforms and `HeapAlloc` on Windows,
49+
/// plus related functions.
50+
///
51+
/// This type can be used in a `static` item
52+
/// with the `#[global_allocator]` attribute
53+
/// to force the global allocator to be the system’s one.
54+
/// (The default is jemalloc for executables, on some platforms.)
55+
///
56+
/// ```rust
57+
/// use std::alloc::System;
58+
///
59+
/// #[global_allocator]
60+
/// static A: System = System;
61+
///
62+
/// fn main() {
63+
/// let a = Box::new(4); // Allocates from the system allocator.
64+
/// println!("{}", a);
65+
/// }
66+
/// ```
67+
///
68+
/// It can also be used directly to allocate memory
69+
/// independently of the standard library’s global allocator.
4770
#[stable(feature = "alloc_system_type", since = "1.28.0")]
4871
pub struct System;
4972

src/libstd/alloc.rs

+62
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,68 @@
99
// except according to those terms.
1010

1111
//! Memory allocation APIs
12+
//!
13+
//! In a given program, the standard library has one “global” memory allocator
14+
//! that is used for example by `Box<T>` and `Vec<T>`.
15+
//!
16+
//! Currently the default global allocator is unspecified.
17+
//! The compiler may link to a version of [jemalloc] on some platforms,
18+
//! but this is not guaranteed.
19+
//! Libraries, however, like `cdylib`s and `staticlib`s are guaranteed
20+
//! to use the [`System`] by default.
21+
//!
22+
//! [jemalloc]: https://github.com/jemalloc/jemalloc
23+
//! [`System`]: struct.System.html
24+
//!
25+
//! # The `#[global_allocator]` attribute
26+
//!
27+
//! This attribute allows configuring the choice of global allocator.
28+
//! You can use this to implement a completely custom global allocator
29+
//! to route all default allocation requests to a custom object.
30+
//!
31+
//! ```rust
32+
//! use std::alloc::{GlobalAlloc, System, Layout};
33+
//!
34+
//! struct MyAllocator;
35+
//!
36+
//! unsafe impl GlobalAlloc for MyAllocator {
37+
//! unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
38+
//! System.alloc(layout)
39+
//! }
40+
//!
41+
//! unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
42+
//! System.dealloc(ptr, layout)
43+
//! }
44+
//! }
45+
//!
46+
//! #[global_allocator]
47+
//! static GLOBAL: MyAllocator = MyAllocator;
48+
//!
49+
//! fn main() {
50+
//! // This `Vec` will allocate memory through `GLOBAL` above
51+
//! let mut v = Vec::new();
52+
//! v.push(1);
53+
//! }
54+
//! ```
55+
//!
56+
//! The attribute is used on a `static` item whose type implements the
57+
//! [`GlobalAlloc`] trait. This type can be provided by an external library:
58+
//!
59+
//! [`GlobalAlloc`]: ../../core/alloc/trait.GlobalAlloc.html
60+
//!
61+
//! ```rust,ignore (demonstrates crates.io usage)
62+
//! extern crate jemallocator;
63+
//!
64+
//! use jemallacator::Jemalloc;
65+
//!
66+
//! #[global_allocator]
67+
//! static GLOBAL: Jemalloc = Jemalloc;
68+
//!
69+
//! fn main() {}
70+
//! ```
71+
//!
72+
//! The `#[global_allocator]` can only be used once in a crate
73+
//! or its recursive dependencies.
1274
1375
#![stable(feature = "alloc_module", since = "1.28.0")]
1476

0 commit comments

Comments
 (0)