__debug 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_H
  10. #define _LIBCPP_DEBUG_H
  11. #include <__assert>
  12. #include <__config>
  13. #include <iosfwd>
  14. #include <type_traits>
  15. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  16. # pragma GCC system_header
  17. #endif
  18. #if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY)
  19. # include <cstddef>
  20. # include <cstdio>
  21. # include <cstdlib>
  22. #endif
  23. #if _LIBCPP_DEBUG_LEVEL == 0 || _LIBCPP_DEBUG_LEVEL == 1
  24. # define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0)
  25. #elif _LIBCPP_DEBUG_LEVEL == 2
  26. # define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(::std::__libcpp_is_constant_evaluated() || (x), m)
  27. #else
  28. # error _LIBCPP_DEBUG_LEVEL must be one of 0, 1, 2
  29. #endif
  30. _LIBCPP_BEGIN_NAMESPACE_STD
  31. #if _LIBCPP_DEBUG_LEVEL == 2 || defined(_LIBCPP_BUILDING_LIBRARY)
  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. __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. _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. __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. #endif // _LIBCPP_DEBUG_LEVEL == 2 || defined(_LIBCPP_BUILDING_LIBRARY)
  169. template <class _Tp>
  170. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 inline void __debug_db_insert_c(_Tp* __c) {
  171. #if _LIBCPP_DEBUG_LEVEL == 2
  172. if (!__libcpp_is_constant_evaluated())
  173. __get_db()->__insert_c(__c);
  174. #else
  175. (void)(__c);
  176. #endif
  177. }
  178. template <class _Tp>
  179. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 inline void __debug_db_insert_i(_Tp* __i) {
  180. #if _LIBCPP_DEBUG_LEVEL == 2
  181. if (!__libcpp_is_constant_evaluated())
  182. __get_db()->__insert_i(__i);
  183. #else
  184. (void)(__i);
  185. #endif
  186. }
  187. _LIBCPP_END_NAMESPACE_STD
  188. #endif // _LIBCPP_DEBUG_H