new 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // -*- C++ -*-
  2. //===----------------------------- new ------------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_NEW
  11. #define _LIBCPP_NEW
  12. /*
  13. new synopsis
  14. namespace std
  15. {
  16. class bad_alloc
  17. : public exception
  18. {
  19. public:
  20. bad_alloc() noexcept;
  21. bad_alloc(const bad_alloc&) noexcept;
  22. bad_alloc& operator=(const bad_alloc&) noexcept;
  23. virtual const char* what() const noexcept;
  24. };
  25. class bad_array_length : public bad_alloc // C++14
  26. {
  27. public:
  28. bad_array_length() noexcept;
  29. };
  30. class bad_array_new_length : public bad_alloc
  31. {
  32. public:
  33. bad_array_new_length() noexcept;
  34. };
  35. struct nothrow_t {};
  36. extern const nothrow_t nothrow;
  37. typedef void (*new_handler)();
  38. new_handler set_new_handler(new_handler new_p) noexcept;
  39. new_handler get_new_handler() noexcept;
  40. } // std
  41. void* operator new(std::size_t size); // replaceable
  42. void* operator new(std::size_t size, const std::nothrow_t&) noexcept; // replaceable
  43. void operator delete(void* ptr) noexcept; // replaceable
  44. void operator delete(void* ptr, std::size_t size) noexcept; // replaceable, C++14
  45. void operator delete(void* ptr, const std::nothrow_t&) noexcept; // replaceable
  46. void* operator new[](std::size_t size); // replaceable
  47. void* operator new[](std::size_t size, const std::nothrow_t&) noexcept; // replaceable
  48. void operator delete[](void* ptr) noexcept; // replaceable
  49. void operator delete[](void* ptr, std::size_t size) noexcept; // replaceable, C++14
  50. void operator delete[](void* ptr, const std::nothrow_t&) noexcept; // replaceable
  51. void* operator new (std::size_t size, void* ptr) noexcept;
  52. void* operator new[](std::size_t size, void* ptr) noexcept;
  53. void operator delete (void* ptr, void*) noexcept;
  54. void operator delete[](void* ptr, void*) noexcept;
  55. */
  56. #include <__config>
  57. #if defined(_LIBCPP_SGX_CONFIG)
  58. #include <../stdc++/new>
  59. #else // !defined(_LIBCPP_SGX_CONFIG)
  60. #include <exception>
  61. #include <cstddef>
  62. #endif // defined(_LIBCPP_SGX_CONFIG)
  63. #include <__undef___deallocate>
  64. #if !defined(_LIBCPP_SGX_CONFIG)
  65. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  66. #pragma GCC system_header
  67. #endif
  68. namespace std // purposefully not using versioning namespace
  69. {
  70. class _LIBCPP_EXCEPTION_ABI bad_alloc
  71. : public exception
  72. {
  73. public:
  74. bad_alloc() _NOEXCEPT;
  75. virtual ~bad_alloc() _NOEXCEPT;
  76. virtual const char* what() const _NOEXCEPT;
  77. };
  78. class _LIBCPP_EXCEPTION_ABI bad_array_new_length
  79. : public bad_alloc
  80. {
  81. public:
  82. bad_array_new_length() _NOEXCEPT;
  83. virtual ~bad_array_new_length() _NOEXCEPT;
  84. virtual const char* what() const _NOEXCEPT;
  85. };
  86. #if defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
  87. class _LIBCPP_EXCEPTION_ABI bad_array_length
  88. : public bad_alloc
  89. {
  90. public:
  91. bad_array_length() _NOEXCEPT;
  92. virtual ~bad_array_length() _NOEXCEPT;
  93. virtual const char* what() const _NOEXCEPT;
  94. };
  95. #define _LIBCPP_BAD_ARRAY_LENGTH_DEFINED
  96. #endif // defined(_LIBCPP_BUILDING_NEW) || (_LIBCPP_STD_VER > 11)
  97. _LIBCPP_FUNC_VIS void __throw_bad_alloc(); // not in C++ spec
  98. struct _LIBCPP_TYPE_VIS nothrow_t {};
  99. extern _LIBCPP_FUNC_VIS const nothrow_t nothrow;
  100. typedef void (*new_handler)();
  101. _LIBCPP_FUNC_VIS new_handler set_new_handler(new_handler) _NOEXCEPT;
  102. _LIBCPP_FUNC_VIS new_handler get_new_handler() _NOEXCEPT;
  103. } // std
  104. #if defined(_WIN32) && !defined(cxx_EXPORTS)
  105. # define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS_ONLY
  106. #else
  107. # define _LIBCPP_NEW_DELETE_VIS _LIBCPP_FUNC_VIS
  108. #endif
  109. _LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz)
  110. #if !__has_feature(cxx_noexcept)
  111. throw(std::bad_alloc)
  112. #endif
  113. ;
  114. _LIBCPP_NEW_DELETE_VIS void* operator new(std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
  115. _LIBCPP_NEW_DELETE_VIS void operator delete(void* __p) _NOEXCEPT;
  116. _LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, const std::nothrow_t&) _NOEXCEPT;
  117. #if defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
  118. (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)
  119. _LIBCPP_NEW_DELETE_VIS void operator delete(void* __p, std::size_t __sz) _NOEXCEPT;
  120. #endif
  121. _LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz)
  122. #if !__has_feature(cxx_noexcept)
  123. throw(std::bad_alloc)
  124. #endif
  125. ;
  126. _LIBCPP_NEW_DELETE_VIS void* operator new[](std::size_t __sz, const std::nothrow_t&) _NOEXCEPT _NOALIAS;
  127. _LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p) _NOEXCEPT;
  128. _LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, const std::nothrow_t&) _NOEXCEPT;
  129. #if defined(_LIBCPP_BUILDING_NEW) || _LIBCPP_STD_VER >= 14 || \
  130. (defined(__cpp_sized_deallocation) && __cpp_sized_deallocation >= 201309)
  131. _LIBCPP_NEW_DELETE_VIS void operator delete[](void* __p, std::size_t __sz) _NOEXCEPT;
  132. #endif
  133. inline _LIBCPP_INLINE_VISIBILITY void* operator new (std::size_t, void* __p) _NOEXCEPT {return __p;}
  134. inline _LIBCPP_INLINE_VISIBILITY void* operator new[](std::size_t, void* __p) _NOEXCEPT {return __p;}
  135. inline _LIBCPP_INLINE_VISIBILITY void operator delete (void*, void*) _NOEXCEPT {}
  136. inline _LIBCPP_INLINE_VISIBILITY void operator delete[](void*, void*) _NOEXCEPT {}
  137. #endif // defined(_LIBCPP_SGX_CONFIG)
  138. _LIBCPP_BEGIN_NAMESPACE_STD
  139. inline _LIBCPP_INLINE_VISIBILITY void *__allocate(size_t __size) {
  140. #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
  141. return ::operator new(__size);
  142. #else
  143. return __builtin_operator_new(__size);
  144. #endif
  145. }
  146. inline _LIBCPP_INLINE_VISIBILITY void __deallocate(void *__ptr) {
  147. #ifdef _LIBCPP_HAS_NO_BUILTIN_OPERATOR_NEW_DELETE
  148. ::operator delete(__ptr);
  149. #else
  150. __builtin_operator_delete(__ptr);
  151. #endif
  152. }
  153. _LIBCPP_END_NAMESPACE_STD
  154. #endif // _LIBCPP_NEW