rust/src/libcore
bors 99d4886ead Auto merge of #49669 - SimonSapin:global-alloc, r=alexcrichton
Add GlobalAlloc trait + tweaks for initial stabilization

This is the outcome of discussion at the Rust All Hands in Berlin. The high-level goal is stabilizing sooner rather than later the ability to [change the global allocator](https://github.com/rust-lang/rust/issues/27389), as well as allocating memory without abusing `Vec::with_capacity` + `mem::forget`.

Since we’re not ready to settle every detail of the `Alloc` trait for the purpose of collections that are generic over the allocator type (for example the possibility of a separate trait for deallocation only, and what that would look like exactly), we propose introducing separately **a new `GlobalAlloc` trait**, for use with the `#[global_allocator]` attribute.

We also propose a number of changes to existing APIs. They are batched in this one PR in order to minimize disruption to Nightly users.

The plan for initial stabilization is detailed in the tracking issue https://github.com/rust-lang/rust/issues/49668.

CC @rust-lang/libs, @glandium

## Immediate breaking changes to unstable features

* For pointers to allocated memory, change the pointed type from `u8` to `Opaque`, a new public [extern type](https://github.com/rust-lang/rust/issues/43467). Since extern types are not `Sized`, `<*mut _>::offset` cannot be used without first casting to another pointer type. (We hope that extern types can also be stabilized soon.)
* In the `Alloc` trait, change these pointers to `ptr::NonNull` and change the `AllocErr` type to a zero-size struct. This makes return types `Result<ptr::NonNull<Opaque>, AllocErr>` be pointer-sized.
* Instead of a new `Layout`, `realloc` takes only a new size (in addition to the pointer and old `Layout`). Changing the alignment is not supported with `realloc`.
* Change the return type of `Layout::from_size_align` from `Option<Self>` to `Result<Self, LayoutErr>`, with `LayoutErr` a new opaque struct.
* A `static` item registered as the global allocator with the `#[global_allocator]` **must now implement the new `GlobalAlloc` trait** instead of `Alloc`.

## Eventually-breaking changes to unstable features, with a deprecation period

* Rename the respective `heap` modules to `alloc` in the `core`, `alloc`, and `std` crates. (Yes, this does mean that `::alloc::alloc::Alloc::alloc` is a valid path to a trait method if you have `exetrn crate alloc;`)
* Rename the the `Heap` type to `Global`, since it is the entry point for what’s registered with `#[global_allocator]`.

Old names remain available for now, as deprecated `pub use` reexports.

## Backward-compatible changes

* Add a new [extern type](https://github.com/rust-lang/rust/issues/43467) `Opaque`, for use in pointers to allocated memory.
* Add a new `GlobalAlloc` trait shown below. Unlike `Alloc`, it uses bare `*mut Opaque` without `NonNull` or `Result`. NULL in return values indicates an error (of unspecified nature). This is easier to implement on top of `malloc`-like APIs.
* Add impls of `GlobalAlloc` for both the `Global` and `System` types, in addition to existing impls of `Alloc`. This enables calling `GlobalAlloc` methods on the stable channel before `Alloc` is stable. Implementing two traits with identical method names can make some calls ambiguous, but most code is expected to have no more than one of the two traits in scope. Erroneous code like `use std::alloc::Global; #[global_allocator] static A: Global = Global;` (where `Global` is defined to call itself, causing infinite recursion) is not statically prevented by the type system, but we count on it being hard enough to do accidentally and easy enough to diagnose.

```rust
extern {
    pub type Opaque;
}

pub unsafe trait GlobalAlloc {
    unsafe fn alloc(&self, layout: Layout) -> *mut Opaque;
    unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout);

    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Opaque {
        // Default impl: self.alloc() and ptr::write_bytes()
    }
    unsafe fn realloc(&self, ptr: *mut Opaque, old_layout: Layout, new_size: usize) -> *mut Opaque {
        // Default impl: self.alloc() and ptr::copy_nonoverlapping() and self.dealloc()
    }

    fn oom(&self) -> ! {
        // intrinsics::abort
    }

    // More methods with default impls may be added in the future
}
```

## Bikeshed

The tracking issue https://github.com/rust-lang/rust/issues/49668 lists some open questions. If consensus is reached before this PR is merged, changes can be integrated.
2018-04-13 10:33:51 +00:00
..
benches Move deny(warnings) into rustbuild 2018-04-08 16:59:14 -06:00
char Dedicated tracking issue for UnicodeVersion and UNICODE_VERSION. 2018-04-12 00:13:53 +02:00
fmt tweak fmt::Arguments docs 2018-04-03 09:11:41 -04:00
hash Fix a few more unstables that I missed 2018-03-26 08:37:56 -05:00
iter Auto merge of #49673 - ollie27:stab, r=sfackler 2018-04-09 03:32:32 +00:00
num Auto merge of #49389 - fanzier:euclidean-division, r=KodrAus 2018-04-13 07:34:37 +00:00
ops Remove all unstable placement features 2018-04-03 11:02:34 +02:00
prelude Remove the CharExt trait, now that libcore has inherent methods for char 2018-04-12 00:13:52 +02:00
slice Deprecate offset_to; switch core&alloc to using offset_from instead 2018-03-31 22:35:37 -07:00
str Merge core::unicode::str into core::str 2018-04-12 00:13:52 +02:00
sync Rollup merge of #48658 - llogiq:no-more-cas, r=kennytm 2018-04-05 16:51:16 +08:00
tests Move Utf8Lossy decoder to libcore 2018-04-12 00:13:43 +02:00
unicode Mark the rest of the unicode feature flag as perma-unstable. 2018-04-12 00:13:53 +02:00
alloc.rs Initial docs for the GlobalAlloc trait 2018-04-12 22:53:22 +02:00
any.rs stage0 cfg cleanup 2018-02-20 08:52:33 -07:00
array.rs Stabilize the TryFrom and TryInto traits 2018-03-26 23:36:02 +02:00
ascii.rs Correct a few stability attributes 2018-04-05 15:39:29 +01:00
borrow.rs Fix formatting. 2018-03-18 13:05:00 +01:00
Cargo.toml Update Cargo submodule 2018-03-11 10:59:28 -07:00
cell.rs Remove deprecated unstable ptr::Shared type alias. 2018-03-17 23:59:35 +01:00
clone.rs Auto merge of #49642 - kennytm:rollup, r=kennytm 2018-04-04 21:12:18 +00:00
cmp.rs Bump the bootstrap compiler to 1.26.0 beta 2018-04-05 07:13:45 -07:00
convert.rs Stabilize the TryFrom and TryInto traits 2018-03-26 23:36:02 +02:00
default.rs Fix "Quasi-quoting is inefficient" warning in incremental rustbuild. 2017-07-18 01:49:40 +08:00
internal_macros.rs fix version number 2017-09-21 15:46:17 -04:00
intrinsics.rs Bump the bootstrap compiler to 1.26.0 beta 2018-04-05 07:13:45 -07:00
iter_private.rs Various fixes to wording consistency in the docs 2017-03-22 17:19:52 +01:00
lib.rs Actually deprecate heap modules. 2018-04-12 22:52:47 +02:00
macros.rs Bump the bootstrap compiler to 1.26.0 beta 2018-04-05 07:13:45 -07:00
marker.rs Document builtin implementations of Clone and Copy 2018-03-26 23:42:29 -04:00
mem.rs impl Unpin for Pin 2018-04-03 14:30:07 +02:00
nonzero.rs num::NonZero* types now have their own tracking issue: #49137 2018-03-18 16:58:38 +01:00
option.rs Remove uses of option_filter feature 2018-04-05 00:02:33 -06:00
panic.rs Correct a few stability attributes 2018-04-05 15:39:29 +01:00
panicking.rs Bump the bootstrap compiler to 1.26.0 beta 2018-04-05 07:13:45 -07:00
ptr.rs Rename alloc::Void to alloc::Opaque 2018-04-12 22:53:22 +02:00
raw.rs Fix up various links 2017-03-20 10:10:16 -04:00
result.rs core: Update stability attributes for FusedIterator 2018-03-03 14:23:05 +01:00
time.rs Correct a few stability attributes 2018-02-10 21:20:42 +00:00
tuple.rs Update bootstrap compiler 2017-08-31 06:58:58 -07:00
unit.rs impl FromIterator<()> for () 2017-10-18 23:12:37 -07:00