[musl] Re: realloci(): A realloc() variant that works in-place
Paul Eggert
eggert@cs.ucla.edu
Fri Oct 31 16:59:43 GMT 2025
On 10/31/25 10:22, Alejandro Colomar wrote:
> Paul, for context, this is a discussion for adding a function
>
> int realloci(void *p, size_t n);
>
> that changes the size of a memory block without moving it. (And thus,
> fails rather often, for some implementations of allocators.)
Reading the threads leading into this, the motivation for this seems to
be C++ and similar memory allocators that want a cheap way to grow an
object - if the object doesn't move they can skip some reinitialization
work, otherwise they have more work to do.
With that in mind, the proposed API is not the best way to go about the
problem. What these users want is a function that acts just like
R=realloc(P,N) EXCEPT that it lets you compare R==P, and if the two
values are the same pointer you know the object did not move and you can
skip some work. This is simpler than realloci because it means that you
need only one call (not two) in the common case when realloci returns
the null pointer.
In other words, these uses want the realloc function the way it was in
7th Edition Unix, before sanitizers got in the way and insisted that
it's an error to compare realloc's first argument with its result even
if they happen to have the same value.
There's an easy way to change the C standard to support these uses: just
change the spec for realloc to support this usage. There is no need to
change the C library, or musl, or any of the commonly used production C
libraries. The only change you'd need to make is to the C standard and
to picky sanitizers.
This would be *much* better than adding a new, hard-to-explain realloci API.
More information about the Libc-alpha
mailing list