about summary refs log tree commit diff
path: root/src/doc
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-06-12 00:36:21 +0000
committerbors <bors@rust-lang.org>2018-06-12 00:36:21 +0000
commit4367e41ea2a105c373de27c2f080fc2527cc6927 (patch)
tree9ca30ec2dbf3311f0a0d0d6275d5abca8f23fc1f /src/doc
parentf9944fde377b8b2575f96fff60f784eddce54002 (diff)
parent7f0d54d98842c786ab7a140c17c3ea32aea6aead (diff)
downloadrust-4367e41ea2a105c373de27c2f080fc2527cc6927.tar.gz
rust-4367e41ea2a105c373de27c2f080fc2527cc6927.zip
Auto merge of #51241 - glandium:globalalloc, r=sfackler,SimonSapin
Stabilize GlobalAlloc and #[global_allocator]

This PR implements the changes discussed in https://github.com/rust-lang/rust/issues/49668#issuecomment-393263510

Fixes #49668
Fixes #27389

This does not change the default global allocator: #36963
Diffstat (limited to 'src/doc')
-rw-r--r--src/doc/unstable-book/src/language-features/global-allocator.md72
-rw-r--r--src/doc/unstable-book/src/library-features/alloc-jemalloc.md13
-rw-r--r--src/doc/unstable-book/src/library-features/alloc-system.md77
3 files changed, 0 insertions, 162 deletions
diff --git a/src/doc/unstable-book/src/language-features/global-allocator.md b/src/doc/unstable-book/src/language-features/global-allocator.md
deleted file mode 100644
index 8f1ba22de8c..00000000000
--- a/src/doc/unstable-book/src/language-features/global-allocator.md
+++ /dev/null
@@ -1,72 +0,0 @@
-# `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, allocator_api, heap_api)]
-
-use std::alloc::{GlobalAlloc, System, Layout, Opaque};
-use std::ptr::NonNull;
-
-struct MyAllocator;
-
-unsafe impl GlobalAlloc for MyAllocator {
-    unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
-        System.alloc(layout)
-    }
-
-    unsafe fn dealloc(&self, ptr: *mut Opaque, 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::alloc` 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.
diff --git a/src/doc/unstable-book/src/library-features/alloc-jemalloc.md b/src/doc/unstable-book/src/library-features/alloc-jemalloc.md
deleted file mode 100644
index 425d4cb79b2..00000000000
--- a/src/doc/unstable-book/src/library-features/alloc-jemalloc.md
+++ /dev/null
@@ -1,13 +0,0 @@
-# `alloc_jemalloc`
-
-The tracking issue for this feature is: [#33082]
-
-[#33082]: https://github.com/rust-lang/rust/issues/33082
-
-See also [`alloc_system`](library-features/alloc-system.html).
-
-------------------------
-
-This feature has been replaced by [the `jemallocator` crate on crates.io.][jemallocator].
-
-[jemallocator]: https://crates.io/crates/jemallocator
diff --git a/src/doc/unstable-book/src/library-features/alloc-system.md b/src/doc/unstable-book/src/library-features/alloc-system.md
deleted file mode 100644
index 9effab202ca..00000000000
--- a/src/doc/unstable-book/src/library-features/alloc-system.md
+++ /dev/null
@@ -1,77 +0,0 @@
-# `alloc_system`
-
-The tracking issue for this feature is: [#32838]
-
-[#32838]: https://github.com/rust-lang/rust/issues/32838
-
-See also [`global_allocator`](language-features/global-allocator.html).
-
-------------------------
-
-The compiler currently ships two default allocators: `alloc_system` and
-`alloc_jemalloc` (some targets don't have jemalloc, however). These allocators
-are normal Rust crates and contain an implementation of the routines to
-allocate and deallocate memory. The standard library is not compiled assuming
-either one, and the compiler will decide which allocator is in use at
-compile-time depending on the type of output artifact being produced.
-
-Binaries generated by the compiler will use `alloc_jemalloc` by default (where
-available). In this situation the compiler "controls the world" in the sense of
-it has power over the final link. Primarily this means that the allocator
-decision can be left up the compiler.
-
-Dynamic and static libraries, however, will use `alloc_system` by default. Here
-Rust is typically a 'guest' in another application or another world where it
-cannot authoritatively decide what allocator is in use. As a result it resorts
-back to the standard APIs (e.g. `malloc` and `free`) for acquiring and releasing
-memory.
-
-# Switching Allocators
-
-Although the compiler's default choices may work most of the time, it's often
-necessary to tweak certain aspects. Overriding the compiler's decision about
-which allocator is in use is done through the `#[global_allocator]` attribute:
-
-```rust,no_run
-#![feature(alloc_system, global_allocator, allocator_api)]
-
-extern crate alloc_system;
-
-use alloc_system::System;
-
-#[global_allocator]
-static A: System = System;
-
-fn main() {
-    let a = Box::new(4); // Allocates from the system allocator.
-    println!("{}", a);
-}
-```
-
-In this example the binary generated will not link to jemalloc by default but
-instead use the system allocator. Conversely to generate a dynamic library which
-uses jemalloc by default one would write:
-
-(The `alloc_jemalloc` crate cannot be used to control the global allocator,
-crate.io’s `jemallocator` crate provides equivalent functionality.)
-
-```toml
-# Cargo.toml
-[dependencies]
-jemallocator = "0.1"
-```
-```rust,ignore
-#![feature(global_allocator)]
-#![crate_type = "dylib"]
-
-extern crate jemallocator;
-
-#[global_allocator]
-static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
-
-pub fn foo() {
-    let a = Box::new(4); // Allocates from jemalloc.
-    println!("{}", a);
-}
-# fn main() {}
-```