Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===--------------------------- __debug ----------------------------------===// |
| 3 | // |
| 4 | // The LLVM Compiler Infrastructure |
| 5 | // |
| 6 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 7 | // Source Licenses. See LICENSE.TXT for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #ifndef _LIBCPP_DEBUG_H |
| 12 | #define _LIBCPP_DEBUG_H |
| 13 | |
Eric Fiselier | b953610 | 2014-08-10 23:53:08 +0000 | [diff] [blame] | 14 | #include <__config> |
| 15 | |
Howard Hinnant | 499cea1 | 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 | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 20 | #if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY) |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 21 | # include <cstdlib> |
| 22 | # include <cstdio> |
| 23 | # include <cstddef> |
Eric Fiselier | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 24 | # include <exception> |
| 25 | #endif |
| 26 | |
| 27 | #if _LIBCPP_DEBUG_LEVEL >= 1 && !defined(_LIBCPP_ASSERT) |
| 28 | # define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : \ |
Eric Fiselier | fa31c10 | 2016-12-28 05:26:56 +0000 | [diff] [blame^] | 29 | _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m))) |
Eric Fiselier | b953610 | 2014-08-10 23:53:08 +0000 | [diff] [blame] | 30 | #endif |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 31 | |
Eric Fiselier | f653588 | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 32 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 33 | #ifndef _LIBCPP_DEBUG_ASSERT |
| 34 | #define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(x, m) |
| 35 | #endif |
| 36 | #define _LIBCPP_DEBUG_MODE(...) __VA_ARGS__ |
| 37 | #endif |
| 38 | |
Eric Fiselier | b953610 | 2014-08-10 23:53:08 +0000 | [diff] [blame] | 39 | #ifndef _LIBCPP_ASSERT |
| 40 | # define _LIBCPP_ASSERT(x, m) ((void)0) |
Howard Hinnant | abe2628 | 2011-09-16 17:29:17 +0000 | [diff] [blame] | 41 | #endif |
Eric Fiselier | f653588 | 2016-07-23 20:36:55 +0000 | [diff] [blame] | 42 | #ifndef _LIBCPP_DEBUG_ASSERT |
| 43 | # define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0) |
| 44 | #endif |
| 45 | #ifndef _LIBCPP_DEBUG_MODE |
| 46 | #define _LIBCPP_DEBUG_MODE(...) ((void)0) |
| 47 | #endif |
Howard Hinnant | abe2628 | 2011-09-16 17:29:17 +0000 | [diff] [blame] | 48 | |
Eric Fiselier | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 49 | #if _LIBCPP_DEBUG_LEVEL < 1 |
| 50 | class _LIBCPP_EXCEPTION_ABI __libcpp_debug_exception; |
| 51 | #endif |
Howard Hinnant | abe2628 | 2011-09-16 17:29:17 +0000 | [diff] [blame] | 52 | |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 53 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 54 | |
Eric Fiselier | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 55 | struct _LIBCPP_TYPE_VIS_ONLY __libcpp_debug_info { |
Eric Fiselier | fa31c10 | 2016-12-28 05:26:56 +0000 | [diff] [blame^] | 56 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 57 | __libcpp_debug_info(const char* __f, int __l, const char* __p, const char* __m) |
| 58 | : __file_(__f), __line_(__l), __pred_(__p), __msg_(__m) {} |
Eric Fiselier | 01eb99a | 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. |
| 70 | extern __libcpp_debug_function_type __libcpp_debug_function; |
| 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 | |
| 76 | /// __libcpp_throw_debug_function - A debug handler that throws |
| 77 | /// an instance of __libcpp_debug_exception when called. |
| 78 | _LIBCPP_NORETURN _LIBCPP_FUNC_VIS |
| 79 | void __libcpp_throw_debug_function(__libcpp_debug_info const&); |
| 80 | |
| 81 | /// __libcpp_set_debug_function - Set the debug handler to the specified |
| 82 | /// function. |
| 83 | _LIBCPP_FUNC_VIS |
| 84 | bool __libcpp_set_debug_function(__libcpp_debug_function_type __func); |
| 85 | |
| 86 | // Setup the throwing debug handler during dynamic initialization. |
| 87 | #if _LIBCPP_DEBUG_LEVEL >= 1 && defined(_LIBCPP_DEBUG_USE_EXCEPTIONS) |
Eric Fiselier | 51d64bb | 2016-12-28 05:20:27 +0000 | [diff] [blame] | 88 | # if defined(_LIBCPP_NO_EXCEPTIONS) |
| 89 | # error _LIBCPP_DEBUG_USE_EXCEPTIONS cannot be used when exceptions are disabled. |
| 90 | # endif |
Eric Fiselier | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 91 | static bool __init_dummy = __libcpp_set_debug_function(__libcpp_throw_debug_function); |
| 92 | #endif |
| 93 | |
| 94 | #if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY) |
| 95 | class _LIBCPP_EXCEPTION_ABI __libcpp_debug_exception : public exception { |
| 96 | public: |
| 97 | __libcpp_debug_exception() _NOEXCEPT; |
| 98 | explicit __libcpp_debug_exception(__libcpp_debug_info const& __i); |
| 99 | __libcpp_debug_exception(__libcpp_debug_exception const&); |
| 100 | ~__libcpp_debug_exception() _NOEXCEPT; |
| 101 | const char* what() const _NOEXCEPT; |
| 102 | private: |
| 103 | struct __libcpp_debug_exception_imp; |
| 104 | __libcpp_debug_exception_imp *__imp_; |
| 105 | }; |
| 106 | #endif |
| 107 | |
| 108 | #if _LIBCPP_DEBUG_LEVEL >= 2 || defined(_LIBCPP_BUILDING_LIBRARY) |
| 109 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 110 | struct _LIBCPP_TYPE_VIS __c_node; |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 111 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 112 | struct _LIBCPP_TYPE_VIS __i_node |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 113 | { |
| 114 | void* __i_; |
| 115 | __i_node* __next_; |
| 116 | __c_node* __c_; |
| 117 | |
Howard Hinnant | 499cea1 | 2013-08-23 17:37:05 +0000 | [diff] [blame] | 118 | #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 119 | __i_node(const __i_node&) = delete; |
| 120 | __i_node& operator=(const __i_node&) = delete; |
Howard Hinnant | 499cea1 | 2013-08-23 17:37:05 +0000 | [diff] [blame] | 121 | #else |
| 122 | private: |
| 123 | __i_node(const __i_node&); |
| 124 | __i_node& operator=(const __i_node&); |
| 125 | public: |
| 126 | #endif |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 127 | _LIBCPP_INLINE_VISIBILITY |
| 128 | __i_node(void* __i, __i_node* __next, __c_node* __c) |
| 129 | : __i_(__i), __next_(__next), __c_(__c) {} |
| 130 | ~__i_node(); |
| 131 | }; |
| 132 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 133 | struct _LIBCPP_TYPE_VIS __c_node |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 134 | { |
| 135 | void* __c_; |
| 136 | __c_node* __next_; |
| 137 | __i_node** beg_; |
| 138 | __i_node** end_; |
| 139 | __i_node** cap_; |
| 140 | |
Howard Hinnant | 499cea1 | 2013-08-23 17:37:05 +0000 | [diff] [blame] | 141 | #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 142 | __c_node(const __c_node&) = delete; |
| 143 | __c_node& operator=(const __c_node&) = delete; |
Howard Hinnant | 499cea1 | 2013-08-23 17:37:05 +0000 | [diff] [blame] | 144 | #else |
| 145 | private: |
| 146 | __c_node(const __c_node&); |
| 147 | __c_node& operator=(const __c_node&); |
| 148 | public: |
| 149 | #endif |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 150 | _LIBCPP_INLINE_VISIBILITY |
| 151 | __c_node(void* __c, __c_node* __next) |
| 152 | : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {} |
| 153 | virtual ~__c_node(); |
| 154 | |
| 155 | virtual bool __dereferenceable(const void*) const = 0; |
| 156 | virtual bool __decrementable(const void*) const = 0; |
| 157 | virtual bool __addable(const void*, ptrdiff_t) const = 0; |
| 158 | virtual bool __subscriptable(const void*, ptrdiff_t) const = 0; |
| 159 | |
Howard Hinnant | 1c3ec6d | 2011-09-27 23:55:03 +0000 | [diff] [blame] | 160 | void __add(__i_node* __i); |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 161 | _LIBCPP_HIDDEN void __remove(__i_node* __i); |
| 162 | }; |
| 163 | |
| 164 | template <class _Cont> |
| 165 | struct _C_node |
| 166 | : public __c_node |
| 167 | { |
| 168 | _C_node(void* __c, __c_node* __n) |
| 169 | : __c_node(__c, __n) {} |
| 170 | |
| 171 | virtual bool __dereferenceable(const void*) const; |
| 172 | virtual bool __decrementable(const void*) const; |
| 173 | virtual bool __addable(const void*, ptrdiff_t) const; |
| 174 | virtual bool __subscriptable(const void*, ptrdiff_t) const; |
| 175 | }; |
| 176 | |
| 177 | template <class _Cont> |
Eric Fiselier | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 178 | inline bool |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 179 | _C_node<_Cont>::__dereferenceable(const void* __i) const |
| 180 | { |
| 181 | typedef typename _Cont::const_iterator iterator; |
| 182 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 183 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 184 | return _Cp->__dereferenceable(__j); |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | template <class _Cont> |
Eric Fiselier | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 188 | inline bool |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 189 | _C_node<_Cont>::__decrementable(const void* __i) const |
| 190 | { |
| 191 | typedef typename _Cont::const_iterator iterator; |
| 192 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 193 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 194 | return _Cp->__decrementable(__j); |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | template <class _Cont> |
Eric Fiselier | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 198 | inline bool |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 199 | _C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const |
| 200 | { |
| 201 | typedef typename _Cont::const_iterator iterator; |
| 202 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 203 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 204 | return _Cp->__addable(__j, __n); |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | template <class _Cont> |
Eric Fiselier | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 208 | inline bool |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 209 | _C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const |
| 210 | { |
| 211 | typedef typename _Cont::const_iterator iterator; |
| 212 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | 9996844 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 213 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 214 | return _Cp->__subscriptable(__j, __n); |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 217 | class _LIBCPP_TYPE_VIS __libcpp_db |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 218 | { |
| 219 | __c_node** __cbeg_; |
| 220 | __c_node** __cend_; |
| 221 | size_t __csz_; |
| 222 | __i_node** __ibeg_; |
| 223 | __i_node** __iend_; |
| 224 | size_t __isz_; |
| 225 | |
| 226 | __libcpp_db(); |
| 227 | public: |
Howard Hinnant | 499cea1 | 2013-08-23 17:37:05 +0000 | [diff] [blame] | 228 | #ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 229 | __libcpp_db(const __libcpp_db&) = delete; |
| 230 | __libcpp_db& operator=(const __libcpp_db&) = delete; |
Howard Hinnant | 499cea1 | 2013-08-23 17:37:05 +0000 | [diff] [blame] | 231 | #else |
| 232 | private: |
| 233 | __libcpp_db(const __libcpp_db&); |
| 234 | __libcpp_db& operator=(const __libcpp_db&); |
| 235 | public: |
| 236 | #endif |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 237 | ~__libcpp_db(); |
| 238 | |
| 239 | class __db_c_iterator; |
| 240 | class __db_c_const_iterator; |
| 241 | class __db_i_iterator; |
| 242 | class __db_i_const_iterator; |
| 243 | |
| 244 | __db_c_const_iterator __c_end() const; |
| 245 | __db_i_const_iterator __i_end() const; |
| 246 | |
| 247 | template <class _Cont> |
| 248 | _LIBCPP_INLINE_VISIBILITY |
| 249 | void __insert_c(_Cont* __c) |
| 250 | { |
| 251 | __c_node* __n = __insert_c(static_cast<void*>(__c)); |
| 252 | ::new(__n) _C_node<_Cont>(__n->__c_, __n->__next_); |
| 253 | } |
| 254 | |
Howard Hinnant | 7608b4a | 2011-09-16 19:52:23 +0000 | [diff] [blame] | 255 | void __insert_i(void* __i); |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 256 | __c_node* __insert_c(void* __c); |
| 257 | void __erase_c(void* __c); |
| 258 | |
| 259 | void __insert_ic(void* __i, const void* __c); |
| 260 | void __iterator_copy(void* __i, const void* __i0); |
| 261 | void __erase_i(void* __i); |
| 262 | |
| 263 | void* __find_c_from_i(void* __i) const; |
| 264 | void __invalidate_all(void* __c); |
| 265 | __c_node* __find_c_and_lock(void* __c) const; |
Howard Hinnant | 1c3ec6d | 2011-09-27 23:55:03 +0000 | [diff] [blame] | 266 | __c_node* __find_c(void* __c) const; |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 267 | void unlock() const; |
| 268 | |
| 269 | void swap(void* __c1, void* __c2); |
| 270 | |
| 271 | |
| 272 | bool __dereferenceable(const void* __i) const; |
| 273 | bool __decrementable(const void* __i) const; |
| 274 | bool __addable(const void* __i, ptrdiff_t __n) const; |
| 275 | bool __subscriptable(const void* __i, ptrdiff_t __n) const; |
Howard Hinnant | 8b00e6c | 2013-08-02 00:26:35 +0000 | [diff] [blame] | 276 | bool __less_than_comparable(const void* __i, const void* __j) const; |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 277 | private: |
| 278 | _LIBCPP_HIDDEN |
| 279 | __i_node* __insert_iterator(void* __i); |
| 280 | _LIBCPP_HIDDEN |
| 281 | __i_node* __find_iterator(const void* __i) const; |
| 282 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 283 | friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db(); |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 284 | }; |
| 285 | |
Howard Hinnant | 83eade6 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 286 | _LIBCPP_FUNC_VIS __libcpp_db* __get_db(); |
| 287 | _LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db(); |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 288 | |
| 289 | |
Eric Fiselier | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 290 | #endif // _LIBCPP_DEBUG_LEVEL >= 2 || defined(_LIBCPP_BUILDING_LIBRARY) |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 291 | |
Eric Fiselier | 01eb99a | 2016-12-28 04:58:52 +0000 | [diff] [blame] | 292 | _LIBCPP_END_NAMESPACE_STD |
Howard Hinnant | abe2628 | 2011-09-16 17:29:17 +0000 | [diff] [blame] | 293 | |
Howard Hinnant | 7a563db | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 294 | #endif // _LIBCPP_DEBUG_H |
| 295 | |