rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.99k stars 12.54k forks source link

Alloc: Clarify supported Layouts #44557

Open joshlf opened 7 years ago

joshlf commented 7 years ago

TLDR

The Alloc trait doesn't currently document what Layouts are guaranteed to be supported, which leaves consumers unsure whether their allocations will fail with AllocErr::Unsupported. This issue proposes addressing this by introducing a distinction between "general" and "specialized" allocators, and documenting a set of required Layouts that the former must be able to handle.

Problem Statement

One of the errors that the Alloc trait's allocation methods can return is AllocErr::Unsupported, which indicates that the allocator doesn't support the requested Layout. Currently, the Alloc trait places no restrictions - either using the type system or in documentation - on what Layouts must be supported. Thus, consumers of a generic Alloc (as opposed to a particular type that implements Alloc) must assume that any given allocation request might be unsupported.

This creates a problem: in order for a consumer of an Alloc to be able to guarantee to their users that they will not crash at runtime (after all, having gotten AllocErr::Unsupported, there's really no other recourse other than to abort or bubble up the error), they need to document the set of Layouts that they might request from an allocator provided by the user. This, in turn, requires that all implementations of Alloc document precisely which Layouts they can handle so that users can ensure that they're upholding the requirements of the consumers. Even if all of this documentation was written and maintained, it'd impose a very large burden on the users. Imagine every time you wanted to use Vec<T, A: Alloc> for a particular, custom A, you had to carefully read the Vec documentation and the documentation for the desired allocator to ensure they were compatible.

Worse still, the current mechanism for configuring the global allocator involves providing an instance of Alloc. In this case, there's no way for code to communicate to the person configuring the global allocator what their requirements are, since that code might be buried behind an arbitrary number of dependencies (e.g., I use crate foo which depends on crate bar which depends on crate baz which is incompatible with the global allocator I've configured).

This came up in practice for me while working on my slab-alloc crate (which is basically just an object cache). I allow users to provide a custom Alloc to back a SlabAlloc instance, but I currently have no way other than in documentation to ensure that the provided Allocs will be compatible with the allocations I perform. If, for example, a user were to provide an allocator incapable of providing page-aligned allocations, or if somebody upstream of my crate configured a global allocator with this limitation, my code would crash at runtime.

Proposal

In order to address this issue, I propose introducing (in the Alloc trait's documentation) the notion of a "general allocator," which is an implementation of the Alloc trait which guarantees the ability to handle a certain class of standard allocation requests.

All implementations of Alloc are assumed to be general allocators unless documented to the contrary. All consumers of an Alloc type parameter are assumed to be compatible with a general allocator (that is, they do not perform any allocations which are outside of the set of guaranteed-to-be-supported allocations) unless documented to the contrary.

An allocation is guaranteed to be supported so long as it meets the following criteria:

This system does not hamstring special-case uses. Alloc implementations which do not provide all of these guarantees merely need to document this. Alloc consumers which require more than what a general allocator guarantees merely need to document this, placing the onus on their users to provide an appropriate Alloc.

Open questions

Mark-Simulacrum commented 7 years ago

cc @alexcrichton

alexcrichton commented 7 years ago

AFAIK "unsupported" is meant for "niche allocators" where a general purpose allocate probably will never be returned from a general purpose allocator.

joshlf commented 7 years ago

Was that the idea? RFC 1398 doesn't give any explanation of when an allocator may return AllocErr::Unsupported, and neither do the current docs. @nikomatsakis , did you have thoughts on this when you wrote that RFC?

If we decide that that's the right answer, then we should at least document that. Documenting that requires introducing a notion of a "general purpose allocator," so I suspect that in documenting it, we'll end up with something pretty similar to what I proposed anyway, if perhaps a little less formal.

pnkfelix commented 6 years ago

@joshlf The intention of AllocErr::Unsupported, as I recall, was to allow an allocator to indicate "this request is not satisfiable by me, and never will be, regardless of what memory you deallocate or what allocation patterns you use in the future."

See also the comment above Unsupported here: https://github.com/nox/rust-rfcs/blob/master/text/1398-kinds-of-allocators.md#allocerr-api

I'm inclined to agree with @alexcrichton that a general purpose global allocator should not be returning Unsupported ... the only exception I could imagine would be for a zero-sized allocation request...

joshlf commented 6 years ago

Some clarifying questions to follow up:

SimonSapin commented 6 years ago

@joshlf’s first point is relevant to https://github.com/rust-lang/rust/issues/45955, though Layout::from_size_align currently does not check whether the size is a multiple of the alignment.

SimonSapin commented 6 years ago

Update: GlobalAlloc and Layout were stabilized in Rust 1.28 with:

The Alloc trait is still unstable.