15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Notes about the Chrome memory allocator. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Background 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)---------- 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)We use this library as a generic way to fork into any of several allocators. 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Currently we can, at runtime, switch between: 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) the default windows allocator 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) the windows low-fragmentation-heap 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) tcmalloc 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) jemalloc (the heap used most notably within Mozilla Firefox) 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)The mechanism for hooking LIBCMT in windows is rather tricky. The core 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)problem is that by default, the windows library does not declare malloc and 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)free as weak symbols. Because of this, they cannot be overriden. To work 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)around this, we start with the LIBCMT.LIB, and manually remove all allocator 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)related functions from it using the visual studio library tool. Once removed, 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)we can now link against the library and provide custom versions of the 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)allocator related functionality. 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Source code 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)----------- 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)This directory contains just the allocator (i.e. shim) layer that switches 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)between the different underlying memory allocation implementations. 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)The tcmalloc and jemalloc libraries originate outside of Chromium 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)and exist in ../../third_party/tcmalloc and ../../third_party/jemalloc 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)(currently, the actual locations are defined in the allocator.gyp file). 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)The third party sources use a vendor-branch SCM pattern to track 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Chromium-specific changes independently from upstream changes. 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)The general intent is to push local changes upstream so that over 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)time we no longer need any forked files. 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Adding a new allocator 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)---------------------- 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Adding a new allocator requires definition of the following five functions: 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) extern "C" { 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool init(); 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void* malloc(size_t s); 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void* realloc(void* p, size_t s); 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void free(void* s); 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t msize(void* p); 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)All other allocation related functions (new/delete/calloc/etc) have been 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)implemented generically to work across all allocators. 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Usage 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)----- 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)You can use the different allocators by setting the environment variable 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)CHROME_ALLOCATOR to: 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "tcmalloc" - TC Malloc (default) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "jemalloc" - JE Malloc 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "winheap" - Windows default heap 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "winlfh" - Windows Low-Fragmentation heap 60