tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Replacing malloc et al in programs.



On Fri, Nov 14, 2025 at 09:48:47 +0100, Anders Magnusson wrote:

> This suggests that either jemalloc has been too deeply integrated into libc
> (so that it cannot even be changed) or that there is some bug in ld?
> How is replacement of the malloc routines supposed to be done these days?

You need to additionally provide a few stubs, see
https://mail-index.netbsd.org/tech-toolchain/2024/11/10/msg004564.html

-uwe
--- Begin Message ---
A program can provide its own allocator.  However when you try to link
an alternative malloc statically, you run into conflicts b/c the libc
malloc is still pulled in via malloc fork hooks.

lib/libc/gen/pthread_atfork.c calls __malloc_prefork, and
__malloc_postfork and __malloc_postfork_child respectively and since
their implementations are in the same file as malloc, you get a name
clash.

With dynamic linking you end up calling libc malloc hooks, which is
probably mostly harmless.

As far as I can tell, mimalloc doesn't seem to provide these.  dmalloc
doesn't either, but mentions them in contrib/aix_5l_notes.

I'm not sure if we can arrange for program to fail to link dynamically
if they provide malloc, but don't provide the hooks.  "Everyone" knows
the set of malloc functions that need to be overriden all at once to
use your own malloc, but the hooks is not in that well known list.

We should at least document this.

Thoughts?

-uwe

--- End Message ---


Home | Main Index | Thread Index | Old Index