Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocator traits and std::heap #32838

Open
nikomatsakis opened this issue Apr 8, 2016 · 412 comments
Open

Allocator traits and std::heap #32838

nikomatsakis opened this issue Apr 8, 2016 · 412 comments

Comments

@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Apr 8, 2016

馃摙 This feature has a dedicated working group, please direct comments and concerns to the working group's repo.

Original Post:


FCP proposal: #32838 (comment)
FCP checkboxes: #32838 (comment)


Tracking issue for rust-lang/rfcs#1398 and the std::heap module.

  • land struct Layout, trait Allocator, and default implementations in alloc crate (#42313)
  • decide where parts should live (e.g. default impls has dependency on alloc crate, but Layout/Allocator could be in libcore...) (#42313)
  • fixme from source code: audit default implementations (in Layout for overflow errors, (potentially switching to overflowing_add and overflowing_mul as necessary).
  • decide if realloc_in_place should be replaced with grow_in_place and shrink_in_place (comment) (#42313)
  • review arguments for/against associated error type (see subthread here)
  • determine what the requirements are on the alignment provided to fn dealloc. (See discussion on allocator rfc and global allocator rfc and trait Alloc PR.)
    • Is it required to deallocate with the exact align that you allocate with? Concerns have been raised that allocators like jemalloc don't require this, and it's difficult to envision an allocator that does require this. (more discussion). @ruuda and @rkruppe look like they've got the most thoughts so far on this.
  • should AllocErr be Error instead? (comment)
  • Is it required to deallocate with the exact size that you allocate with? With the usable_size business we may wish to allow, for example, that you if you allocate with (size, align) you must deallocate with a size somewhere in the range of size...usable_size(size, align). It appears that jemalloc is totally ok with this (doesn't require you to deallocate with a precise size you allocate with) and this would also allow Vec to naturally take advantage of the excess capacity jemalloc gives it when it does an allocation. (although actually doing this is also somewhat orthogonal to this decision, we're just empowering Vec). So far @gankro has most of the thoughts on this. (@alexcrichton believes this was settled in #42313 due to the definition of "fits")
  • similar to previous question: Is it required to deallocate with the exact alignment that you allocated with? (See comment from 5 June 2017)
  • OSX/alloc_system is buggy on huge alignments (e.g. an align of 1 << 32) #30170 #43217
  • should Layout provide a fn stride(&self) method? (See also rust-lang/rfcs#1397, #17027 )
  • Allocator::owns as a method? #44302

State of std::heap after #42313:

pub struct Layout { /* ... */ }

impl Layout {
    pub fn new<T>() -> Self;
    pub fn for_value<T: ?Sized>(t: &T) -> Self;
    pub fn array<T>(n: usize) -> Option<Self>;
    pub fn from_size_align(size: usize, align: usize) -> Option<Layout>;
    pub unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Layout;

    pub fn size(&self) -> usize;
    pub fn align(&self) -> usize;
    pub fn align_to(&self, align: usize) -> Self;
    pub fn padding_needed_for(&self, align: usize) -> usize;
    pub fn repeat(&self, n: usize) -> Option<(Self, usize)>;
    pub fn extend(&self, next: Self) -> Option<(Self, usize)>;
    pub fn repeat_packed(&self, n: usize) -> Option<Self>;
    pub fn extend_packed(&self, next: Self) -> Option<(Self, usize)>;
}

pub enum AllocErr {
    Exhausted { request: Layout },
    Unsupported { details: &'static str },
}

impl AllocErr {
    pub fn invalid_input(details: &'static str) -> Self;
    pub fn is_memory_exhausted(&self) -> bool;
    pub fn is_request_unsupported(&self) -> bool;
    pub fn description(&self) -> &str;
}

pub struct CannotReallocInPlace;

pub struct Excess(pub *mut u8, pub usize);

pub unsafe trait Alloc {
    // required
    unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr>;
    unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout);

    // provided
    fn oom(&mut self, _: AllocErr) -> !;
    fn usable_size(&self, layout: &Layout) -> (usize, usize);
    unsafe fn realloc(&mut self,
                      ptr: *mut u8,
                      layout: Layout,
                      new_layout: Layout) -> Result<*mut u8, AllocErr>;
    unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr>;
    unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr>;
    unsafe fn realloc_excess(&mut self,
                             ptr: *mut u8,
                             layout: Layout,
                             new_layout: Layout) -> Result<Excess, AllocErr>;
    unsafe fn grow_in_place(&mut self,
                            ptr: *mut u8,
                            layout: Layout,
                            new_layout: Layout) -> Result<(), CannotReallocInPlace>;
    unsafe fn shrink_in_place(&mut self,
                              ptr: *mut u8,
                              layout: Layout,
                              new_layout: Layout) -> Result<(), CannotReallocInPlace>;

    // convenience
    fn alloc_one<T>(&mut self) -> Result<Unique<T>, AllocErr>
        where Self: Sized;
    unsafe fn dealloc_one<T>(&mut self, ptr: Unique<T>)
        where Self: Sized;
    fn alloc_array<T>(&mut self, n: usize) -> Result<Unique<T>, AllocErr>
        where Self: Sized;
    unsafe fn realloc_array<T>(&mut self,
                               ptr: Unique<T>,
                               n_old: usize,
                               n_new: usize) -> Result<Unique<T>, AllocErr>
        where Self: Sized;
    unsafe fn dealloc_array<T>(&mut self, ptr: Unique<T>, n: usize) -> Result<(), AllocErr>
        where Self: Sized;
}

/// The global default allocator
pub struct Heap;

impl Alloc for Heap {
    // ...
}

impl<'a> Alloc for &'a Heap {
    // ...
}

/// The "system" allocator
pub struct System;

impl Alloc for System {
    // ...
}

impl<'a> Alloc for &'a System {
    // ...
}
@gereeter
Copy link
Contributor

@gereeter gereeter commented Apr 11, 2016

I unfortunately wasn't paying close enough attention to mention this in the RFC discussion, but I think that realloc_in_place should be replaced by two functions, grow_in_place and shrink_in_place, for two reasons:

  • I can't think of a single use case (short of implementing realloc or realloc_in_place) where it is unknown whether the size of the allocation is increasing or decreasing. Using more specialized methods makes it slightly more clear what is going on.
  • The code paths for growing and shrinking allocations tend to be radically different - growing involves testing whether adjacent blocks of memory are free and claiming them, while shrinking involves carving off properly sized subblocks and freeing them. While the cost of a branch inside realloc_in_place is quite small, using grow and shrink better captures the distinct tasks that an allocator needs to perform.

Note that these can be added backwards-compatibly next to realloc_in_place, but this would constrain which functions would be by default implemented in terms of which others.

For consistency, realloc would probably also want to be split into grow and split, but the only advantage to having an overloadable realloc function that I know of is to be able to use mmap's remap option, which does not have such a distinction.

@gereeter
Copy link
Contributor

@gereeter gereeter commented Apr 11, 2016

Additionally, I think that the default implementations of realloc and realloc_in_place should be slightly adjusted - instead of checking against the usable_size, realloc should just first try to realloc_in_place. In turn, realloc_in_place should by default check against the usable size and return success in the case of a small change instead of universally returning failure.

This makes it easier to produce a high-performance implementation of realloc: all that is required is improving realloc_in_place. However, the default performance of realloc does not suffer, as the check against the usable_size is still performed.

eddyb added a commit to eddyb/rust that referenced this issue Oct 18, 2016
鈥akis

`#[may_dangle]` attribute

`#[may_dangle]` attribute

Second step of rust-lang#34761. Last big hurdle before we can work in earnest towards Allocator integration (rust-lang#32838)

Note: I am not clear if this is *also* a syntax-breaking change that needs to be part of a breaking-batch.
eddyb added a commit to eddyb/rust that referenced this issue Oct 19, 2016
鈥akis

`#[may_dangle]` attribute

`#[may_dangle]` attribute

Second step of rust-lang#34761. Last big hurdle before we can work in earnest towards Allocator integration (rust-lang#32838)

Note: I am not clear if this is *also* a syntax-breaking change that needs to be part of a breaking-batch.
eddyb added a commit to eddyb/rust that referenced this issue Oct 19, 2016
鈥akis

`#[may_dangle]` attribute

`#[may_dangle]` attribute

Second step of rust-lang#34761. Last big hurdle before we can work in earnest towards Allocator integration (rust-lang#32838)

Note: I am not clear if this is *also* a syntax-breaking change that needs to be part of a breaking-batch.
@pnkfelix
Copy link
Member

@pnkfelix pnkfelix commented Oct 26, 2016

Another issue: The doc for fn realloc_in_place says that if it returns Ok, then one is assured that ptr now "fits" new_layout.

To me this implies that it must check that the alignment of the given address matches any constraint implied by new_layout.

However, I don't think the spec for the underlying fn reallocate_inplace function implies that it will perform any such check.

  • Furthermore, it seems reasonable that any client diving into using fn realloc_in_place will themselves be ensuring that the alignments work (in practice I suspect it means that the same alignment is required everywhere for the given use case...)

So, should the implementation of fn realloc_in_place really be burdened with checking that the alignment of the given ptr is compatible with that of new_layout? It is probably better in this case (of this one method) to push that requirement back to the caller...

@pnkfelix
Copy link
Member

@pnkfelix pnkfelix commented Oct 26, 2016

@gereeter you make good points; I will add them to the check list I am accumulating in the issue description.

@pnkfelix
Copy link
Member

@pnkfelix pnkfelix commented Oct 31, 2016

(at this point I am waiting for #[may_dangle] support to ride the train into the beta channel so that I will then be able to use it for std collections as part of allocator integration)

@Ixrec Ixrec mentioned this issue Dec 10, 2016
@joshlf
Copy link
Contributor

@joshlf joshlf commented Jan 4, 2017

I'm new to Rust, so forgive me if this has been discussed elsewhere.

Is there any thought on how to support object-specific allocators? Some allocators such as slab allocators and magazine allocators are bound to a particular type, and do the work of constructing new objects, caching constructed objects which have been "freed" (rather than actually dropping them), returning already-constructed cached objects, and dropping objects before freeing the underlying memory to an underlying allocator when required.

Currently, this proposal doesn't include anything along the lines of ObjectAllocator<T>, but it would be very helpful. In particular, I'm working on an implementation of a magazine allocator object-caching layer (link above), and while I can have this only wrap an Allocator and do the work of constructing and dropping objects in the caching layer itself, it'd be great if I could also have this wrap other object allocators (like a slab allocator) and truly be a generic caching layer.

Where would an object allocator type or trait fit into this proposal? Would it be left for a future RFC? Something else?

@Ericson2314
Copy link
Contributor

@Ericson2314 Ericson2314 commented Jan 4, 2017

I don't think this has been discussed yet.

You could write your own ObjectAllocator<T>, and then do impl<T: Allocator, U> ObjectAllocator<U> for T { .. }, so that every regular allocator can serve as an object-specific allocator for all objects.

Future work would be modifying collections to use your trait for their nodes, instead of plain ole' (generic) allocators directly.

@nikomatsakis
Copy link
Contributor Author

@nikomatsakis nikomatsakis commented Jan 4, 2017

@pnkfelix

(at this point I am waiting for #[may_dangle] support to ride the train into the beta channel so that I will then be able to use it for std collections as part of allocator integration)

I guess this has happened?

@joshlf
Copy link
Contributor

@joshlf joshlf commented Jan 4, 2017

@Ericson2314 Yeah, writing my own is definitely an option for experimental purposes, but I think there'd be much more benefit to it being standardized in terms of interoperability (for example, I plan on also implementing a slab allocator, but it would be nice if a third-party user of my code could use somebody else's slab allocator with my magazine caching layer). My question is simply whether an ObjectAllocator<T> trait or something like it is worth discussing. Although it seems that it might be best for a different RFC? I'm not terribly familiar with the guidelines for how much belongs in a single RFC and when things belong in separate RFCs...

@steveklabnik
Copy link
Member

@steveklabnik steveklabnik commented Jan 4, 2017

@joshlf

Where would an object allocator type or trait fit into this proposal? Would it be left for a future RFC? Something else?

Yes, it would be another RFC.

I'm not terribly familiar with the guidelines for how much belongs in a single RFC and when things belong in separate RFCs...

that depends on the scope of the RFC itself, which is decided by the person who writes it, and then feedback is given by everyone.

But really, as this is a tracking issue for this already-accepted RFC, thinking about extensions and design changes isn't really for this thread; you should open a new one over on the RFCs repo.

@Ericson2314
Copy link
Contributor

@Ericson2314 Ericson2314 commented Jan 4, 2017

@joshlf Ah, I thought ObjectAllocator<T> was supposed to be a trait. I meant prototype the trait not a specific allocator. Yes that trait would merit its own RFC as @steveklabnik says.


@steveklabnik yeah now discussion would be better elsewhere. But @joshlf was also raising the issue lest it expose a hitherto unforeseen flaw in the accepted but unimplemented API design. In that sense it matches the earlier posts in this thread.

@joshlf
Copy link
Contributor

@joshlf joshlf commented Jan 4, 2017

@Ericson2314 Yeah, I thought that was what you meant. I think we're on the same page :)

@steveklabnik Sounds good; I'll poke around with my own implementation and submit an RFC if it ends up seeming like a good idea.

@alexreg
Copy link
Contributor

@alexreg alexreg commented Jan 4, 2017

@joshlf I don't any reason why custom allocators would go into the compiler or standard library. Once this RFC lands, you could easily publish your own crate that does an arbitrary sort of allocation (even a fully-fledged allocator like jemalloc could be custom-implemented!).

@joshlf
Copy link
Contributor

@joshlf joshlf commented Jan 4, 2017

@alexreg This isn't about a particular custom allocator, but rather a trait that specifies the type of all allocators which are parametric on a particular type. So just like RFC 1398 defines a trait (Allocator) that is the type of any low-level allocator, I'm asking about a trait (ObjectAllocator<T>) that is the type of any allocator which can allocate/deallocate and construct/drop objects of type T.

@Ericson2314
Copy link
Contributor

@Ericson2314 Ericson2314 commented Jan 4, 2017

@alexreg See my early point about using standard library collections with custom object-specific allocators.

@alexreg
Copy link
Contributor

@alexreg alexreg commented Jan 4, 2017

@alexreg
Copy link
Contributor

@alexreg alexreg commented Jan 4, 2017

@joshlf
Copy link
Contributor

@joshlf joshlf commented Jan 4, 2017

Sure, but I鈥檓 not sure that would belong in the standard library. Could easily go into another crate, with no loss of functionality or usability.

Yes but you probably want some standard library functionality to rely on it (such as what @Ericson2314 suggested).

I think you鈥檇 want to use standard-library collections (any heap-allocated value) with an arbitrary custom allocator; i.e. not limited to object-specific ones.

Ideally you'd want both - to accept either type of allocator. There are very significant benefits to using object-specific caching; for example, both slab allocation and magazine caching give very significant performance benefits - take a look at the papers I linked to above if you're curious.

@alexreg
Copy link
Contributor

@alexreg alexreg commented Jan 4, 2017

@joshlf
Copy link
Contributor

@joshlf joshlf commented Jan 4, 2017

But the object allocator trait could simply be a subtrait of the general allocator trait. It鈥檚 as simple as that, as far as I鈥檓 concerned. Sure, certain types of allocators can be more efficient than general-purpose allocators, but neither the compiler nor the standard really need to (or indeed should) know about this.

Ah, so the problem is that the semantics are different. Allocator allocates and frees raw byte blobs. ObjectAllocator<T>, on the other hand, would allocate already-constructed objects and would also be responsible for dropping these objects (including being able to cache constructed objects which could be handed out later in leu of constructing a newly-allocated object, which is expensive). The trait would look something like this:

trait ObjectAllocator<T> {
    fn alloc() -> T;
    fn free(t T);
}

This is not compatible with Allocator, whose methods deal with raw pointers and have no notion of type. Additionally, with Allocators, it is the caller's responsibility to drop the object being freed first. This is really important - knowing about the type T allows ObjectAllocator<T> to do things like call T's drop method, and since free(t) moves t into free, the caller cannot drop t first - it is instead the ObjectAllocator<T>'s responsibility. Fundamentally, these two traits are incompatible with one another.

@burdges
Copy link

@burdges burdges commented Mar 11, 2019

Would specialization be used by alloc users to handle ZST? Or just if size_of::<T>() == 0 checks?

@joshlf
Copy link
Contributor

@joshlf joshlf commented Mar 11, 2019

Would specialization be used by alloc users to handle ZST? Or just if size_of::<T>() == 0 checks?

The latter should be sufficient; the appropriate code paths would be trivially removed at compile time.

@gnzlbg
Copy link
Contributor

@gnzlbg gnzlbg commented Mar 11, 2019

Doesn't that imply that we should have the API explicitly not handle ZSTs rather than be implementation-defined?

For me, an important constraint is that if we ban zero-sized allocations, the Alloc methods should be able to assume that the Layout passed to them is not zero-sized.

There are multiple ways to achieve this. One would be to add another Safety clause to all Alloc methods stating that if the Layout is zero-sized the behavior is undefined.

Alternatively, we could ban zero-sized Layouts, then Alloc doesn't need to say anything about zero-sized allocations since these cannot safely happen, but doing that would have some downsides.

For example, , some types like HashMap build up the Layout from multiple Layouts, and while the final Layout might not be zero-sized, the intermediate ones might be (e.g. in HashSet). So these types would need to use "something else" (e.g. a LayoutBuilder type) to build up their final Layouts, and pay for a "non-zero-sized" check (or use an _unchecked) method when converting to Layout.

Would specialization be used by alloc users to handle ZST? Or just if size_of::() == 0 checks?

We can't specialize on ZSTs yet. Right now all code uses size_of::<T>() == 0 .

@joshlf
Copy link
Contributor

@joshlf joshlf commented Mar 11, 2019

There are multiple ways to achieve this. One would be to add another Safety clause to all Alloc methods stating that if the Layout is zero-sized the behavior is undefined.

It'd be interesting to consider whether there are ways to make this a compile-time guarantee, but even a debug_assert that the layout is non-zero-sized should be sufficient to catch 99% of bugs.

@brson
Copy link
Contributor

@brson brson commented Apr 4, 2019

I haven't paid any attention to discussions about allocators, so sorry about that. But I've long wished that the allocator had access to the type of the value its allocating. There may be allocator designs that could use it.

@TimDiekmann
Copy link
Contributor

@TimDiekmann TimDiekmann commented Apr 4, 2019

Then we'd probably have the same issues as C++ and it's allocator api.

@gnzlbg
Copy link
Contributor

@gnzlbg gnzlbg commented Apr 5, 2019

But I've long wished that the allocator had access to the type of the value its allocating. T

What do you need this for?

@raphaelcohn
Copy link

@raphaelcohn raphaelcohn commented Apr 12, 2019

@gnzblg @brson Today I had a potential use case for a knowing something about the type of value being allocated.

I'm working on a global allocator which can be switched between three underlying allocators - a thread local one, a global one with locks, and one for coroutines to use - the idea being I can constrain a coroutine representing a network connection to a maximum amount of dynamic memory usage (in the absence of being able to control allocators in collections, esp in third party code)*.

It'd be possibly be handy to know if I'm allocating a value that might move across threads (eg Arc) vs one that won't. Or it might not. But it is a possible scenario. At the moment the global allocator has a switch which the user uses to tell it from which allocator to make allocations (not needed for realloc or free; we can just look at the memory address for that).

*[It also allows me to use NUMA local memory wherever possible without any locking, and, with a 1 core 1 thread model, to cap total memory usage].

@gnzlbg
Copy link
Contributor

@gnzlbg gnzlbg commented Apr 12, 2019

@raphaelcohn

I'm working on a global allocator

I don't think any of this would (or could) apply to the GlobalAlloc trait, and the Alloc trait already has generic methods that can make use of type information (e.g. alloc_array<T>(1) allocates a single T, where T is the actual type, so the allocator can take the type into account while performing the allocation). I think it would be more useful for the purposes of this discussion to actually see code implementing allocators that make use of type information. I haven't heard any good argument about why these methods need to be part of some generic allocator trait, as opposed to just being part of the allocator API, or some other allocator trait.

@gnzlbg
Copy link
Contributor

@gnzlbg gnzlbg commented Apr 12, 2019

I think it would also be very interesting to know which of the types parametrized by Alloc do you intend to combine with allocators that use type information, and what do you expect to be the outcome.

AFAICT, the only interesting type for that would be Box because it allocates a T directly. Pretty much all other types in std never allocate a T, but some private internal type that your allocator can't know anything about. For example, Rc and Arc could allocate (InternalRefCounts, T), List / BTreeSet / etc. allocate internal node types, Vec/Deque/... allocate arrays of Ts, but not Ts themselves, etc.

For Box and Vec we could add in backwards compatible ways a BoxAlloc and an ArrayAlloc trait with blanket impls for Alloc that allocators could specialize to hijack how those behave, if there is ever a need to attack these problems in a generic way. But is there a reason why providing your own MyAllocBox and MyAllocVec types that conspire with your allocator to exploit type information isn't a viable solution ?

@TimDiekmann
Copy link
Contributor

@TimDiekmann TimDiekmann commented May 4, 2019

As we now have a dedicated repository for the allocators WG, and the list in the OP is out of date, this issue may be closed to keep discussions and tracking of this feature in one place?

@alexcrichton
Copy link
Member

@alexcrichton alexcrichton commented May 6, 2019

A good point @TimDiekmann! I'm going to go ahead and close this in favor of discussion threads in that repository.

@SimonSapin
Copy link
Contributor

@SimonSapin SimonSapin commented May 6, 2019

This is still the tracking issue that some #[unstable] attribute point to. I think it should not be closed until these features have been either stabilized or deprecated. (Or we could change the attributes to point to a different issue.)

@jethrogb
Copy link
Contributor

@jethrogb jethrogb commented May 6, 2019

Yeah unstable features referenced in git master should definitely have an open tracking issue.

@Gankra
Copy link
Contributor

@Gankra Gankra commented May 6, 2019

Agreed. Also added a notice and link to the OP.

@Gankra Gankra reopened this May 6, 2019
@mbrubeck mbrubeck mentioned this issue Nov 19, 2019
0 of 6 tasks complete
makubacki added a commit to makubacki/edk2 that referenced this issue Apr 21, 2020
We're building with nightly and the latest uses 'AllocRef' instead
of 'Alloc'.

This commit updates our code to prevent a build failure on nightly.

For reference:

allocater_api discussion:
rust-lang/rust#32838

Impacting code change:
rust-lang/rust@7ca25db

AllocRef documentation:
https://doc.rust-lang.org/nightly/core/alloc/trait.AllocRef.html
uefibot pushed a commit to microsoft/mu_basecore that referenced this issue Apr 22, 2020
Makes updates around core::alloc:Alloc needed because the code is using nightly.

Discussion around the allocater_api can be found [here](rust-lang/rust#32838).

Three main changes are made to get TestRustLangApp and TestRustLangApp2 compiling.
1. Use Alloc instead of AllocRef
2. Pass a new memory initialization argument to alloc()
3. Update code for a different return type from alloc()
makubacki added a commit to makubacki/edk2 that referenced this issue Apr 23, 2020
1. Updates Alloc trait to AllocRef

  allocater_api discussion:
  rust-lang/rust#32838

  Impacting code change:
   rust-lang/rust@7ca25db

  AllocRef documentation:
  https://doc.rust-lang.org/nightly/core/alloc/trait.AllocRef.html

2. Adds AllocInit arg to alloc() calls

  Per https://doc.rust-lang.org/nightly/core/alloc/trait.AllocRef.html, a
  new core::alloc::AllocInit enum was added to the core::alloc::AllocRef
  trait. The enum allows the caller to specify whether the memory should
  be zeroed or left uninitialized.

  Code change in rust-lang that implemented the new parameter:
  rust-lang/rust@56cbf2f

  This change updates callers of alloc() to pass the second parameter,
  conforming to the latest interface and preventing a build failure.

3. Updates usage of AllocRef methods to account for recent changes

  The return value for core::alloc::AllocRef.alloc() is now a
  Result<MemoryBlock, AllocErr> whereas it was previously
  Result<(NonNull<u8>, usize), AllocErr>.

  Calling the as_ptr() method on the Ok variant is no longer
  valid. This change uses the underlying ptr field in the MemoryBlock
  to similarly get the core::ptr::NonNull<u8>.

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
makubacki added a commit to makubacki/edk2 that referenced this issue Apr 27, 2020
1. Updates Alloc trait to AllocRef

  allocater_api discussion:
  rust-lang/rust#32838

  Impacting code change:
   rust-lang/rust@7ca25db

  AllocRef documentation:
  https://doc.rust-lang.org/nightly/core/alloc/trait.AllocRef.html

2. Adds AllocInit arg to alloc() calls

  Per https://doc.rust-lang.org/nightly/core/alloc/trait.AllocRef.html, a
  new core::alloc::AllocInit enum was added to the core::alloc::AllocRef
  trait. The enum allows the caller to specify whether the memory should
  be zeroed or left uninitialized.

  Code change in rust-lang that implemented the new parameter:
  rust-lang/rust@56cbf2f

  This change updates callers of alloc() to pass the second parameter,
  conforming to the latest interface and preventing a build failure.

3. Updates usage of AllocRef methods to account for recent changes

  The return value for core::alloc::AllocRef.alloc() is now a
  Result<MemoryBlock, AllocErr> whereas it was previously
  Result<(NonNull<u8>, usize), AllocErr>.

  Calling the as_ptr() method on the Ok variant is no longer
  valid. This change uses the underlying ptr field in the MemoryBlock
  to similarly get the core::ptr::NonNull<u8>.

Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
makubacki added a commit to makubacki/edk2 that referenced this issue Apr 29, 2020
We're building with nightly and the latest uses 'AllocRef' instead
of 'Alloc'.

This commit updates our code to prevent a build failure on nightly.

For reference:

allocater_api discussion:
rust-lang/rust#32838

Impacting code change:
rust-lang/rust@7ca25db

AllocRef documentation:
https://doc.rust-lang.org/nightly/core/alloc/trait.AllocRef.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can鈥檛 perform that action at this time.