new.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //===--------------------------- new.cpp ----------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #define _LIBCPP_BUILDING_NEW
  10. #include <stdlib.h>
  11. #include <__config>
  12. #if !defined(_LIBCPP_SGX_CONFIG)
  13. #include "new"
  14. #if defined(__APPLE__) && !defined(LIBCXXRT)
  15. #include <cxxabi.h>
  16. #ifndef _LIBCPPABI_VERSION
  17. // On Darwin, there are two STL shared libraries and a lower level ABI
  18. // shared library. The global holding the current new handler is
  19. // in the ABI library and named __cxa_new_handler.
  20. #define __new_handler __cxxabiapple::__cxa_new_handler
  21. #endif
  22. #else // __APPLE__
  23. #if defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
  24. #include <cxxabi.h>
  25. #endif // defined(LIBCXX_BUILDING_LIBCXXABI)
  26. #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
  27. static std::new_handler __new_handler;
  28. #endif // _LIBCPPABI_VERSION
  29. #endif
  30. #ifndef __GLIBCXX__
  31. // Implement all new and delete operators as weak definitions
  32. // in this shared library, so that they can be overridden by programs
  33. // that define non-weak copies of the functions.
  34. _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
  35. void *
  36. operator new(std::size_t size)
  37. #if !__has_feature(cxx_noexcept)
  38. throw(std::bad_alloc)
  39. #endif
  40. {
  41. if (size == 0)
  42. size = 1;
  43. void* p;
  44. while ((p = ::malloc(size)) == 0)
  45. {
  46. // If malloc fails and there is a new_handler,
  47. // call it to try free up memory.
  48. std::new_handler nh = std::get_new_handler();
  49. if (nh)
  50. nh();
  51. else
  52. #ifndef _LIBCPP_NO_EXCEPTIONS
  53. throw std::bad_alloc();
  54. #else
  55. break;
  56. #endif
  57. }
  58. return p;
  59. }
  60. _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
  61. void*
  62. operator new(size_t size, const std::nothrow_t&) _NOEXCEPT
  63. {
  64. void* p = 0;
  65. #ifndef _LIBCPP_NO_EXCEPTIONS
  66. try
  67. {
  68. #endif // _LIBCPP_NO_EXCEPTIONS
  69. p = ::operator new(size);
  70. #ifndef _LIBCPP_NO_EXCEPTIONS
  71. }
  72. catch (...)
  73. {
  74. }
  75. #endif // _LIBCPP_NO_EXCEPTIONS
  76. return p;
  77. }
  78. _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
  79. void*
  80. operator new[](size_t size)
  81. #if !__has_feature(cxx_noexcept)
  82. throw(std::bad_alloc)
  83. #endif
  84. {
  85. return ::operator new(size);
  86. }
  87. _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
  88. void*
  89. operator new[](size_t size, const std::nothrow_t&) _NOEXCEPT
  90. {
  91. void* p = 0;
  92. #ifndef _LIBCPP_NO_EXCEPTIONS
  93. try
  94. {
  95. #endif // _LIBCPP_NO_EXCEPTIONS
  96. p = ::operator new[](size);
  97. #ifndef _LIBCPP_NO_EXCEPTIONS
  98. }
  99. catch (...)
  100. {
  101. }
  102. #endif // _LIBCPP_NO_EXCEPTIONS
  103. return p;
  104. }
  105. _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
  106. void
  107. operator delete(void* ptr) _NOEXCEPT
  108. {
  109. if (ptr)
  110. ::free(ptr);
  111. }
  112. _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
  113. void
  114. operator delete(void* ptr, const std::nothrow_t&) _NOEXCEPT
  115. {
  116. ::operator delete(ptr);
  117. }
  118. _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
  119. void
  120. operator delete(void* ptr, size_t) _NOEXCEPT
  121. {
  122. ::operator delete(ptr);
  123. }
  124. _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
  125. void
  126. operator delete[] (void* ptr) _NOEXCEPT
  127. {
  128. ::operator delete(ptr);
  129. }
  130. _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
  131. void
  132. operator delete[] (void* ptr, const std::nothrow_t&) _NOEXCEPT
  133. {
  134. ::operator delete[](ptr);
  135. }
  136. _LIBCPP_WEAK _LIBCPP_NEW_DELETE_VIS
  137. void
  138. operator delete[] (void* ptr, size_t) _NOEXCEPT
  139. {
  140. ::operator delete[](ptr);
  141. }
  142. #endif // !__GLIBCXX__
  143. namespace std
  144. {
  145. #ifndef __GLIBCXX__
  146. const nothrow_t nothrow = {};
  147. #endif
  148. #ifndef _LIBCPPABI_VERSION
  149. #ifndef __GLIBCXX__
  150. new_handler
  151. set_new_handler(new_handler handler) _NOEXCEPT
  152. {
  153. return __sync_lock_test_and_set(&__new_handler, handler);
  154. }
  155. new_handler
  156. get_new_handler() _NOEXCEPT
  157. {
  158. return __sync_fetch_and_add(&__new_handler, nullptr);
  159. }
  160. #endif // !__GLIBCXX__
  161. #ifndef LIBCXXRT
  162. bad_alloc::bad_alloc() _NOEXCEPT
  163. {
  164. }
  165. #ifndef __GLIBCXX__
  166. bad_alloc::~bad_alloc() _NOEXCEPT
  167. {
  168. }
  169. const char*
  170. bad_alloc::what() const _NOEXCEPT
  171. {
  172. return "std::bad_alloc";
  173. }
  174. #endif // !__GLIBCXX__
  175. bad_array_new_length::bad_array_new_length() _NOEXCEPT
  176. {
  177. }
  178. bad_array_new_length::~bad_array_new_length() _NOEXCEPT
  179. {
  180. }
  181. const char*
  182. bad_array_new_length::what() const _NOEXCEPT
  183. {
  184. return "bad_array_new_length";
  185. }
  186. #endif //LIBCXXRT
  187. const char*
  188. bad_array_length::what() const _NOEXCEPT
  189. {
  190. return "bad_array_length";
  191. }
  192. bad_array_length::bad_array_length() _NOEXCEPT
  193. {
  194. }
  195. bad_array_length::~bad_array_length() _NOEXCEPT
  196. {
  197. }
  198. #endif // _LIBCPPABI_VERSION
  199. #ifndef LIBSTDCXX
  200. void
  201. __throw_bad_alloc()
  202. {
  203. #ifndef _LIBCPP_NO_EXCEPTIONS
  204. throw bad_alloc();
  205. #endif
  206. }
  207. #endif // !LIBSTDCXX
  208. } // std
  209. #endif // !defined(_LIBCPP_SGX_CONFIG)