4 ms·
Well, unless you're implementing malloc using e.g. sbrk().
by jasonhansel 4y ago
Well, unless you're implementing malloc using e.g. sbrk().
- kllrnohj 4y agoAlthough you shouldn't be on a modern system, you'd instead be implementing malloc on top of mmap. So remove sbrk, and make mmap the "object allocator", and tada! You don't really need linear virtual address spaces anymore.
- gary_0 4y agoNewer allocators like mimalloc[0] don't even support sbrk (I think jemalloc still does). Mimalloc seems to have some interesting features. [0] https://github.com/microsoft/mimalloc https://github.com/microsoft/mimalloc
- astrange 4y agoYes, this is why malloc() can't actually be implemented in C. Actual implementations of it exist because of special compiler dispensation, or mostly that the callers and implementations are in separate libraries so the implementation isn't visible to the caller.
- kazinator 4y agomalloc perhaps can't be implemented in the abstract dialect called ISO C. It can be impemented in GNU C, and others.
- astrange 4y agoIt can't. GNU C adds a malloc attribute: > Attribute malloc indicates that a function is malloc-like, i.e., that the pointer P returned by the function cannot alias any other pointer valid when the function returns, and moreover no pointers to valid objects occur in any storage addressed by P. In addition, the GCC predicts that a function with the attribute returns non-null in most cases. But it doesn't provide any operations to do the things in this paragraph (create new pointers). The operation that does this is malloc itself. This will typically not cause problems, but it would if LTO got so good you could include libc in it.