libc_override_redefine.h 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // Used on systems that don't have their own definition of
  34. // malloc/new/etc. (Typically this will be a windows msvcrt.dll that
  35. // has been edited to remove the definitions.) We can just define our
  36. // own as normal functions.
  37. //
  38. // This should also work on systems were all the malloc routines are
  39. // defined as weak symbols, and there's no support for aliasing.
  40. #ifndef TCMALLOC_LIBC_OVERRIDE_REDEFINE_H_
  41. #define TCMALLOC_LIBC_OVERRIDE_REDEFINE_H_
  42. void* operator new(size_t size) { return tc_new(size); }
  43. void operator delete(void* p) throw() { tc_delete(p); }
  44. void* operator new[](size_t size) { return tc_newarray(size); }
  45. void operator delete[](void* p) throw() { tc_deletearray(p); }
  46. void* operator new(size_t size, const std::nothrow_t& nt) throw() {
  47. return tc_new_nothrow(size, nt);
  48. }
  49. void* operator new[](size_t size, const std::nothrow_t& nt) throw() {
  50. return tc_newarray_nothrow(size, nt);
  51. }
  52. void operator delete(void* ptr, const std::nothrow_t& nt) throw() {
  53. return tc_delete_nothrow(ptr, nt);
  54. }
  55. void operator delete[](void* ptr, const std::nothrow_t& nt) throw() {
  56. return tc_deletearray_nothrow(ptr, nt);
  57. }
  58. #ifdef ENABLE_SIZED_DELETE
  59. void operator delete(void* p, size_t s) throw() { tc_delete_sized(p, s); }
  60. void operator delete[](void* p, size_t s) throw(){ tc_deletearray_sized(p); }
  61. #endif
  62. extern "C" {
  63. void* malloc(size_t s) { return tc_malloc(s); }
  64. void free(void* p) { tc_free(p); }
  65. void* realloc(void* p, size_t s) { return tc_realloc(p, s); }
  66. void* calloc(size_t n, size_t s) { return tc_calloc(n, s); }
  67. void cfree(void* p) { tc_cfree(p); }
  68. void* memalign(size_t a, size_t s) { return tc_memalign(a, s); }
  69. void* valloc(size_t s) { return tc_valloc(s); }
  70. void* pvalloc(size_t s) { return tc_pvalloc(s); }
  71. int posix_memalign(void** r, size_t a, size_t s) {
  72. return tc_posix_memalign(r, a, s);
  73. }
  74. void malloc_stats(void) { tc_malloc_stats(); }
  75. int mallopt(int cmd, int v) { return tc_mallopt(cmd, v); }
  76. #ifdef HAVE_STRUCT_MALLINFO
  77. struct mallinfo mallinfo(void) { return tc_mallinfo(); }
  78. #endif
  79. size_t malloc_size(void* p) { return tc_malloc_size(p); }
  80. size_t malloc_usable_size(void* p) { return tc_malloc_size(p); }
  81. } // extern "C"
  82. // No need to do anything at tcmalloc-registration time: we do it all
  83. // via overriding weak symbols (at link time).
  84. static void ReplaceSystemAlloc() { }
  85. #endif // TCMALLOC_LIBC_OVERRIDE_REDEFINE_H_