__debug 8.5 KB

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