Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- __debug ----------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_DEBUG_H |
| 11 | #define _LIBCPP_DEBUG_H |
| 12 | |
Eric Fiselier | 14b6de9 | 2014-08-10 23:53:08 +0000 | [diff] [blame] | 13 | #include <__config> |
Eric Fiselier | 873b8d3 | 2019-03-18 21:50:12 +0000 | [diff] [blame] | 14 | #include <iosfwd> |
Eric Fiselier | 14b6de9 | 2014-08-10 23:53:08 +0000 | [diff] [blame] | 15 | |
Howard Hinnant | 8ea9824 | 2013-08-23 17:37:05 +0000 | [diff] [blame] | 16 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 17 | #pragma GCC system_header |
| 18 | #endif |
| 19 | |
Eric Fiselier | 02e6fbc | 2016-12-28 06:15:01 +0000 | [diff] [blame] | 20 | #if defined(_LIBCPP_HAS_NO_NULLPTR) |
| 21 | # include <cstddef> |
| 22 | #endif |
| 23 | |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 24 | #if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY) |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 25 | # include <cstdlib> |
| 26 | # include <cstdio> |
| 27 | # include <cstddef> |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Louis Dionne | ba40078 | 2020-10-02 15:02:52 -0400 | [diff] [blame] | 30 | #if _LIBCPP_DEBUG_LEVEL == 0 |
Eric Fiselier | d8f24a4 | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 31 | # define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0) |
Louis Dionne | ba40078 | 2020-10-02 15:02:52 -0400 | [diff] [blame] | 32 | # define _LIBCPP_ASSERT_IMPL(x, m) ((void)0) |
| 33 | #elif _LIBCPP_DEBUG_LEVEL == 1 |
| 34 | # define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0) |
Louis Dionne | ba40078 | 2020-10-02 15:02:52 -0400 | [diff] [blame] | 35 | # define _LIBCPP_ASSERT_IMPL(x, m) ((x) ? (void)0 : _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m))) |
| 36 | #elif _LIBCPP_DEBUG_LEVEL == 2 |
| 37 | # define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(x, m) |
Louis Dionne | ba40078 | 2020-10-02 15:02:52 -0400 | [diff] [blame] | 38 | # define _LIBCPP_ASSERT_IMPL(x, m) ((x) ? (void)0 : _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m))) |
| 39 | #else |
| 40 | # error _LIBCPP_DEBUG_LEVEL must be one of 0, 1, 2 |
Eric Fiselier | d8f24a4 | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 41 | #endif |
Louis Dionne | ba40078 | 2020-10-02 15:02:52 -0400 | [diff] [blame] | 42 | |
| 43 | #if !defined(_LIBCPP_ASSERT) |
| 44 | # define _LIBCPP_ASSERT(x, m) _LIBCPP_ASSERT_IMPL(x, m) |
Eric Fiselier | d8f24a4 | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 45 | #endif |
Howard Hinnant | a47c6d5 | 2011-09-16 17:29:17 +0000 | [diff] [blame] | 46 | |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 47 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 48 | |
Eric Fiselier | b5eb1bf | 2017-01-04 23:56:00 +0000 | [diff] [blame] | 49 | struct _LIBCPP_TEMPLATE_VIS __libcpp_debug_info { |
Eric Fiselier | 3253fff | 2016-12-28 05:26:56 +0000 | [diff] [blame] | 50 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Eric Fiselier | 30ee693 | 2016-12-28 05:56:16 +0000 | [diff] [blame] | 51 | __libcpp_debug_info() |
| 52 | : __file_(nullptr), __line_(-1), __pred_(nullptr), __msg_(nullptr) {} |
| 53 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Eric Fiselier | 3253fff | 2016-12-28 05:26:56 +0000 | [diff] [blame] | 54 | __libcpp_debug_info(const char* __f, int __l, const char* __p, const char* __m) |
| 55 | : __file_(__f), __line_(__l), __pred_(__p), __msg_(__m) {} |
Eric Fiselier | 873b8d3 | 2019-03-18 21:50:12 +0000 | [diff] [blame] | 56 | |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 57 | _LIBCPP_FUNC_VIS string what() const; |
Eric Fiselier | 873b8d3 | 2019-03-18 21:50:12 +0000 | [diff] [blame] | 58 | |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 59 | const char* __file_; |
| 60 | int __line_; |
| 61 | const char* __pred_; |
| 62 | const char* __msg_; |
| 63 | }; |
| 64 | |
| 65 | /// __libcpp_debug_function_type - The type of the assertion failure handler. |
| 66 | typedef void(*__libcpp_debug_function_type)(__libcpp_debug_info const&); |
| 67 | |
| 68 | /// __libcpp_debug_function - The handler function called when a _LIBCPP_ASSERT |
| 69 | /// fails. |
Louis Dionne | 5254b37 | 2018-10-25 12:13:43 +0000 | [diff] [blame] | 70 | extern _LIBCPP_EXPORTED_FROM_ABI __libcpp_debug_function_type __libcpp_debug_function; |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 71 | |
| 72 | /// __libcpp_abort_debug_function - A debug handler that aborts when called. |
| 73 | _LIBCPP_NORETURN _LIBCPP_FUNC_VIS |
| 74 | void __libcpp_abort_debug_function(__libcpp_debug_info const&); |
| 75 | |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 76 | /// __libcpp_set_debug_function - Set the debug handler to the specified |
| 77 | /// function. |
| 78 | _LIBCPP_FUNC_VIS |
| 79 | bool __libcpp_set_debug_function(__libcpp_debug_function_type __func); |
| 80 | |
Louis Dionne | ba40078 | 2020-10-02 15:02:52 -0400 | [diff] [blame] | 81 | #if _LIBCPP_DEBUG_LEVEL == 2 || defined(_LIBCPP_BUILDING_LIBRARY) |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 82 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 83 | struct _LIBCPP_TYPE_VIS __c_node; |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 84 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 85 | struct _LIBCPP_TYPE_VIS __i_node |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 86 | { |
| 87 | void* __i_; |
| 88 | __i_node* __next_; |
| 89 | __c_node* __c_; |
| 90 | |
Eric Fiselier | 2d8515f | 2017-01-06 20:58:25 +0000 | [diff] [blame] | 91 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 92 | __i_node(const __i_node&) = delete; |
| 93 | __i_node& operator=(const __i_node&) = delete; |
Howard Hinnant | 8ea9824 | 2013-08-23 17:37:05 +0000 | [diff] [blame] | 94 | #else |
| 95 | private: |
| 96 | __i_node(const __i_node&); |
| 97 | __i_node& operator=(const __i_node&); |
| 98 | public: |
| 99 | #endif |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 100 | _LIBCPP_INLINE_VISIBILITY |
| 101 | __i_node(void* __i, __i_node* __next, __c_node* __c) |
| 102 | : __i_(__i), __next_(__next), __c_(__c) {} |
| 103 | ~__i_node(); |
| 104 | }; |
| 105 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 106 | struct _LIBCPP_TYPE_VIS __c_node |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 107 | { |
| 108 | void* __c_; |
| 109 | __c_node* __next_; |
| 110 | __i_node** beg_; |
| 111 | __i_node** end_; |
| 112 | __i_node** cap_; |
| 113 | |
Eric Fiselier | 2d8515f | 2017-01-06 20:58:25 +0000 | [diff] [blame] | 114 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 115 | __c_node(const __c_node&) = delete; |
| 116 | __c_node& operator=(const __c_node&) = delete; |
Howard Hinnant | 8ea9824 | 2013-08-23 17:37:05 +0000 | [diff] [blame] | 117 | #else |
| 118 | private: |
| 119 | __c_node(const __c_node&); |
| 120 | __c_node& operator=(const __c_node&); |
| 121 | public: |
| 122 | #endif |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 123 | _LIBCPP_INLINE_VISIBILITY |
| 124 | __c_node(void* __c, __c_node* __next) |
| 125 | : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {} |
| 126 | virtual ~__c_node(); |
| 127 | |
| 128 | virtual bool __dereferenceable(const void*) const = 0; |
| 129 | virtual bool __decrementable(const void*) const = 0; |
| 130 | virtual bool __addable(const void*, ptrdiff_t) const = 0; |
| 131 | virtual bool __subscriptable(const void*, ptrdiff_t) const = 0; |
| 132 | |
Howard Hinnant | b399c60 | 2011-09-27 23:55:03 +0000 | [diff] [blame] | 133 | void __add(__i_node* __i); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 134 | _LIBCPP_HIDDEN void __remove(__i_node* __i); |
| 135 | }; |
| 136 | |
| 137 | template <class _Cont> |
| 138 | struct _C_node |
| 139 | : public __c_node |
| 140 | { |
| 141 | _C_node(void* __c, __c_node* __n) |
| 142 | : __c_node(__c, __n) {} |
| 143 | |
| 144 | virtual bool __dereferenceable(const void*) const; |
| 145 | virtual bool __decrementable(const void*) const; |
| 146 | virtual bool __addable(const void*, ptrdiff_t) const; |
| 147 | virtual bool __subscriptable(const void*, ptrdiff_t) const; |
| 148 | }; |
| 149 | |
| 150 | template <class _Cont> |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 151 | inline bool |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 152 | _C_node<_Cont>::__dereferenceable(const void* __i) const |
| 153 | { |
| 154 | typedef typename _Cont::const_iterator iterator; |
| 155 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 156 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 157 | return _Cp->__dereferenceable(__j); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | template <class _Cont> |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 161 | inline bool |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 162 | _C_node<_Cont>::__decrementable(const void* __i) const |
| 163 | { |
| 164 | typedef typename _Cont::const_iterator iterator; |
| 165 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 166 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 167 | return _Cp->__decrementable(__j); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | template <class _Cont> |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 171 | inline bool |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 172 | _C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const |
| 173 | { |
| 174 | typedef typename _Cont::const_iterator iterator; |
| 175 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 176 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 177 | return _Cp->__addable(__j, __n); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | template <class _Cont> |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 181 | inline bool |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 182 | _C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const |
| 183 | { |
| 184 | typedef typename _Cont::const_iterator iterator; |
| 185 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 186 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 187 | return _Cp->__subscriptable(__j, __n); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 190 | class _LIBCPP_TYPE_VIS __libcpp_db |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 191 | { |
| 192 | __c_node** __cbeg_; |
| 193 | __c_node** __cend_; |
| 194 | size_t __csz_; |
| 195 | __i_node** __ibeg_; |
| 196 | __i_node** __iend_; |
| 197 | size_t __isz_; |
| 198 | |
| 199 | __libcpp_db(); |
| 200 | public: |
Eric Fiselier | 2d8515f | 2017-01-06 20:58:25 +0000 | [diff] [blame] | 201 | #ifndef _LIBCPP_CXX03_LANG |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 202 | __libcpp_db(const __libcpp_db&) = delete; |
| 203 | __libcpp_db& operator=(const __libcpp_db&) = delete; |
Howard Hinnant | 8ea9824 | 2013-08-23 17:37:05 +0000 | [diff] [blame] | 204 | #else |
| 205 | private: |
| 206 | __libcpp_db(const __libcpp_db&); |
| 207 | __libcpp_db& operator=(const __libcpp_db&); |
| 208 | public: |
| 209 | #endif |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 210 | ~__libcpp_db(); |
| 211 | |
| 212 | class __db_c_iterator; |
| 213 | class __db_c_const_iterator; |
| 214 | class __db_i_iterator; |
| 215 | class __db_i_const_iterator; |
| 216 | |
| 217 | __db_c_const_iterator __c_end() const; |
| 218 | __db_i_const_iterator __i_end() const; |
| 219 | |
Eric Fiselier | aed4eab | 2019-03-05 02:10:31 +0000 | [diff] [blame] | 220 | typedef __c_node*(_InsertConstruct)(void*, void*, __c_node*); |
| 221 | |
| 222 | template <class _Cont> |
| 223 | _LIBCPP_INLINE_VISIBILITY static __c_node* __create_C_node(void *__mem, void *__c, __c_node *__next) { |
Arthur O'Dwyer | 0c4472a | 2020-12-11 20:30:28 -0500 | [diff] [blame] | 224 | return ::new (__mem) _C_node<_Cont>(__c, __next); |
Eric Fiselier | aed4eab | 2019-03-05 02:10:31 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 227 | template <class _Cont> |
| 228 | _LIBCPP_INLINE_VISIBILITY |
| 229 | void __insert_c(_Cont* __c) |
| 230 | { |
Eric Fiselier | aed4eab | 2019-03-05 02:10:31 +0000 | [diff] [blame] | 231 | __insert_c(static_cast<void*>(__c), &__create_C_node<_Cont>); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Howard Hinnant | c90aa63 | 2011-09-16 19:52:23 +0000 | [diff] [blame] | 234 | void __insert_i(void* __i); |
Eric Fiselier | aed4eab | 2019-03-05 02:10:31 +0000 | [diff] [blame] | 235 | void __insert_c(void* __c, _InsertConstruct* __fn); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 236 | void __erase_c(void* __c); |
| 237 | |
| 238 | void __insert_ic(void* __i, const void* __c); |
| 239 | void __iterator_copy(void* __i, const void* __i0); |
| 240 | void __erase_i(void* __i); |
| 241 | |
| 242 | void* __find_c_from_i(void* __i) const; |
| 243 | void __invalidate_all(void* __c); |
| 244 | __c_node* __find_c_and_lock(void* __c) const; |
Howard Hinnant | b399c60 | 2011-09-27 23:55:03 +0000 | [diff] [blame] | 245 | __c_node* __find_c(void* __c) const; |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 246 | void unlock() const; |
| 247 | |
| 248 | void swap(void* __c1, void* __c2); |
| 249 | |
| 250 | |
| 251 | bool __dereferenceable(const void* __i) const; |
| 252 | bool __decrementable(const void* __i) const; |
| 253 | bool __addable(const void* __i, ptrdiff_t __n) const; |
| 254 | bool __subscriptable(const void* __i, ptrdiff_t __n) const; |
Howard Hinnant | e6ff0b6 | 2013-08-02 00:26:35 +0000 | [diff] [blame] | 255 | bool __less_than_comparable(const void* __i, const void* __j) const; |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 256 | private: |
| 257 | _LIBCPP_HIDDEN |
| 258 | __i_node* __insert_iterator(void* __i); |
| 259 | _LIBCPP_HIDDEN |
| 260 | __i_node* __find_iterator(const void* __i) const; |
| 261 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 262 | friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db(); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 263 | }; |
| 264 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 265 | _LIBCPP_FUNC_VIS __libcpp_db* __get_db(); |
| 266 | _LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db(); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 267 | |
| 268 | |
Louis Dionne | ba40078 | 2020-10-02 15:02:52 -0400 | [diff] [blame] | 269 | #endif // _LIBCPP_DEBUG_LEVEL == 2 || defined(_LIBCPP_BUILDING_LIBRARY) |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 270 | |
Eric Fiselier | fb82543 | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 271 | _LIBCPP_END_NAMESPACE_STD |
Howard Hinnant | a47c6d5 | 2011-09-16 17:29:17 +0000 | [diff] [blame] | 272 | |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 273 | #endif // _LIBCPP_DEBUG_H |