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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split realloc into separate grow and shrink methods #41

Closed
Amanieu opened this issue Mar 4, 2020 · 3 comments · Fixed by rust-lang/rust#70362
Closed

Split realloc into separate grow and shrink methods #41

Amanieu opened this issue Mar 4, 2020 · 3 comments · Fixed by rust-lang/rust#70362
Labels
A-Alloc Trait Issues regarding the Alloc trait Proposal

Comments

@Amanieu
Copy link
Member

Amanieu commented Mar 4, 2020

If you think about it, growing and shrinking an allocation are actually 2 very different operations. The only reason for merging them together is historical, because C did it.

If you look at Vec, the biggest user of realloc, you will see that growing and shrinking are two completely separate code paths. Similarly, within allocator implementations, growing and shrinking are also often handled differently.

As such, it makes sense to remove realloc and realloc_zeroed and add three new methods:

  • grow
  • grow_zeroed
  • shrink
@TimDiekmann
Copy link
Member

In rust-lang/rust#69824 you can see, how nice growing and shrinking can be separated 👍

@TimDiekmann
Copy link
Member

As AllocRef will support zero-sized layouts, we have to decide, if we want restrictions on grow and shrink if a parameter is zero. I think it's fine to leave this to the implementor of AllocRef and only require, that every pointer returned by any method of AllocRef can be passed to another method.

Additionally, we should reevaluate #5.

@Amanieu
Copy link
Member Author

Amanieu commented Mar 9, 2020

I think grow and shrink should require that the new size is strictly greater than or less than the current size. Apart from that I don't think it makes sense to add restrictions.

@TimDiekmann TimDiekmann mentioned this issue Mar 12, 2020
18 tasks
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 2, 2020
Overhaul of the `AllocRef` trait to match allocator-wg's latest consens; Take 2

GitHub won't let me reopen rust-lang#69889 so I make a new PR.

In addition to rust-lang#69889 this fixes the unsoundness of `RawVec::into_box` when using allocators supporting overallocating. Also it uses `MemoryBlock` in `AllocRef` to unify `_in_place` methods by passing `&mut MemoryBlock`. Additionally, `RawVec` now checks for `size_of::<T>()` again and ignore every ZST. The internal capacity of `RawVec` isn't used by ZSTs anymore, as `into_box` now requires a length to be specified.

r? @Amanieu

fixes rust-lang/wg-allocators#38
fixes rust-lang/wg-allocators#41
fixes rust-lang/wg-allocators#44
fixes rust-lang/wg-allocators#51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Alloc Trait Issues regarding the Alloc trait Proposal
Projects
None yet
2 participants