memory.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 2010-2011 PathScale, Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. *
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  15. * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /**
  27. * memory.cc - Contains stub definition of C++ new/delete operators.
  28. *
  29. * These definitions are intended to be used for testing and are weak symbols
  30. * to allow them to be replaced by definitions from a STL implementation.
  31. * These versions simply wrap malloc() and free(), they do not provide a
  32. * C++-specific allocator.
  33. */
  34. #include <stddef.h>
  35. #include <stdlib.h>
  36. #include "stdexcept.h"
  37. #include "atomic.h"
  38. namespace std
  39. {
  40. struct nothrow_t {};
  41. }
  42. /// The type of the function called when allocation fails.
  43. typedef void (*new_handler)();
  44. /**
  45. * The function to call when allocation fails. By default, there is no
  46. * handler and a bad allocation exception is thrown if an allocation fails.
  47. */
  48. static atomic<new_handler> new_handl{nullptr};
  49. namespace std
  50. {
  51. /**
  52. * Sets a function to be called when there is a failure in new.
  53. */
  54. __attribute__((weak))
  55. new_handler set_new_handler(new_handler handler) noexcept
  56. {
  57. return new_handl.exchange(handler);
  58. }
  59. __attribute__((weak))
  60. new_handler get_new_handler(void) noexcept
  61. {
  62. return new_handl.load();
  63. }
  64. }
  65. #if __cplusplus < 201103L
  66. #define BADALLOC throw(std::bad_alloc)
  67. #else
  68. #define BADALLOC
  69. #endif
  70. namespace
  71. {
  72. /**
  73. * Helper for forwarding from no-throw operators to versions that can
  74. * return nullptr. Catches any exception and converts it into a nullptr
  75. * return.
  76. */
  77. template<void*(New)(size_t)>
  78. void *noexcept_new(size_t size)
  79. {
  80. #if !defined(_CXXRT_NO_EXCEPTIONS)
  81. try
  82. {
  83. return New(size);
  84. } catch (...)
  85. {
  86. // nothrow operator new should return NULL in case of
  87. // std::bad_alloc exception in new handler
  88. return nullptr;
  89. }
  90. #else
  91. return New(size);
  92. #endif
  93. }
  94. }
  95. __attribute__((weak))
  96. void* operator new(size_t size) BADALLOC
  97. {
  98. if (0 == size)
  99. {
  100. size = 1;
  101. }
  102. void * mem = malloc(size);
  103. while (0 == mem)
  104. {
  105. new_handler h = std::get_new_handler();
  106. if (0 != h)
  107. {
  108. h();
  109. }
  110. else
  111. {
  112. #if !defined(_CXXRT_NO_EXCEPTIONS)
  113. throw std::bad_alloc();
  114. #else
  115. break;
  116. #endif
  117. }
  118. mem = malloc(size);
  119. }
  120. return mem;
  121. }
  122. __attribute__((weak))
  123. void* operator new(size_t size, const std::nothrow_t &) _LIBCXXRT_NOEXCEPT
  124. {
  125. return noexcept_new<(::operator new)>(size);
  126. }
  127. __attribute__((weak))
  128. void operator delete(void * ptr) _LIBCXXRT_NOEXCEPT
  129. {
  130. free(ptr);
  131. }
  132. __attribute__((weak))
  133. void * operator new[](size_t size) BADALLOC
  134. {
  135. return ::operator new(size);
  136. }
  137. __attribute__((weak))
  138. void * operator new[](size_t size, const std::nothrow_t &) _LIBCXXRT_NOEXCEPT
  139. {
  140. return noexcept_new<(::operator new[])>(size);
  141. }
  142. __attribute__((weak))
  143. void operator delete[](void * ptr) _LIBCXXRT_NOEXCEPT
  144. {
  145. ::operator delete(ptr);
  146. }
  147. // C++14 additional delete operators
  148. #if __cplusplus >= 201402L
  149. __attribute__((weak))
  150. void operator delete(void * ptr, size_t) _LIBCXXRT_NOEXCEPT
  151. {
  152. ::operator delete(ptr);
  153. }
  154. __attribute__((weak))
  155. void operator delete[](void * ptr, size_t) _LIBCXXRT_NOEXCEPT
  156. {
  157. ::operator delete(ptr);
  158. }
  159. #endif