rust-lang / wg-allocators

Home of the Allocators working group: Paving a path for a standard set of allocator traits to be used in collections!
http://bit.ly/hello-wg-allocators
205 stars 9 forks source link

Add best-effort `owns` method? #52

Closed lachlansneff closed 4 years ago

lachlansneff commented 4 years ago

AllocRef should have an owns method for determining if a particular allocation came from specific allocation. Obviously, many allocators couldn't know for sure, so it would have to be best effort.

fn owns(&self, ptr: NonNull<u8>, layout: Layout) -> bool;

or

pub enum Owns {
    Yes,
    Maybe,
}

fn owns(&self, ptr: NonNull<u8>, layout: Layout) -> Owns;

This could be very useful for implementing combinatorial allocators, like in https://www.youtube.com/watch?v=LIb3L4vKZ7U.

Amanieu commented 4 years ago

This was previously discussed here: https://github.com/rust-lang/rust/issues/44302

The conclusion was that it isn't implementable (or at least not efficiently) for the majority of allocator designs. This is best left to a separate trait for allocators that specifically support composability.

lachlansneff commented 4 years ago

Ah, that makes sense.