Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd GlobalAlloc trait + tweaks for initial stabilization #49669
Conversation
rust-highfive
assigned
shepmaster
Apr 4, 2018
This was referenced Apr 4, 2018
This comment has been minimized.
This comment has been minimized.
|
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
This was referenced Apr 4, 2018
SimonSapin
added
A-allocators
T-libs
labels
Apr 4, 2018
This comment has been minimized.
This comment has been minimized.
I moved this |
This comment has been minimized.
This comment has been minimized.
|
I’ve added an exception in tidy. |
glandium
reviewed
Apr 4, 2018
| @@ -756,7 +753,7 @@ mod tests { | |||
| // before allocation attempts start failing. | |||
| struct BoundedAlloc { fuel: usize } | |||
| unsafe impl Alloc for BoundedAlloc { | |||
| unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { | |||
| unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> { | |||
This comment has been minimized.
This comment has been minimized.
glandium
reviewed
Apr 4, 2018
| @@ -737,7 +737,7 @@ impl<T: Clone> RcFromSlice<T> for Rc<[T]> { | |||
| // In the event of a panic, elements that have been written | |||
| // into the new RcBox will be dropped, then the memory freed. | |||
| struct Guard<T> { | |||
| mem: *mut u8, | |||
| mem: NonNull<u8>, | |||
This comment has been minimized.
This comment has been minimized.
Amanieu
reviewed
Apr 4, 2018
| @@ -9,6 +9,7 @@ path = "lib.rs" | |||
|
|
|||
| [dependencies] | |||
| core = { path = "../libcore" } | |||
| libc = { path = "../rustc/libc_shim" } | |||
This comment has been minimized.
This comment has been minimized.
Amanieu
Apr 4, 2018
Contributor
liballoc should not depend on libc. This will break code that runs in kernel-like environments where you have a global memory allocator but no libc.
This comment has been minimized.
This comment has been minimized.
SimonSapin
Apr 4, 2018
Author
Contributor
What if libc was only a dependency for cfg(any(unix, target_os = "redox"))? It’s only used there.
This comment has been minimized.
This comment has been minimized.
Amanieu
Apr 4, 2018
Contributor
I would prefer if liballoc was completely OS-independent. I am currently using Rust to make Linux binaries which do not link to libc (just using raw syscalls).
This is why, when I originally implemented printing a message on OOM (#30801), I allowed the OOM to be dynamically set at runtime. It would default to intrinsics::abort, but would be overridden by libstd on startup to a handler that prints a message.
This comment has been minimized.
This comment has been minimized.
SimonSapin
Apr 4, 2018
Author
Contributor
So you’re saying that a oom method should be added to the GlobalAlloc trait so that #[global_allocator] can forward it?
This comment has been minimized.
This comment has been minimized.
Amanieu
Apr 4, 2018
Contributor
I was thinking of adding back a dynamic OOM handler, but your idea is better.
This comment has been minimized.
This comment has been minimized.
SimonSapin
Apr 5, 2018
Author
Contributor
What does dynamic handler mean, if not the same as in my previous comment?
This comment has been minimized.
This comment has been minimized.
SimonSapin
Apr 5, 2018
Author
Contributor
Never mind, I see later in the thread that you mention set_oom_handler.
Amanieu
reviewed
Apr 4, 2018
| } | ||
| } | ||
|
|
||
| /// The `CannotReallocInPlace` error is used when `grow_in_place` or | ||
| /// The `CannotgInPlace` error is used when `grow_in_place` or |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
This comment has been minimized.
This comment has been minimized.
I don't understand the reasoning behind this. Isn't |
This comment has been minimized.
This comment has been minimized.
|
@Amanieu no one will be using the |
This comment has been minimized.
This comment has been minimized.
|
Well if it’s the only thing available on Stable, people (at least some library authors) will definitely use |
This comment has been minimized.
This comment has been minimized.
|
These tests already regressed at an intermediate commit for this PR and rust-lang/llvm#110 fixed them at the time, but now they regressed again and I don’t know why.
|
This comment has been minimized.
This comment has been minimized.
|
What is the allocator going to be doing in the oom method? I don't really see why e.g. jemalloc would handle that differently than the system allocator. |
This comment has been minimized.
This comment has been minimized.
|
A little too big for me to handle |
rust-highfive
assigned
alexcrichton
and unassigned
shepmaster
Apr 5, 2018
This comment has been minimized.
This comment has been minimized.
|
You are right that jemalloc would handle this the same way as the system allocator. However in a There are two ways to support this functionality:
Both of these will work for |
This comment has been minimized.
This comment has been minimized.
|
But if you want to customize that behavior then sticking it on |
This comment has been minimized.
This comment has been minimized.
|
Your PR failed on Travis. Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
This comment has been minimized.
This comment has been minimized.
|
@sfackler You could use the same argument to say that the |
This comment has been minimized.
This comment has been minimized.
|
Yeah, Alloc::oom doesn't make much sense to me either. |
bors
added a commit
that referenced
this pull request
Apr 13, 2018
This comment has been minimized.
This comment has been minimized.
|
|
bors
merged commit c5ffdd7
into
rust-lang:master
Apr 13, 2018
This was referenced Apr 13, 2018
SimonSapin
deleted the
SimonSapin:global-alloc
branch
Apr 13, 2018
SimonSapin
added a commit
to SimonSapin/jemallocator
that referenced
this pull request
Apr 15, 2018
SimonSapin
added a commit
to SimonSapin/jemallocator
that referenced
this pull request
Apr 15, 2018
SimonSapin
referenced this pull request
Apr 15, 2018
Merged
Implement the GlobalAlloc trait, upgrade to nightly-2018-04-15 #40
SimonSapin
added a commit
to servo/servo
that referenced
this pull request
Apr 15, 2018
SimonSapin
referenced this pull request
Apr 15, 2018
Closed
Update jemallocator, fix for nightly-2018-04-15 #20640
SimonSapin
added a commit
to servo/servo
that referenced
this pull request
Apr 15, 2018
bors-servo
added a commit
to servo/servo
that referenced
this pull request
Apr 15, 2018
SimonSapin
added a commit
to servo/servo
that referenced
this pull request
Apr 15, 2018
SimonSapin
referenced this pull request
Apr 15, 2018
Merged
Fork the jemallocator crate, fix for nightly-2018-04-15 #20641
SimonSapin
added a commit
to servo/servo
that referenced
this pull request
Apr 15, 2018
SimonSapin
added a commit
to servo/servo
that referenced
this pull request
Apr 15, 2018
SimonSapin
added a commit
to servo/servo
that referenced
this pull request
Apr 15, 2018
dwrensha
added a commit
to dwrensha/seer
that referenced
this pull request
Apr 15, 2018
jonhoo
added a commit
to jonhoo/arccstr
that referenced
this pull request
Apr 15, 2018
SimonSapin
added a commit
to servo/servo
that referenced
this pull request
Apr 16, 2018
bors-servo
added a commit
to servo/servo
that referenced
this pull request
Apr 16, 2018
This comment has been minimized.
This comment has been minimized.
|
So this removed the specializations of Why? And where should these be re-added? |
This comment has been minimized.
This comment has been minimized.
|
These still exist in https://crates.io/crates/jemallocator. The |
SimonSapin commentedApr 4, 2018
•
edited
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, as well as allocating memory without abusing
Vec::with_capacity+mem::forget.Since we’re not ready to settle every detail of the
Alloctrait 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 newGlobalAlloctrait, 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 #49668.
CC @rust-lang/libs, @glandium
Immediate breaking changes to unstable features
u8toOpaque, a new public extern type. Since extern types are notSized,<*mut _>::offsetcannot be used without first casting to another pointer type. (We hope that extern types can also be stabilized soon.)Alloctrait, change these pointers toptr::NonNulland change theAllocErrtype to a zero-size struct. This makes return typesResult<ptr::NonNull<Opaque>, AllocErr>be pointer-sized.Layout,realloctakes only a new size (in addition to the pointer and oldLayout). Changing the alignment is not supported withrealloc.Layout::from_size_alignfromOption<Self>toResult<Self, LayoutErr>, withLayoutErra new opaque struct.staticitem registered as the global allocator with the#[global_allocator]must now implement the newGlobalAlloctrait instead ofAlloc.Eventually-breaking changes to unstable features, with a deprecation period
heapmodules toallocin thecore,alloc, andstdcrates. (Yes, this does mean that::alloc::alloc::Alloc::allocis a valid path to a trait method if you haveexetrn crate alloc;)Heaptype toGlobal, since it is the entry point for what’s registered with#[global_allocator].Old names remain available for now, as deprecated
pub usereexports.Backward-compatible changes
Opaque, for use in pointers to allocated memory.GlobalAlloctrait shown below. UnlikeAlloc, it uses bare*mut OpaquewithoutNonNullorResult. NULL in return values indicates an error (of unspecified nature). This is easier to implement on top ofmalloc-like APIs.GlobalAllocfor both theGlobalandSystemtypes, in addition to existing impls ofAlloc. This enables callingGlobalAllocmethods on the stable channel beforeAllocis 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 likeuse std::alloc::Global; #[global_allocator] static A: Global = Global;(whereGlobalis 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.Bikeshed
The tracking issue #49668 lists some open questions. If consensus is reached before this PR is merged, changes can be integrated.