libc_override.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2011, Google Inc. 2// All rights reserved. 3// 4// Redistribution and use in source and binary forms, with or without 5// modification, are permitted provided that the following conditions are 6// met: 7// 8// * Redistributions of source code must retain the above copyright 9// notice, this list of conditions and the following disclaimer. 10// * Redistributions in binary form must reproduce the above 11// copyright notice, this list of conditions and the following disclaimer 12// in the documentation and/or other materials provided with the 13// distribution. 14// * Neither the name of Google Inc. nor the names of its 15// contributors may be used to endorse or promote products derived from 16// this software without specific prior written permission. 17// 18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 30// --- 31// Author: Craig Silverstein <opensource@google.com> 32// 33// This .h file imports the code that causes tcmalloc to override libc 34// versions of malloc/free/new/delete/etc. That is, it provides the 35// logic that makes it so calls to malloc(10) go through tcmalloc, 36// rather than the default (libc) malloc. 37// 38// This file also provides a method: ReplaceSystemAlloc(), that every 39// libc_override_*.h file it #includes is required to provide. This 40// is called when first setting up tcmalloc -- that is, when a global 41// constructor in tcmalloc.cc is executed -- to do any initialization 42// work that may be required for this OS. (Note we cannot entirely 43// control when tcmalloc is initialized, and the system may do some 44// mallocs and frees before this routine is called.) It may be a 45// noop. 46// 47// Every libc has its own way of doing this, and sometimes the compiler 48// matters too, so we have a different file for each libc, and often 49// for different compilers and OS's. 50 51#ifndef TCMALLOC_LIBC_OVERRIDE_INL_H_ 52#define TCMALLOC_LIBC_OVERRIDE_INL_H_ 53 54#include <config.h> 55#ifdef HAVE_FEATURES_H 56#include <features.h> // for __GLIBC__ 57#endif 58#include <gperftools/tcmalloc.h> 59 60static void ReplaceSystemAlloc(); // defined in the .h files below 61 62// For windows, there are two ways to get tcmalloc. If we're 63// patching, then src/windows/patch_function.cc will do the necessary 64// overriding here. Otherwise, we doing the 'redefine' trick, where 65// we remove malloc/new/etc from mscvcrt.dll, and just need to define 66// them now. 67#if defined(_WIN32) && defined(WIN32_DO_PATCHING) 68void PatchWindowsFunctions(); // in src/windows/patch_function.cc 69static void ReplaceSystemAlloc() { PatchWindowsFunctions(); } 70 71#elif defined(_WIN32) && !defined(WIN32_DO_PATCHING) 72#include "libc_override_redefine.h" 73 74#elif defined(__APPLE__) 75#include "libc_override_osx.h" 76 77#elif defined(__GLIBC__) 78#include "libc_override_glibc.h" 79 80// Not all gcc systems necessarily support weak symbols, but all the 81// ones I know of do, so for now just assume they all do. 82#elif defined(__GNUC__) 83#include "libc_override_gcc_and_weak.h" 84 85#else 86#error Need to add support for your libc/OS here 87 88#endif 89 90#endif // TCMALLOC_LIBC_OVERRIDE_INL_H_ 91