Skip to content

Conversation

@jonathandw743
Copy link

STBDS_REALLOC_SIZED for stb_ds (similarly to stb_image).
Good for using different kinds of allocators like arenas.

@nothings
Copy link
Owner

I'm not opposed in principle, but why add STBDS_MALLOC?

@jonathandw743
Copy link
Author

jonathandw743 commented Jun 22, 2025

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.
Most allocators have separate alloc and realloc functions where realloc does "more stuff". Allowing the simpler alloc to be used where it can be is slightly more efficient.

One downside is slightly increased complexity of the code - one extra macro.
This is not much of an issue for the user as if they do happen to have an allocator with realloc but no alloc, they can just define STBDS_MALLOC as realloc(0, ...). If this is an issue, I could make defining STBDS_MALLOC optional and provide a default definition using STBDS_REALLOC_SIZED.

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.

@nothings
Copy link
Owner

nothings commented Jun 22, 2025

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).)

@jonathandw743
Copy link
Author

having to wire up a malloc just seems like unnecessary friction

Would you like me to remove malloc entirely or provide a default definition?

@nothings
Copy link
Owner

Better to just remove it entirely so it's one less thing to think about.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants