// For windows, there are two ways to get tcmalloc. If we’re
// patching, then src/windows/patch_function.cc will do the necessary
// overriding here. Otherwise, we doing the ‘redefine’ trick, where
// we remove malloc/new/etc from mscvcrt.dll, and just need to define
// them now.
#if defined(_WIN32) && defined(WIN32_DO_PATCHING)void PatchWindowsFunctions(); // in src/windows/patch_function.cc
static void ReplaceSystemAlloc() { PatchWindowsFunctions(); }

// Unlike for libc, we know these exist in our module, so we can get
// and patch at the same time.
for (int i = 0; i < kNumFunctions; i++) {
function_info_[i].windows_fn = (GenericFnPtr)
::GetProcAddress(hkernel32, function_info_[i].name);
// If origstub_fn is not NULL, it’s left around from a previous
// patch. We need to set it to NULL for the new Patch call.
// Since we’ve patched Unpatch() not to delete origstub_fn_ (it
// causes problems in some contexts, though obviously not this
// one), we should delete it now, before setting it to NULL.
// NOTE: casting from a function to a pointer is contra the C++
// spec. It’s not safe on IA64, but is on i386. We use
// a C-style cast here to emphasize this is not legal C++.
delete[] (char*)(function_info_[i].origstub_fn);
function_info_[i].origstub_fn = NULL; // Patch() will fill this in
CHECK_EQ(sidestep::SIDESTEP_SUCCESS,
PreamblePatcher::Patch(function_info_[i].windows_fn,
function_info_[i].perftools_fn,
&function_info_[i].origstub_fn));
}
}

As the result, once it hooks windows heap management function, it’s able to take control.