study on source code of Tcmalloc

This night, I downloaded the source code of Tcmalloc after supper, and studied it for several hours. I am recording some findings here in case that I forget them tommorow.

1. File libc_override_gcc_and_weak.h does override malloc routines (malloc, free, realloc, and so on) on systems that define the memory allocation routines to be weak symbols in their libc (almost all unix-based systems are like this), on gcc, which supports the 'alias' attribute.

2. tc_malloc in tcmalloc.cc is the entry for malloc defined in Tcmalloc tool which call do_malloc finally.  

3. Here is the basic idea of do_malloc. 

http://goog-perftools.sourceforge.net/doc/tcmalloc.html

or 

doc\tcmalloc

4. When allocating several pages for large objects, it calls TCMalloc_SystemAlloc in system_alloc.cc. It abstracts system allocator, and provides two kinds of system allocator, MmapSysAllocator and SbrkSysAllocator. There is a comment in the code."In 64-bit debug mode, place the mmap allocator first since it

allocates pointers that do not fit in 32 bits and therefore gives us better testing of code's 64-bit correctness. It also leads to less false negatives in heap-checking code."

5. NOTE: TCMalloc currently does not return any memory to the system. So it cannot solve the issue that memory usage keep increasing when a job is running.

 

PS: The source code is clear and elegant.

原文地址:https://www.cnblogs.com/Torstan/p/2599942.html