-
Notifications
You must be signed in to change notification settings - Fork 8k
Realloc sized for stb_ds #1811
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
base: master
Are you sure you want to change the base?
Realloc sized for stb_ds #1811
Conversation
|
I'm not opposed in principle, but why add STBDS_MALLOC? |
It's not entirely necessary but: Its how it is done in in stb_image and stb_image_write - parity between different files seems like a good idea. One downside is slightly increased complexity of the code - one extra macro. The macro I think is most redundant is in fact STBDS_REALLOC as if the user doesn't want to use the oldsz provided by STBDS_REALLOC_SIZED, they can just... not. Having STBDS_REALLOC could be seen as more ergonomic for the user, but IMHO the user having to know that there are two macros and knowing which one is used if both are defined is more costly than the slightly cleaner code. I just included this for parity, but if it's not important then I can remove it. |
|
Regarding the last, removing STBDS_REALLOC is a non-starter because that breaks back-compatibility. Which is bad in the first place, but even worse for a feature most people don't need (old size). But of course it's fine to use the implementation strategy of making STBDS_REALLOC_SIZED be the base case and STBDS_REALLOC just defines an appropriate STBDS_REALLOC_SIZED. As to MALLOC, stb_ds was written long after stb_image and stb_image_write, and omitting MALLOC as being redundant was an intentional choice when it was written, not an oversight. Given the traditional definition of realloc as being malloc when oldptr=NULL, having to wire up a malloc just seems like unnecessary friction. If anything, the no-malloc interface should be backported to stb_image et al. (You could also theoretically eliminate free, since Posix and ISO C both also define realloc as being free if size is 0, but there was some reason I didn't exploit this, although I don't remember now; maybe either some other platforms it doesn't work, or just the edge case of using realloc to free a NULL ptr being treated as malloc(0) not free(NULL).) |
Would you like me to remove malloc entirely or provide a default definition? |
|
Better to just remove it entirely so it's one less thing to think about. |
STBDS_REALLOC_SIZED for stb_ds (similarly to stb_image).
Good for using different kinds of allocators like arenas.