3

I'm just looking at the comments at the top of the malloc.c file in glibc and it says this:

You may already by default be using a C library containing a malloc
that is based on some version of this malloc (for example in
linux). You might still want to use the one in this file in order to
customize settings or to avoid overheads associated with library
versions.

I dont understand why glibc code would be saying a version of Linux may be using something different to the code in glibc malloc.c? Could somebody please help re-word what it means? I thought that glibc malloc() is what every linux would be using for memory management?

user997112
  • 29,025
  • 43
  • 182
  • 361
  • 2
    At the moment that comment was written, it was not yet part of glibc, right? When it was added to glibc, the comment was not stripped out (and there are good reasons for that). – Jim Balter Dec 31 '13 at 19:20

2 Answers2

4

It says so because the malloc implementation in glibc is based on ptmalloc, which again is based on the Doug Lea malloc implementation from which those comments orignate from. That malloc implementation was imported and made the default in glibc version 2.3.

As ptmalloc/Doug Lea's malloc implementation was a separate library you could use to replace the standard malloc() on your system, those comments applied. But the comments have been kept when introducing that library into glibc.

nos
  • 223,662
  • 58
  • 417
  • 506
  • So basically that part of the comment should have been removed as its out of context? – user997112 Dec 31 '13 at 19:19
  • @user997112 There are good reasons not to alter the source just because it has been included into glibc. – Jim Balter Dec 31 '13 at 19:21
  • @JimBalter Just above that comment in the glibc sources there's also a comment staing that substantial changes were made to adapt it to glibc and "Do not look for much commonality with the ptmalloc2 version." There probably arn't that many good reasons. – nos Dec 31 '13 at 19:23
  • Thanks guys. Im just looking at memory allocation- am I right in thinking I only really need to concentrate on malloc.c, arena.c and memmem.c? – user997112 Dec 31 '13 at 19:35
0

You could link a different library providing malloc (e.g. some libmalloc or libtcmalloc ...) See this.

You could link a different libc, e.g. MUSL libc providing its own malloc. So you can have a Linux application not using the malloc from GNU libc...

And the GNU libc can be used on other systems (Hurd, Solaris, AIX...) having their own system malloc ....

Some applications might provide their own malloc or some wrapper. (Think of libgc ...).

You could play LD_PRELOAD tricks to overload malloc, see this.

And your application can have been statically linked (e.g. to an older version of libc or to MUSL libc...). You might even have several libc .... (several versions of GNU libc).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547