__debug 7.9 KB

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