__debug 7.2 KB

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