Howard Hinnant | 27e0e77 | 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 | |
Howard Hinnant | a47c6d5 | 2011-09-16 17:29:17 +0000 | [diff] [blame] | 14 | #if _LIBCPP_DEBUG_LEVEL >= 1 |
| 15 | |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 16 | # include <cstdlib> |
| 17 | # include <cstdio> |
| 18 | # include <cstddef> |
Howard Hinnant | 0405bf6 | 2013-03-25 19:29:35 +0000 | [diff] [blame] | 19 | # ifndef _LIBCPP_ASSERT |
| 20 | # define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : (_VSTD::printf("%s\n", m), _VSTD::abort())) |
| 21 | # endif |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 22 | |
Howard Hinnant | a47c6d5 | 2011-09-16 17:29:17 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | #if _LIBCPP_DEBUG_LEVEL >= 2 |
| 26 | |
Howard Hinnant | 170425c | 2013-08-02 17:50:49 +0000 | [diff] [blame^] | 27 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 28 | #pragma GCC system_header |
| 29 | #endif |
| 30 | |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 31 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 32 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 33 | struct _LIBCPP_TYPE_VIS __c_node; |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 34 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 35 | struct _LIBCPP_TYPE_VIS __i_node |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 36 | { |
| 37 | void* __i_; |
| 38 | __i_node* __next_; |
| 39 | __c_node* __c_; |
| 40 | |
| 41 | __i_node(const __i_node&) = delete; |
| 42 | __i_node& operator=(const __i_node&) = delete; |
| 43 | _LIBCPP_INLINE_VISIBILITY |
| 44 | __i_node(void* __i, __i_node* __next, __c_node* __c) |
| 45 | : __i_(__i), __next_(__next), __c_(__c) {} |
| 46 | ~__i_node(); |
| 47 | }; |
| 48 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 49 | struct _LIBCPP_TYPE_VIS __c_node |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 50 | { |
| 51 | void* __c_; |
| 52 | __c_node* __next_; |
| 53 | __i_node** beg_; |
| 54 | __i_node** end_; |
| 55 | __i_node** cap_; |
| 56 | |
| 57 | __c_node(const __c_node&) = delete; |
| 58 | __c_node& operator=(const __c_node&) = delete; |
| 59 | _LIBCPP_INLINE_VISIBILITY |
| 60 | __c_node(void* __c, __c_node* __next) |
| 61 | : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {} |
| 62 | virtual ~__c_node(); |
| 63 | |
| 64 | virtual bool __dereferenceable(const void*) const = 0; |
| 65 | virtual bool __decrementable(const void*) const = 0; |
| 66 | virtual bool __addable(const void*, ptrdiff_t) const = 0; |
| 67 | virtual bool __subscriptable(const void*, ptrdiff_t) const = 0; |
| 68 | |
Howard Hinnant | b399c60 | 2011-09-27 23:55:03 +0000 | [diff] [blame] | 69 | void __add(__i_node* __i); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 70 | _LIBCPP_HIDDEN void __remove(__i_node* __i); |
| 71 | }; |
| 72 | |
| 73 | template <class _Cont> |
| 74 | struct _C_node |
| 75 | : public __c_node |
| 76 | { |
| 77 | _C_node(void* __c, __c_node* __n) |
| 78 | : __c_node(__c, __n) {} |
| 79 | |
| 80 | virtual bool __dereferenceable(const void*) const; |
| 81 | virtual bool __decrementable(const void*) const; |
| 82 | virtual bool __addable(const void*, ptrdiff_t) const; |
| 83 | virtual bool __subscriptable(const void*, ptrdiff_t) const; |
| 84 | }; |
| 85 | |
| 86 | template <class _Cont> |
| 87 | bool |
| 88 | _C_node<_Cont>::__dereferenceable(const void* __i) const |
| 89 | { |
| 90 | typedef typename _Cont::const_iterator iterator; |
| 91 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 92 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 93 | return _Cp->__dereferenceable(__j); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | template <class _Cont> |
| 97 | bool |
| 98 | _C_node<_Cont>::__decrementable(const void* __i) const |
| 99 | { |
| 100 | typedef typename _Cont::const_iterator iterator; |
| 101 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 102 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 103 | return _Cp->__decrementable(__j); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | template <class _Cont> |
| 107 | bool |
| 108 | _C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const |
| 109 | { |
| 110 | typedef typename _Cont::const_iterator iterator; |
| 111 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 112 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 113 | return _Cp->__addable(__j, __n); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | template <class _Cont> |
| 117 | bool |
| 118 | _C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const |
| 119 | { |
| 120 | typedef typename _Cont::const_iterator iterator; |
| 121 | const iterator* __j = static_cast<const iterator*>(__i); |
Howard Hinnant | c834c51 | 2011-11-29 18:15:50 +0000 | [diff] [blame] | 122 | _Cont* _Cp = static_cast<_Cont*>(__c_); |
| 123 | return _Cp->__subscriptable(__j, __n); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 126 | class _LIBCPP_TYPE_VIS __libcpp_db |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 127 | { |
| 128 | __c_node** __cbeg_; |
| 129 | __c_node** __cend_; |
| 130 | size_t __csz_; |
| 131 | __i_node** __ibeg_; |
| 132 | __i_node** __iend_; |
| 133 | size_t __isz_; |
| 134 | |
| 135 | __libcpp_db(); |
| 136 | public: |
| 137 | __libcpp_db(const __libcpp_db&) = delete; |
| 138 | __libcpp_db& operator=(const __libcpp_db&) = delete; |
| 139 | ~__libcpp_db(); |
| 140 | |
| 141 | class __db_c_iterator; |
| 142 | class __db_c_const_iterator; |
| 143 | class __db_i_iterator; |
| 144 | class __db_i_const_iterator; |
| 145 | |
| 146 | __db_c_const_iterator __c_end() const; |
| 147 | __db_i_const_iterator __i_end() const; |
| 148 | |
| 149 | template <class _Cont> |
| 150 | _LIBCPP_INLINE_VISIBILITY |
| 151 | void __insert_c(_Cont* __c) |
| 152 | { |
| 153 | __c_node* __n = __insert_c(static_cast<void*>(__c)); |
| 154 | ::new(__n) _C_node<_Cont>(__n->__c_, __n->__next_); |
| 155 | } |
| 156 | |
Howard Hinnant | c90aa63 | 2011-09-16 19:52:23 +0000 | [diff] [blame] | 157 | void __insert_i(void* __i); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 158 | __c_node* __insert_c(void* __c); |
| 159 | void __erase_c(void* __c); |
| 160 | |
| 161 | void __insert_ic(void* __i, const void* __c); |
| 162 | void __iterator_copy(void* __i, const void* __i0); |
| 163 | void __erase_i(void* __i); |
| 164 | |
| 165 | void* __find_c_from_i(void* __i) const; |
| 166 | void __invalidate_all(void* __c); |
| 167 | __c_node* __find_c_and_lock(void* __c) const; |
Howard Hinnant | b399c60 | 2011-09-27 23:55:03 +0000 | [diff] [blame] | 168 | __c_node* __find_c(void* __c) const; |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 169 | void unlock() const; |
| 170 | |
| 171 | void swap(void* __c1, void* __c2); |
| 172 | |
| 173 | |
| 174 | bool __dereferenceable(const void* __i) const; |
| 175 | bool __decrementable(const void* __i) const; |
| 176 | bool __addable(const void* __i, ptrdiff_t __n) const; |
| 177 | bool __subscriptable(const void* __i, ptrdiff_t __n) const; |
Howard Hinnant | e6ff0b6 | 2013-08-02 00:26:35 +0000 | [diff] [blame] | 178 | bool __less_than_comparable(const void* __i, const void* __j) const; |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 179 | private: |
| 180 | _LIBCPP_HIDDEN |
| 181 | __i_node* __insert_iterator(void* __i); |
| 182 | _LIBCPP_HIDDEN |
| 183 | __i_node* __find_iterator(const void* __i) const; |
| 184 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 185 | friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db(); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Howard Hinnant | 8331b76 | 2013-03-06 23:30:19 +0000 | [diff] [blame] | 188 | _LIBCPP_FUNC_VIS __libcpp_db* __get_db(); |
| 189 | _LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db(); |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 190 | |
| 191 | |
| 192 | _LIBCPP_END_NAMESPACE_STD |
| 193 | |
Howard Hinnant | a47c6d5 | 2011-09-16 17:29:17 +0000 | [diff] [blame] | 194 | #endif |
| 195 | |
Howard Hinnant | 27e0e77 | 2011-09-14 18:33:51 +0000 | [diff] [blame] | 196 | #endif // _LIBCPP_DEBUG_H |
| 197 | |