I wanted to post a little information about some changes that I'm
working on finishing up for the windows version of tcmalloc. If
you've ever hadtrouble overriding malloc/free on windows, you might
find this useful.
With Chrome, we wanted to override the default C runtime allocators with TCMalloc. Chrome links the C runtime statically (/MT) in VS2005. Unfortunately, VS2005 does not have a static mechanism to override all allocators. This sounds easy, but it is not - VS2005 and VS2008 both use C runtimes with internal functions that cannot be overridden. We also didn't like the runtime patching approach which tcmalloc currently uses. So, to get static linkage to work, we take the C runtime library from Microsoft and remove all heap allocators from it using the LIB.EXE tool. We then implement stub functions for the non-overridable functions in the C runtime and manually link Chrome to use the new library.
If you want to do this too, here are the steps:
Steps
1) Create a slimmed down version of the C Runtime Library. The C Runtime Library ships with VS2005 in $VCInstallDir\lib\libcmt.lib. We use the script below to do this.
2) In TCMalloc's config.h, define WIN32_OVERRIDE_ALLOCATORS
3) Modify your DLL or EXE build with the following:
a) link in tcmalloc.lib by adding a Project Dependency to it.
b) in Properties -> Linker -> Input, set "Ignore Specific Library" to "libcmt.lib"
c) in Properties -> Linker -> Input, add "mylibcmt.lib" to the "Additional Dependencies" line.
With Chrome, we wanted to override the default C runtime allocators with TCMalloc. Chrome links the C runtime statically (/MT) in VS2005. Unfortunately, VS2005 does not have a static mechanism to override all allocators. This sounds easy, but it is not - VS2005 and VS2008 both use C runtimes with internal functions that cannot be overridden. We also didn't like the runtime patching approach which tcmalloc currently uses. So, to get static linkage to work, we take the C runtime library from Microsoft and remove all heap allocators from it using the LIB.EXE tool. We then implement stub functions for the non-overridable functions in the C runtime and manually link Chrome to use the new library.
If you want to do this too, here are the steps:
Steps
1) Create a slimmed down version of the C Runtime Library. The C Runtime Library ships with VS2005 in $VCInstallDir\lib\libcmt.lib. We use the script below to do this.
2) In TCMalloc's config.h, define WIN32_OVERRIDE_ALLOCATORS
3) Modify your DLL or EXE build with the following:
a) link in tcmalloc.lib by adding a Project Dependency to it.
b) in Properties -> Linker -> Input, set "Ignore Specific Library" to "libcmt.lib"
c) in Properties -> Linker -> Input, add "mylibcmt.lib" to the "Additional Dependencies" line.
SLIM_CRT.BAT
REM
REM This script takes libcmt.lib for VS2005 and removes the allocation
related
REM functions from it.
REM
REM Usage: prep_libcmt.bat <VCInstallDir> <OutputFile>
REM
REM VCInstallDir is the path where VC is installed, typically:
REM C:\Program Files\Microsoft Visual Studio 8\VC\
REM
REM OutputFile is the directory where the modified libcmt file should
be stored.
REM
SET LIBCMT=%1lib\libcmt.lib
SET LIBCMTPDB=%1lib\libcmt.pdb
SET OUTDIR=%2
SET OUTCMT=%2\libcmt.lib
MKDIR %OUTDIR%
COPY %LIBCMT% %OUTDIR%
COPY %LIBCMTPDB% %OUTDIR%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\malloc.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\free.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\realloc.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\calloc.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\new.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\delete.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\new2.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\delete2.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\align.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\msize.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\heapinit.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\expand.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\heapchk.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\heapwalk.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\heapmin.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\sbheap.obj %OUTCMT%
LIB /IGNORE:4006,4221 /REMOVE:build\intel\mt_obj\smalheap.obj %OUTCMT%