alloc_cpp.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Copyright (C) 2009-2022 Free Software Foundation, Inc.
  2. Contributed by Richard Henderson <rth@redhat.com>.
  3. This file is part of the GNU Transactional Memory Library (libitm).
  4. Libitm is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. Libitm is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libitm_i.h"
  20. using namespace GTM;
  21. /* Mangling the names by hand requires that we know how size_t is handled.
  22. We've gotten the letter from autoconf, now substitute it into the names.
  23. Everything below uses X as a placeholder for clarity. */
  24. #define S1(x,y) x##y
  25. #define S(x,y) S1(x,y)
  26. #define _ZnwX S(_Znw,MANGLE_SIZE_T)
  27. #define _ZnaX S(_Zna,MANGLE_SIZE_T)
  28. #define _ZdlPvX S(_ZdlPv,MANGLE_SIZE_T)
  29. #define _ZnwXRKSt9nothrow_t S(S(_Znw,MANGLE_SIZE_T),RKSt9nothrow_t)
  30. #define _ZnaXRKSt9nothrow_t S(S(_Zna,MANGLE_SIZE_T),RKSt9nothrow_t)
  31. #define _ZdlPvXRKSt9nothrow_t S(S(_ZdlPv,MANGLE_SIZE_T),RKSt9nothrow_t)
  32. #define _ZGTtnwX S(_ZGTtnw,MANGLE_SIZE_T)
  33. #define _ZGTtnaX S(_ZGTtna,MANGLE_SIZE_T)
  34. #define _ZGTtdlPvX S(_ZGTtdlPv,MANGLE_SIZE_T)
  35. #define _ZGTtnwXRKSt9nothrow_t S(S(_ZGTtnw,MANGLE_SIZE_T),RKSt9nothrow_t)
  36. #define _ZGTtnaXRKSt9nothrow_t S(S(_ZGTtna,MANGLE_SIZE_T),RKSt9nothrow_t)
  37. #define _ZGTtdlPvXRKSt9nothrow_t S(S(_ZGTtdlPv,MANGLE_SIZE_T),RKSt9nothrow_t)
  38. /* Everything from libstdc++ is weak, to avoid requiring that library
  39. to be linked into plain C applications using libitm.so. */
  40. extern "C" {
  41. extern void *_ZnwX (size_t) __attribute__((weak));
  42. extern void _ZdlPv (void *) __attribute__((weak));
  43. extern void _ZdlPvX (void *, size_t) __attribute__((weak));
  44. extern void *_ZnaX (size_t) __attribute__((weak));
  45. extern void _ZdaPv (void *) __attribute__((weak));
  46. typedef const struct nothrow_t { } *c_nothrow_p;
  47. extern void *_ZnwXRKSt9nothrow_t (size_t, c_nothrow_p) __attribute__((weak));
  48. extern void _ZdlPvRKSt9nothrow_t (void *, c_nothrow_p) __attribute__((weak));
  49. extern void _ZdlPvXRKSt9nothrow_t
  50. (void *, size_t, c_nothrow_p) __attribute__((weak));
  51. extern void *_ZnaXRKSt9nothrow_t (size_t, c_nothrow_p) __attribute__((weak));
  52. extern void _ZdaPvRKSt9nothrow_t (void *, c_nothrow_p) __attribute__((weak));
  53. #if !defined (HAVE_ELF_STYLE_WEAKREF)
  54. void *_ZnwX (size_t) { return NULL; }
  55. void _ZdlPv (void *) { return; }
  56. void _ZdlPvX (void *, size_t) { return; }
  57. void *_ZnaX (size_t) { return NULL; }
  58. void _ZdaPv (void *) { return; }
  59. void *_ZnwXRKSt9nothrow_t (size_t, c_nothrow_p) { return NULL; }
  60. void _ZdlPvRKSt9nothrow_t (void *, c_nothrow_p) { return; }
  61. void _ZdlPvXRKSt9nothrow_t (void *, size_t, c_nothrow_p) { return; }
  62. void *_ZnaXRKSt9nothrow_t (size_t, c_nothrow_p) { return NULL; }
  63. void _ZdaPvRKSt9nothrow_t (void *, c_nothrow_p) { return; }
  64. #endif /* HAVE_ELF_STYLE_WEAKREF */
  65. /* Wrap the delete nothrow symbols for usage with a single argument.
  66. Perhaps should have a configure type check for this, because the
  67. std::nothrow_t reference argument is unused (empty class), and most
  68. targets don't actually need that second argument. So we _could_
  69. invoke these functions as if they were a single argument free. */
  70. static void
  71. del_opnt (void *ptr)
  72. {
  73. _ZdlPvRKSt9nothrow_t (ptr, NULL);
  74. }
  75. static void
  76. del_opvnt (void *ptr)
  77. {
  78. _ZdaPvRKSt9nothrow_t (ptr, NULL);
  79. }
  80. static void
  81. delsz_opnt (void *ptr, size_t sz)
  82. {
  83. _ZdlPvXRKSt9nothrow_t (ptr, sz, NULL);
  84. }
  85. /* Wrap: operator new (std::size_t sz) */
  86. void *
  87. _ZGTtnwX (size_t sz)
  88. {
  89. void *r = _ZnwX (sz);
  90. if (r)
  91. gtm_thr()->record_allocation (r, _ZdlPv);
  92. return r;
  93. }
  94. /* Wrap: operator new (std::size_t sz, const std::nothrow_t&) */
  95. void *
  96. _ZGTtnwXRKSt9nothrow_t (size_t sz, c_nothrow_p nt)
  97. {
  98. void *r = _ZnwXRKSt9nothrow_t (sz, nt);
  99. if (r)
  100. gtm_thr()->record_allocation (r, del_opnt);
  101. return r;
  102. }
  103. /* Wrap: operator new[] (std::size_t sz) */
  104. void *
  105. _ZGTtnaX (size_t sz)
  106. {
  107. void *r = _ZnaX (sz);
  108. if (r)
  109. gtm_thr()->record_allocation (r, _ZdaPv);
  110. return r;
  111. }
  112. /* Wrap: operator new[] (std::size_t sz, const std::nothrow_t& nothrow) */
  113. void *
  114. _ZGTtnaXRKSt9nothrow_t (size_t sz, c_nothrow_p nt)
  115. {
  116. void *r = _ZnaXRKSt9nothrow_t (sz, nt);
  117. if (r)
  118. gtm_thr()->record_allocation (r, del_opvnt);
  119. return r;
  120. }
  121. /* Wrap: operator delete(void* ptr) */
  122. void
  123. _ZGTtdlPv (void *ptr)
  124. {
  125. if (ptr)
  126. gtm_thr()->forget_allocation (ptr, _ZdlPv);
  127. }
  128. /* Wrap: operator delete (void *ptr, const std::nothrow_t&) */
  129. void
  130. _ZGTtdlPvRKSt9nothrow_t (void *ptr, c_nothrow_p nt UNUSED)
  131. {
  132. if (ptr)
  133. gtm_thr()->forget_allocation (ptr, del_opnt);
  134. }
  135. /* Wrap: operator delete[] (void *ptr) */
  136. void
  137. _ZGTtdaPv (void *ptr)
  138. {
  139. if (ptr)
  140. gtm_thr()->forget_allocation (ptr, _ZdaPv);
  141. }
  142. /* Wrap: operator delete[] (void *ptr, const std::nothrow_t&) */
  143. void
  144. _ZGTtdaPvRKSt9nothrow_t (void *ptr, c_nothrow_p nt UNUSED)
  145. {
  146. if (ptr)
  147. gtm_thr()->forget_allocation (ptr, del_opvnt);
  148. }
  149. /* Wrap: operator delete(void* ptr, std::size_t sz) */
  150. void
  151. _ZGTtdlPvX (void *ptr, size_t sz)
  152. {
  153. if (ptr)
  154. gtm_thr()->forget_allocation (ptr, sz, _ZdlPvX);
  155. }
  156. /* Wrap: operator delete (void *ptr, std::size_t sz, const std::nothrow_t&) */
  157. void
  158. _ZGTtdlPvXRKSt9nothrow_t (void *ptr, size_t sz, c_nothrow_p nt UNUSED)
  159. {
  160. if (ptr)
  161. gtm_thr()->forget_allocation (ptr, sz, delsz_opnt);
  162. }
  163. } // extern "C"