blob: bac580cfa800589e4c23ee320349ac64495d98d0 [file] [log] [blame]
Howard Hinnant27e0e772011-09-14 18:33:51 +00001// -*- 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 Hinnanta47c6d52011-09-16 17:29:17 +000014#if _LIBCPP_DEBUG_LEVEL >= 1
15
Howard Hinnant27e0e772011-09-14 18:33:51 +000016# include <cstdlib>
17# include <cstdio>
18# include <cstddef>
Howard Hinnant0405bf62013-03-25 19:29:35 +000019# ifndef _LIBCPP_ASSERT
20# define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : (_VSTD::printf("%s\n", m), _VSTD::abort()))
21# endif
Howard Hinnant27e0e772011-09-14 18:33:51 +000022
Howard Hinnanta47c6d52011-09-16 17:29:17 +000023#endif
24
25#if _LIBCPP_DEBUG_LEVEL >= 2
26
Howard Hinnant170425c2013-08-02 17:50:49 +000027#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28#pragma GCC system_header
29#endif
30
Howard Hinnant27e0e772011-09-14 18:33:51 +000031_LIBCPP_BEGIN_NAMESPACE_STD
32
Howard Hinnant8331b762013-03-06 23:30:19 +000033struct _LIBCPP_TYPE_VIS __c_node;
Howard Hinnant27e0e772011-09-14 18:33:51 +000034
Howard Hinnant8331b762013-03-06 23:30:19 +000035struct _LIBCPP_TYPE_VIS __i_node
Howard Hinnant27e0e772011-09-14 18:33:51 +000036{
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 Hinnant8331b762013-03-06 23:30:19 +000049struct _LIBCPP_TYPE_VIS __c_node
Howard Hinnant27e0e772011-09-14 18:33:51 +000050{
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 Hinnantb399c602011-09-27 23:55:03 +000069 void __add(__i_node* __i);
Howard Hinnant27e0e772011-09-14 18:33:51 +000070 _LIBCPP_HIDDEN void __remove(__i_node* __i);
71};
72
73template <class _Cont>
74struct _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
86template <class _Cont>
87bool
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 Hinnantc834c512011-11-29 18:15:50 +000092 _Cont* _Cp = static_cast<_Cont*>(__c_);
93 return _Cp->__dereferenceable(__j);
Howard Hinnant27e0e772011-09-14 18:33:51 +000094}
95
96template <class _Cont>
97bool
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 Hinnantc834c512011-11-29 18:15:50 +0000102 _Cont* _Cp = static_cast<_Cont*>(__c_);
103 return _Cp->__decrementable(__j);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000104}
105
106template <class _Cont>
107bool
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 Hinnantc834c512011-11-29 18:15:50 +0000112 _Cont* _Cp = static_cast<_Cont*>(__c_);
113 return _Cp->__addable(__j, __n);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000114}
115
116template <class _Cont>
117bool
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 Hinnantc834c512011-11-29 18:15:50 +0000122 _Cont* _Cp = static_cast<_Cont*>(__c_);
123 return _Cp->__subscriptable(__j, __n);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000124}
125
Howard Hinnant8331b762013-03-06 23:30:19 +0000126class _LIBCPP_TYPE_VIS __libcpp_db
Howard Hinnant27e0e772011-09-14 18:33:51 +0000127{
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();
136public:
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 Hinnantc90aa632011-09-16 19:52:23 +0000157 void __insert_i(void* __i);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000158 __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 Hinnantb399c602011-09-27 23:55:03 +0000168 __c_node* __find_c(void* __c) const;
Howard Hinnant27e0e772011-09-14 18:33:51 +0000169 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 Hinnante6ff0b62013-08-02 00:26:35 +0000178 bool __less_than_comparable(const void* __i, const void* __j) const;
Howard Hinnant27e0e772011-09-14 18:33:51 +0000179private:
180 _LIBCPP_HIDDEN
181 __i_node* __insert_iterator(void* __i);
182 _LIBCPP_HIDDEN
183 __i_node* __find_iterator(const void* __i) const;
184
Howard Hinnant8331b762013-03-06 23:30:19 +0000185 friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000186};
187
Howard Hinnant8331b762013-03-06 23:30:19 +0000188_LIBCPP_FUNC_VIS __libcpp_db* __get_db();
189_LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000190
191
192_LIBCPP_END_NAMESPACE_STD
193
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000194#endif
195
Howard Hinnant27e0e772011-09-14 18:33:51 +0000196#endif // _LIBCPP_DEBUG_H
197