memory.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 new_handler new_handl;
  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 ATOMIC_SWAP(&new_handl, handler);
  58. }
  59. __attribute__((weak))
  60. new_handler get_new_handler(void) noexcept
  61. {
  62. return ATOMIC_LOAD(&new_handl);
  63. }
  64. }
  65. #if __cplusplus < 201103L
  66. #define NOEXCEPT noexcept
  67. #define BADALLOC throw(std::bad_alloc)
  68. #else
  69. #define NOEXCEPT noexcept
  70. #define BADALLOC
  71. #endif
  72. namespace
  73. {
  74. /**
  75. * Helper for forwarding from no-throw operators to versions that can
  76. * return nullptr. Catches any exception and converts it into a nullptr
  77. * return.
  78. */
  79. template<void*(New)(size_t)>
  80. void *noexcept_new(size_t size)
  81. {
  82. #if !defined(_CXXRT_NO_EXCEPTIONS)
  83. try
  84. {
  85. return New(size);
  86. } catch (...)
  87. {
  88. // nothrow operator new should return NULL in case of
  89. // std::bad_alloc exception in new handler
  90. return nullptr;
  91. }
  92. #else
  93. return New(size);
  94. #endif
  95. }
  96. }
  97. __attribute__((weak))
  98. void* operator new(size_t size) BADALLOC
  99. {
  100. if (0 == size)
  101. {
  102. size = 1;
  103. }
  104. void * mem = malloc(size);
  105. while (0 == mem)
  106. {
  107. new_handler h = std::get_new_handler();
  108. if (0 != h)
  109. {
  110. h();
  111. }
  112. else
  113. {
  114. #if !defined(_CXXRT_NO_EXCEPTIONS)
  115. throw std::bad_alloc();
  116. #else
  117. break;
  118. #endif
  119. }
  120. mem = malloc(size);
  121. }
  122. return mem;
  123. }
  124. __attribute__((weak))
  125. void* operator new(size_t size, const std::nothrow_t &) NOEXCEPT
  126. {
  127. return noexcept_new<(::operator new)>(size);
  128. }
  129. __attribute__((weak))
  130. void operator delete(void * ptr) NOEXCEPT
  131. {
  132. free(ptr);
  133. }
  134. __attribute__((weak))
  135. void * operator new[](size_t size) BADALLOC
  136. {
  137. return ::operator new(size);
  138. }
  139. __attribute__((weak))
  140. void * operator new[](size_t size, const std::nothrow_t &) NOEXCEPT
  141. {
  142. return noexcept_new<(::operator new[])>(size);
  143. }
  144. __attribute__((weak))
  145. void operator delete[](void * ptr) NOEXCEPT
  146. {
  147. ::operator delete(ptr);
  148. }
  149. // C++14 additional delete operators
  150. #if __cplusplus >= 201402L
  151. __attribute__((weak))
  152. void operator delete(void * ptr, size_t) NOEXCEPT
  153. {
  154. ::operator delete(ptr);
  155. }
  156. __attribute__((weak))
  157. void operator delete[](void * ptr, size_t) NOEXCEPT
  158. {
  159. ::operator delete(ptr);
  160. }
  161. #endif