nothings / stb

stb single-file public domain libraries for C/C++
https://twitter.com/nothings
Other
26.31k stars 7.69k forks source link

stb_ds: Support environments without realloc #1447

Closed digicannon closed 1 year ago

digicannon commented 1 year ago

For environments that have malloc and free but not realloc. STBDS_REALLOC can be implemented with something like this thanks to the addition of size_in_use:

void * realloc(void * old_ptr, size_t new_size, size_t size_in_use) {
    void * new_ptr = malloc(new_size);
    memcpy(new_ptr, old_ptr, size_in_use);
    return new_ptr;
}

Also made the arguments for STBDS_REALLOC/FREE match in the definition and documentation.

rygorous commented 1 year ago

This is not going to get merged.

Niche platforms and environments are something we can't test reliably and already take up a disproportionate amount of our time. If anything, we're going to support fewer affordances for platforms lacking basic C functionality going forward, not add more of them.

If your platforms' allocator has neither realloc nor a way to figure out the size of an allocation gives it base pointer, there is already an easy way to support it using the existing API: just put a tag value with the size in front in your realloc implementation. That way, the code that deals with the peculiarities of your platform lives on your side where you can test and debug it. By contrast, if we merged this, 99.9% of stb_ds users (ourselves included) would be on a platform that ignores size_in_use which means it's only a matter of time until it breaks.

nothings commented 1 year ago

Haha, no, I said by email I'd be ok with this, although it needs to be changed to not break existing code.

nothings commented 1 year ago

Although the only reason I said it would be ok to add it is because I didn't realize there was already an STBDS_REALLOC etc., so I thought size_in_use would be ok to support if we were adding the feature, but I suppose if there already is one I need to reconsider whether it's worth it.

nothings commented 1 year ago

If your platforms' allocator has neither realloc nor a way to figure out the size of an allocation gives it base pointer, there is already an easy way to support it using the existing API: just put a tag value with the size in front in your realloc implementation. That way, the code that deals with the peculiarities of your platform lives on your side where you can test and debug it. By contrast, if we merged this, 99.9% of stb_ds users (ourselves included) would be on a platform that ignores size_in_use which means it's only a matter of time until it breaks.

I guess this is a good argument, so yeah, nevermind, not going to accept this.