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

Split realloc into separate grow and shrink methods #41

Closed Amanieu closed 4 years ago

Amanieu commented 4 years ago

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:

TimDiekmann commented 4 years ago

In https://github.com/rust-lang/rust/pull/69824 you can see, how nice growing and shrinking can be separated :+1:

TimDiekmann commented 4 years ago

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 commented 4 years ago

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.