__debug 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___DEBUG
  10. #define _LIBCPP___DEBUG
  11. #include <__assert>
  12. #include <__config>
  13. #include <cstddef>
  14. #include <type_traits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. // Catch invalid uses of the legacy _LIBCPP_DEBUG toggle.
  19. #if defined(_LIBCPP_DEBUG) && _LIBCPP_DEBUG != 0 && !defined(_LIBCPP_ENABLE_DEBUG_MODE)
  20. # error "Enabling the debug mode now requires having configured the library with support for the debug mode"
  21. #endif
  22. #if defined(_LIBCPP_ENABLE_DEBUG_MODE) && !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY)
  23. # define _LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY
  24. #endif
  25. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  26. # define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(::std::__libcpp_is_constant_evaluated() || (x), m)
  27. #else
  28. # define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0)
  29. #endif
  30. #if defined(_LIBCPP_ENABLE_DEBUG_MODE) || defined(_LIBCPP_BUILDING_LIBRARY)
  31. _LIBCPP_BEGIN_NAMESPACE_STD
  32. struct _LIBCPP_TYPE_VIS __c_node;
  33. struct _LIBCPP_TYPE_VIS __i_node
  34. {
  35. void* __i_;
  36. __i_node* __next_;
  37. __c_node* __c_;
  38. __i_node(const __i_node&) = delete;
  39. __i_node& operator=(const __i_node&) = delete;
  40. _LIBCPP_INLINE_VISIBILITY
  41. __i_node(void* __i, __i_node* __next, __c_node* __c)
  42. : __i_(__i), __next_(__next), __c_(__c) {}
  43. ~__i_node();
  44. };
  45. struct _LIBCPP_TYPE_VIS __c_node
  46. {
  47. void* __c_;
  48. __c_node* __next_;
  49. __i_node** beg_;
  50. __i_node** end_;
  51. __i_node** cap_;
  52. __c_node(const __c_node&) = delete;
  53. __c_node& operator=(const __c_node&) = delete;
  54. _LIBCPP_INLINE_VISIBILITY
  55. explicit __c_node(void* __c, __c_node* __next)
  56. : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {}
  57. virtual ~__c_node();
  58. virtual bool __dereferenceable(const void*) const = 0;
  59. virtual bool __decrementable(const void*) const = 0;
  60. virtual bool __addable(const void*, ptrdiff_t) const = 0;
  61. virtual bool __subscriptable(const void*, ptrdiff_t) const = 0;
  62. void __add(__i_node* __i);
  63. _LIBCPP_HIDDEN void __remove(__i_node* __i);
  64. };
  65. template <class _Cont>
  66. struct _C_node
  67. : public __c_node
  68. {
  69. explicit _C_node(void* __c, __c_node* __n)
  70. : __c_node(__c, __n) {}
  71. virtual bool __dereferenceable(const void*) const;
  72. virtual bool __decrementable(const void*) const;
  73. virtual bool __addable(const void*, ptrdiff_t) const;
  74. virtual bool __subscriptable(const void*, ptrdiff_t) const;
  75. };
  76. template <class _Cont>
  77. inline bool
  78. _C_node<_Cont>::__dereferenceable(const void* __i) const
  79. {
  80. typedef typename _Cont::const_iterator iterator;
  81. const iterator* __j = static_cast<const iterator*>(__i);
  82. _Cont* _Cp = static_cast<_Cont*>(__c_);
  83. return _Cp->__dereferenceable(__j);
  84. }
  85. template <class _Cont>
  86. inline bool
  87. _C_node<_Cont>::__decrementable(const void* __i) const
  88. {
  89. typedef typename _Cont::const_iterator iterator;
  90. const iterator* __j = static_cast<const iterator*>(__i);
  91. _Cont* _Cp = static_cast<_Cont*>(__c_);
  92. return _Cp->__decrementable(__j);
  93. }
  94. template <class _Cont>
  95. inline bool
  96. _C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const
  97. {
  98. typedef typename _Cont::const_iterator iterator;
  99. const iterator* __j = static_cast<const iterator*>(__i);
  100. _Cont* _Cp = static_cast<_Cont*>(__c_);
  101. return _Cp->__addable(__j, __n);
  102. }
  103. template <class _Cont>
  104. inline bool
  105. _C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const
  106. {
  107. typedef typename _Cont::const_iterator iterator;
  108. const iterator* __j = static_cast<const iterator*>(__i);
  109. _Cont* _Cp = static_cast<_Cont*>(__c_);
  110. return _Cp->__subscriptable(__j, __n);
  111. }
  112. class _LIBCPP_TYPE_VIS __libcpp_db
  113. {
  114. __c_node** __cbeg_;
  115. __c_node** __cend_;
  116. size_t __csz_;
  117. __i_node** __ibeg_;
  118. __i_node** __iend_;
  119. size_t __isz_;
  120. explicit __libcpp_db();
  121. public:
  122. __libcpp_db(const __libcpp_db&) = delete;
  123. __libcpp_db& operator=(const __libcpp_db&) = delete;
  124. ~__libcpp_db();
  125. class __db_c_iterator;
  126. class __db_c_const_iterator;
  127. class __db_i_iterator;
  128. class __db_i_const_iterator;
  129. __db_c_const_iterator __c_end() const;
  130. __db_i_const_iterator __i_end() const;
  131. typedef __c_node*(_InsertConstruct)(void*, void*, __c_node*);
  132. template <class _Cont>
  133. _LIBCPP_INLINE_VISIBILITY static __c_node* __create_C_node(void *__mem, void *__c, __c_node *__next) {
  134. return ::new (__mem) _C_node<_Cont>(__c, __next);
  135. }
  136. template <class _Cont>
  137. _LIBCPP_INLINE_VISIBILITY
  138. void __insert_c(_Cont* __c)
  139. {
  140. __insert_c(static_cast<void*>(__c), &__create_C_node<_Cont>);
  141. }
  142. void __insert_i(void* __i);
  143. void __insert_c(void* __c, _InsertConstruct* __fn);
  144. void __erase_c(void* __c);
  145. void __insert_ic(void* __i, const void* __c);
  146. void __iterator_copy(void* __i, const void* __i0);
  147. void __erase_i(void* __i);
  148. void* __find_c_from_i(void* __i) const;
  149. void __invalidate_all(void* __c);
  150. __c_node* __find_c_and_lock(void* __c) const;
  151. __c_node* __find_c(void* __c) const;
  152. void unlock() const;
  153. void swap(void* __c1, void* __c2);
  154. bool __dereferenceable(const void* __i) const;
  155. bool __decrementable(const void* __i) const;
  156. bool __addable(const void* __i, ptrdiff_t __n) const;
  157. bool __subscriptable(const void* __i, ptrdiff_t __n) const;
  158. bool __less_than_comparable(const void* __i, const void* __j) const;
  159. private:
  160. _LIBCPP_HIDDEN
  161. __i_node* __insert_iterator(void* __i);
  162. _LIBCPP_HIDDEN
  163. __i_node* __find_iterator(const void* __i) const;
  164. friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db();
  165. };
  166. _LIBCPP_FUNC_VIS __libcpp_db* __get_db();
  167. _LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db();
  168. _LIBCPP_END_NAMESPACE_STD
  169. #endif // defined(_LIBCPP_ENABLE_DEBUG_MODE) || defined(_LIBCPP_BUILDING_LIBRARY)
  170. _LIBCPP_BEGIN_NAMESPACE_STD
  171. template <class _Tp>
  172. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 inline void __debug_db_insert_c(_Tp* __c) {
  173. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  174. if (!__libcpp_is_constant_evaluated())
  175. __get_db()->__insert_c(__c);
  176. #else
  177. (void)(__c);
  178. #endif
  179. }
  180. template <class _Tp>
  181. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 inline void __debug_db_insert_i(_Tp* __i) {
  182. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  183. if (!__libcpp_is_constant_evaluated())
  184. __get_db()->__insert_i(__i);
  185. #else
  186. (void)(__i);
  187. #endif
  188. }
  189. template <class _Tp>
  190. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 inline void __debug_db_erase_c(_Tp* __c) {
  191. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  192. if (!__libcpp_is_constant_evaluated())
  193. __get_db()->__erase_c(__c);
  194. #else
  195. (void)(__c);
  196. #endif
  197. }
  198. template <class _Tp>
  199. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 inline void __debug_db_swap(_Tp* __lhs, _Tp* __rhs) {
  200. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  201. if (!__libcpp_is_constant_evaluated())
  202. __get_db()->swap(__lhs, __rhs);
  203. #else
  204. (void)(__lhs);
  205. (void)(__rhs);
  206. #endif
  207. }
  208. template <class _Tp>
  209. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 inline void __debug_db_invalidate_all(_Tp* __c) {
  210. #ifdef _LIBCPP_ENABLE_DEBUG_MODE
  211. if (!__libcpp_is_constant_evaluated())
  212. __get_db()->__invalidate_all(__c);
  213. #else
  214. (void)(__c);
  215. #endif
  216. }
  217. _LIBCPP_END_NAMESPACE_STD
  218. #endif // _LIBCPP___DEBUG