libc_override_gcc_and_weak.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 to override malloc routines on systems that define the
  34. // memory allocation routines to be weak symbols in their libc
  35. // (almost all unix-based systems are like this), on gcc, which
  36. // suppports the 'alias' attribute.
  37. #ifndef TCMALLOC_LIBC_OVERRIDE_GCC_AND_WEAK_INL_H_
  38. #define TCMALLOC_LIBC_OVERRIDE_GCC_AND_WEAK_INL_H_
  39. #ifdef HAVE_SYS_CDEFS_H
  40. #include <sys/cdefs.h> // for __THROW
  41. #endif
  42. #include <gperftools/tcmalloc.h>
  43. #include "getenv_safe.h" // TCMallocGetenvSafe
  44. #include "base/commandlineflags.h"
  45. #ifndef __THROW // I guess we're not on a glibc-like system
  46. # define __THROW // __THROW is just an optimization, so ok to make it ""
  47. #endif
  48. #ifndef __GNUC__
  49. # error libc_override_gcc_and_weak.h is for gcc distributions only.
  50. #endif
  51. #define ALIAS(tc_fn) __attribute__ ((alias (#tc_fn), used))
  52. #ifndef TCMALLOC_SGX
  53. void* operator new(size_t size) throw (std::bad_alloc)
  54. ALIAS(tc_new);
  55. void operator delete(void* p) throw()
  56. ALIAS(tc_delete);
  57. void* operator new[](size_t size) throw (std::bad_alloc)
  58. ALIAS(tc_newarray);
  59. void operator delete[](void* p) throw()
  60. ALIAS(tc_deletearray);
  61. void* operator new(size_t size, const std::nothrow_t& nt) throw()
  62. ALIAS(tc_new_nothrow);
  63. void* operator new[](size_t size, const std::nothrow_t& nt) throw()
  64. ALIAS(tc_newarray_nothrow);
  65. void operator delete(void* p, const std::nothrow_t& nt) throw()
  66. ALIAS(tc_delete_nothrow);
  67. void operator delete[](void* p, const std::nothrow_t& nt) throw()
  68. ALIAS(tc_deletearray_nothrow);
  69. #if defined(ENABLE_SIZED_DELETE)
  70. void operator delete(void *p, size_t size) throw()
  71. ALIAS(tc_delete_sized);
  72. void operator delete[](void *p, size_t size) throw()
  73. ALIAS(tc_deletearray_sized);
  74. #elif defined(ENABLE_DYNAMIC_SIZED_DELETE) && \
  75. (__GNUC__ * 100 + __GNUC_MINOR__) >= 405
  76. static void delegate_sized_delete(void *p, size_t s) throw() {
  77. (operator delete)(p);
  78. }
  79. static void delegate_sized_deletearray(void *p, size_t s) throw() {
  80. (operator delete[])(p);
  81. }
  82. extern "C" __attribute__((weak))
  83. int tcmalloc_sized_delete_enabled(void);
  84. static bool sized_delete_enabled(void) {
  85. if (tcmalloc_sized_delete_enabled != 0) {
  86. return !!tcmalloc_sized_delete_enabled();
  87. }
  88. const char *flag = TCMallocGetenvSafe("TCMALLOC_ENABLE_SIZED_DELETE");
  89. return tcmalloc::commandlineflags::StringToBool(flag, false);
  90. }
  91. extern "C" {
  92. static void *resolve_delete_sized(void) {
  93. if (sized_delete_enabled()) {
  94. return reinterpret_cast<void *>(tc_delete_sized);
  95. }
  96. return reinterpret_cast<void *>(delegate_sized_delete);
  97. }
  98. static void *resolve_deletearray_sized(void) {
  99. if (sized_delete_enabled()) {
  100. return reinterpret_cast<void *>(tc_deletearray_sized);
  101. }
  102. return reinterpret_cast<void *>(delegate_sized_deletearray);
  103. }
  104. }
  105. void operator delete(void *p, size_t size) throw()
  106. __attribute__((ifunc("resolve_delete_sized")));
  107. void operator delete[](void *p, size_t size) throw()
  108. __attribute__((ifunc("resolve_deletearray_sized")));
  109. #else /* !ENABLE_SIZED_DELETE && !ENABLE_DYN_SIZED_DELETE */
  110. void operator delete(void *p, size_t size) throw()
  111. ALIAS(tc_delete);
  112. void operator delete[](void *p, size_t size) throw()
  113. ALIAS(tc_deletearray);
  114. #endif /* !ENABLE_SIZED_DELETE && !ENABLE_DYN_SIZED_DELETE */
  115. #endif
  116. extern "C" {
  117. void* __attribute__((weak)) malloc(size_t size) __THROW ALIAS(tc_malloc);
  118. void __attribute__((weak)) free(void* ptr) __THROW ALIAS(tc_free);
  119. void* __attribute__((weak)) realloc(void* ptr, size_t size) __THROW ALIAS(tc_realloc);
  120. void* __attribute__((weak)) calloc(size_t n, size_t size) __THROW ALIAS(tc_calloc);
  121. #ifndef TCMALLOC_SGX
  122. void cfree(void* ptr) __THROW ALIAS(tc_cfree);
  123. #endif
  124. void* __attribute__((weak)) memalign(size_t align, size_t s) __THROW ALIAS(tc_memalign);
  125. #ifndef TCMALLOC_SGX
  126. void* valloc(size_t size) __THROW ALIAS(tc_valloc);
  127. void* pvalloc(size_t size) __THROW ALIAS(tc_pvalloc);
  128. int posix_memalign(void** r, size_t a, size_t s) __THROW
  129. ALIAS(tc_posix_memalign);
  130. #ifndef __UCLIBC__
  131. void malloc_stats(void) __THROW ALIAS(tc_malloc_stats);
  132. #endif
  133. int mallopt(int cmd, int value) __THROW ALIAS(tc_mallopt);
  134. #ifdef HAVE_STRUCT_MALLINFO
  135. struct mallinfo mallinfo(void) __THROW ALIAS(tc_mallinfo);
  136. #endif
  137. size_t malloc_size(void* p) __THROW ALIAS(tc_malloc_size);
  138. #if defined(__ANDROID__)
  139. size_t malloc_usable_size(const void* p) __THROW
  140. ALIAS(tc_malloc_size);
  141. #else
  142. size_t malloc_usable_size(void* p) __THROW ALIAS(tc_malloc_size);
  143. #endif
  144. #endif
  145. } // extern "C"
  146. #undef ALIAS
  147. // No need to do anything at tcmalloc-registration time: we do it all
  148. // via overriding weak symbols (at link time).
  149. static void ReplaceSystemAlloc() { }
  150. #endif // TCMALLOC_LIBC_OVERRIDE_GCC_AND_WEAK_INL_H_