__debug 7.8 KB

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