Is it ok to use tcmalloc in that shared library?
It depends on a few things:
- Whether your shared library is linked with tcmalloc in such a way that it exposes
malloc
and operator new
as external symbols. Normally, it does.
- Whether the user of your library links to your library or loads it at run-time with
dlopen
and what dlopen
options are used.
What happens if the user's executable itself is not linked with tcmalloc?
One of the two things can happen:
malloc
is already a resolved symbol in user's application/process. In this case your .so
uses that malloc
. That happens when the user loads your .so
with dlopen
.
malloc
is not resolved yet, so that user's application/process uses malloc
from tcmalloc from your .so
. That happens when the user links against your .so
in linker command line and your .so
comes before -lc
.
It may be most robust for your .so
to not link tcmalloc at all. The user of the application can then decide which malloc
implementation to use either by linking against tcmalloc or other allocator, or trying different allocators by pre-loading them at run-time using LD_PRELOAD
.
You may like to learn how Unix linkers work to answer such questions yourself in the future.