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 | // Used to override malloc routines on systems that are using glibc. |
| 35 | |
| 36 | #ifndef TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ |
| 37 | #define TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ |
| 38 | |
| 39 | #include <config.h> |
| 40 | #include <features.h> // for __GLIBC__ |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 41 | #include <gperftools/tcmalloc.h> |
| 42 | |
| 43 | #ifndef __GLIBC__ |
| 44 | # error libc_override_glibc.h is for glibc distributions only. |
| 45 | #endif |
| 46 | |
| 47 | // In glibc, the memory-allocation methods are weak symbols, so we can |
| 48 | // just override them with our own. If we're using gcc, we can use |
| 49 | // __attribute__((alias)) to do the overriding easily (exception: |
| 50 | // Mach-O, which doesn't support aliases). Otherwise we have to use a |
| 51 | // function call. |
| 52 | #if !defined(__GNUC__) || defined(__MACH__) |
| 53 | |
| 54 | // This also defines ReplaceSystemAlloc(). |
| 55 | # include "libc_override_redefine.h" // defines functions malloc()/etc |
| 56 | |
| 57 | #else // #if !defined(__GNUC__) || defined(__MACH__) |
| 58 | |
| 59 | // If we get here, we're a gcc system, so do all the overriding we do |
| 60 | // with gcc. This does the overriding of all the 'normal' memory |
| 61 | // allocation. This also defines ReplaceSystemAlloc(). |
| 62 | # include "libc_override_gcc_and_weak.h" |
| 63 | |
| 64 | // We also have to do some glibc-specific overriding. Some library |
| 65 | // routines on RedHat 9 allocate memory using malloc() and free it |
| 66 | // using __libc_free() (or vice-versa). Since we provide our own |
| 67 | // implementations of malloc/free, we need to make sure that the |
| 68 | // __libc_XXX variants (defined as part of glibc) also point to the |
| 69 | // same implementations. Since it only matters for redhat, we |
| 70 | // do it inside the gcc #ifdef, since redhat uses gcc. |
| 71 | // TODO(csilvers): only do this if we detect we're an old enough glibc? |
| 72 | |
| 73 | #define ALIAS(tc_fn) __attribute__ ((alias (#tc_fn))) |
| 74 | extern "C" { |
| 75 | void* __libc_malloc(size_t size) ALIAS(tc_malloc); |
| 76 | void __libc_free(void* ptr) ALIAS(tc_free); |
| 77 | void* __libc_realloc(void* ptr, size_t size) ALIAS(tc_realloc); |
| 78 | void* __libc_calloc(size_t n, size_t size) ALIAS(tc_calloc); |
| 79 | void __libc_cfree(void* ptr) ALIAS(tc_cfree); |
| 80 | void* __libc_memalign(size_t align, size_t s) ALIAS(tc_memalign); |
| 81 | void* __libc_valloc(size_t size) ALIAS(tc_valloc); |
| 82 | void* __libc_pvalloc(size_t size) ALIAS(tc_pvalloc); |
| 83 | int __posix_memalign(void** r, size_t a, size_t s) ALIAS(tc_posix_memalign); |
| 84 | } // extern "C" |
| 85 | #undef ALIAS |
| 86 | |
| 87 | #endif // #if defined(__GNUC__) && !defined(__MACH__) |
| 88 | |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 89 | // No need to write ReplaceSystemAlloc(); one of the #includes above |
| 90 | // did it for us. |
| 91 | |
| 92 | #endif // TCMALLOC_LIBC_OVERRIDE_GLIBC_INL_H_ |