Skip to content

Split realloc into separate grow and shrink methods #41

@Amanieu

Description

@Amanieu
Member

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

Activity

TimDiekmann

TimDiekmann commented on Mar 8, 2020

@TimDiekmann
Member

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

TimDiekmann

TimDiekmann commented on Mar 9, 2020

@TimDiekmann
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

Amanieu commented on Mar 9, 2020

@Amanieu
MemberAuthor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @Amanieu@TimDiekmann

      Issue actions

        Split realloc into separate grow and shrink methods · Issue #41 · rust-lang/wg-allocators