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