libc_override.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // 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. #ifndef TCMALLOC_LIBC_OVERRIDE_INL_H_
  51. #define TCMALLOC_LIBC_OVERRIDE_INL_H_
  52. #include <config.h>
  53. #ifdef HAVE_FEATURES_H
  54. #include <features.h> // for __GLIBC__
  55. #endif
  56. #include <gperftools/tcmalloc.h>
  57. static void ReplaceSystemAlloc(); // defined in the .h files below
  58. // For windows, there are two ways to get tcmalloc. If we're
  59. // patching, then src/windows/patch_function.cc will do the necessary
  60. // overriding here. Otherwise, we doing the 'redefine' trick, where
  61. // we remove malloc/new/etc from mscvcrt.dll, and just need to define
  62. // them now.
  63. #if defined(_WIN32) && defined(WIN32_DO_PATCHING)
  64. void PatchWindowsFunctions(); // in src/windows/patch_function.cc
  65. static void ReplaceSystemAlloc() { PatchWindowsFunctions(); }
  66. #elif defined(_WIN32) && !defined(WIN32_DO_PATCHING)
  67. #include "libc_override_redefine.h"
  68. #elif defined(__APPLE__)
  69. #include "libc_override_osx.h"
  70. #elif defined(__GLIBC__)
  71. #include "libc_override_glibc.h"
  72. // Not all gcc systems necessarily support weak symbols, but all the
  73. // ones I know of do, so for now just assume they all do.
  74. #elif defined(__GNUC__)
  75. #include "libc_override_gcc_and_weak.h"
  76. #else
  77. #error Need to add support for your libc/OS here
  78. #endif
  79. #endif // TCMALLOC_LIBC_OVERRIDE_INL_H_