alloc_cpp.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* Copyright (C) 2009-2015 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 _ZnwXRKSt9nothrow_t S(S(_Znw,MANGLE_SIZE_T),RKSt9nothrow_t)
  29. #define _ZnaXRKSt9nothrow_t S(S(_Zna,MANGLE_SIZE_T),RKSt9nothrow_t)
  30. #define _ZGTtnwX S(_ZGTtnw,MANGLE_SIZE_T)
  31. #define _ZGTtnaX S(_ZGTtna,MANGLE_SIZE_T)
  32. #define _ZGTtnwXRKSt9nothrow_t S(S(_ZGTtnw,MANGLE_SIZE_T),RKSt9nothrow_t)
  33. #define _ZGTtnaXRKSt9nothrow_t S(S(_ZGTtna,MANGLE_SIZE_T),RKSt9nothrow_t)
  34. /* Everything from libstdc++ is weak, to avoid requiring that library
  35. to be linked into plain C applications using libitm.so. */
  36. extern "C" {
  37. extern void *_ZnwX (size_t) __attribute__((weak));
  38. extern void _ZdlPv (void *) __attribute__((weak));
  39. extern void *_ZnaX (size_t) __attribute__((weak));
  40. extern void _ZdaPv (void *) __attribute__((weak));
  41. typedef const struct nothrow_t { } *c_nothrow_p;
  42. extern void *_ZnwXRKSt9nothrow_t (size_t, c_nothrow_p) __attribute__((weak));
  43. extern void _ZdlPvRKSt9nothrow_t (void *, c_nothrow_p) __attribute__((weak));
  44. extern void *_ZnaXRKSt9nothrow_t (size_t, c_nothrow_p) __attribute__((weak));
  45. extern void _ZdaPvRKSt9nothrow_t (void *, c_nothrow_p) __attribute__((weak));
  46. #if !defined (HAVE_ELF_STYLE_WEAKREF)
  47. void *_ZnwX (size_t) { return NULL; }
  48. void _ZdlPv (void *) { return; }
  49. void *_ZnaX (size_t) { return NULL; }
  50. void _ZdaPv (void *) { return; }
  51. void *_ZnwXRKSt9nothrow_t (size_t, c_nothrow_p) { return NULL; }
  52. void _ZdlPvRKSt9nothrow_t (void *, c_nothrow_p) { return; }
  53. void *_ZnaXRKSt9nothrow_t (size_t, c_nothrow_p) { return NULL; }
  54. void _ZdaPvRKSt9nothrow_t (void *, c_nothrow_p) { return; }
  55. #endif /* HAVE_ELF_STYLE_WEAKREF */
  56. /* Wrap the delete nothrow symbols for usage with a single argument.
  57. Perhaps should have a configure type check for this, because the
  58. std::nothrow_t reference argument is unused (empty class), and most
  59. targets don't actually need that second argument. So we _could_
  60. invoke these functions as if they were a single argument free. */
  61. static void
  62. del_opnt (void *ptr)
  63. {
  64. _ZdlPvRKSt9nothrow_t (ptr, NULL);
  65. }
  66. static void
  67. del_opvnt (void *ptr)
  68. {
  69. _ZdaPvRKSt9nothrow_t (ptr, NULL);
  70. }
  71. /* Wrap: operator new (std::size_t sz) */
  72. void *
  73. _ZGTtnwX (size_t sz)
  74. {
  75. void *r = _ZnwX (sz);
  76. if (r)
  77. gtm_thr()->record_allocation (r, _ZdlPv);
  78. return r;
  79. }
  80. /* Wrap: operator new (std::size_t sz, const std::nothrow_t&) */
  81. void *
  82. _ZGTtnwXRKSt9nothrow_t (size_t sz, c_nothrow_p nt)
  83. {
  84. void *r = _ZnwXRKSt9nothrow_t (sz, nt);
  85. if (r)
  86. gtm_thr()->record_allocation (r, del_opnt);
  87. return r;
  88. }
  89. /* Wrap: operator new[] (std::size_t sz) */
  90. void *
  91. _ZGTtnaX (size_t sz)
  92. {
  93. void *r = _ZnaX (sz);
  94. if (r)
  95. gtm_thr()->record_allocation (r, _ZdaPv);
  96. return r;
  97. }
  98. /* Wrap: operator new[] (std::size_t sz, const std::nothrow_t& nothrow) */
  99. void *
  100. _ZGTtnaXRKSt9nothrow_t (size_t sz, c_nothrow_p nt)
  101. {
  102. void *r = _ZnaXRKSt9nothrow_t (sz, nt);
  103. if (r)
  104. gtm_thr()->record_allocation (r, del_opvnt);
  105. return r;
  106. }
  107. /* Wrap: operator delete(void* ptr) */
  108. void
  109. _ZGTtdlPv (void *ptr)
  110. {
  111. if (ptr)
  112. gtm_thr()->forget_allocation (ptr, _ZdlPv);
  113. }
  114. /* Wrap: operator delete (void *ptr, const std::nothrow_t&) */
  115. void
  116. _ZGTtdlPvRKSt9nothrow_t (void *ptr, c_nothrow_p nt UNUSED)
  117. {
  118. if (ptr)
  119. gtm_thr()->forget_allocation (ptr, del_opnt);
  120. }
  121. /* Wrap: operator delete[] (void *ptr) */
  122. void
  123. _ZGTtdaPv (void *ptr)
  124. {
  125. if (ptr)
  126. gtm_thr()->forget_allocation (ptr, _ZdaPv);
  127. }
  128. /* Wrap: operator delete[] (void *ptr, const std::nothrow_t&) */
  129. void
  130. _ZGTtdaPvRKSt9nothrow_t (void *ptr, c_nothrow_p nt UNUSED)
  131. {
  132. if (ptr)
  133. gtm_thr()->forget_allocation (ptr, del_opvnt);
  134. }
  135. } // extern "C"