blob: a8788f68f8f405f28f377926fd3510f4c35b62a7 [file] [log] [blame]
Howard Hinnant7a563db2011-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
Eric Fiselierb9536102014-08-10 23:53:08 +000014#include <__config>
15
Howard Hinnant499cea12013-08-23 17:37:05 +000016#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17#pragma GCC system_header
18#endif
19
Eric Fiselier32eebb72016-12-28 06:15:01 +000020#if defined(_LIBCPP_HAS_NO_NULLPTR)
21# include <cstddef>
22#endif
23
Eric Fiselier01eb99a2016-12-28 04:58:52 +000024#if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant7a563db2011-09-14 18:33:51 +000025# include <cstdlib>
26# include <cstdio>
27# include <cstddef>
Eric Fiselier01eb99a2016-12-28 04:58:52 +000028# include <exception>
29#endif
30
31#if _LIBCPP_DEBUG_LEVEL >= 1 && !defined(_LIBCPP_ASSERT)
32# define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : \
Eric Fiselierfa31c102016-12-28 05:26:56 +000033 _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m)))
Eric Fiselierb9536102014-08-10 23:53:08 +000034#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +000035
Eric Fiselierf6535882016-07-23 20:36:55 +000036#if _LIBCPP_DEBUG_LEVEL >= 2
37#ifndef _LIBCPP_DEBUG_ASSERT
38#define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(x, m)
39#endif
40#define _LIBCPP_DEBUG_MODE(...) __VA_ARGS__
41#endif
42
Eric Fiselierb9536102014-08-10 23:53:08 +000043#ifndef _LIBCPP_ASSERT
44# define _LIBCPP_ASSERT(x, m) ((void)0)
Howard Hinnantabe26282011-09-16 17:29:17 +000045#endif
Eric Fiselierf6535882016-07-23 20:36:55 +000046#ifndef _LIBCPP_DEBUG_ASSERT
47# define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0)
48#endif
49#ifndef _LIBCPP_DEBUG_MODE
50#define _LIBCPP_DEBUG_MODE(...) ((void)0)
51#endif
Howard Hinnantabe26282011-09-16 17:29:17 +000052
Eric Fiselier01eb99a2016-12-28 04:58:52 +000053#if _LIBCPP_DEBUG_LEVEL < 1
54class _LIBCPP_EXCEPTION_ABI __libcpp_debug_exception;
55#endif
Howard Hinnantabe26282011-09-16 17:29:17 +000056
Howard Hinnant7a563db2011-09-14 18:33:51 +000057_LIBCPP_BEGIN_NAMESPACE_STD
58
Eric Fiselierc3589a82017-01-04 23:56:00 +000059struct _LIBCPP_TEMPLATE_VIS __libcpp_debug_info {
Eric Fiselierfa31c102016-12-28 05:26:56 +000060 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier72a67ff2016-12-28 05:56:16 +000061 __libcpp_debug_info()
62 : __file_(nullptr), __line_(-1), __pred_(nullptr), __msg_(nullptr) {}
63 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselierfa31c102016-12-28 05:26:56 +000064 __libcpp_debug_info(const char* __f, int __l, const char* __p, const char* __m)
65 : __file_(__f), __line_(__l), __pred_(__p), __msg_(__m) {}
Eric Fiselier01eb99a2016-12-28 04:58:52 +000066 const char* __file_;
67 int __line_;
68 const char* __pred_;
69 const char* __msg_;
70};
71
72/// __libcpp_debug_function_type - The type of the assertion failure handler.
73typedef void(*__libcpp_debug_function_type)(__libcpp_debug_info const&);
74
75/// __libcpp_debug_function - The handler function called when a _LIBCPP_ASSERT
76/// fails.
Louis Dionne07f95bd2018-10-25 12:13:43 +000077extern _LIBCPP_EXPORTED_FROM_ABI __libcpp_debug_function_type __libcpp_debug_function;
Eric Fiselier01eb99a2016-12-28 04:58:52 +000078
79/// __libcpp_abort_debug_function - A debug handler that aborts when called.
80_LIBCPP_NORETURN _LIBCPP_FUNC_VIS
81void __libcpp_abort_debug_function(__libcpp_debug_info const&);
82
83/// __libcpp_throw_debug_function - A debug handler that throws
84/// an instance of __libcpp_debug_exception when called.
85 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS
86void __libcpp_throw_debug_function(__libcpp_debug_info const&);
87
88/// __libcpp_set_debug_function - Set the debug handler to the specified
89/// function.
90_LIBCPP_FUNC_VIS
91bool __libcpp_set_debug_function(__libcpp_debug_function_type __func);
92
93// Setup the throwing debug handler during dynamic initialization.
94#if _LIBCPP_DEBUG_LEVEL >= 1 && defined(_LIBCPP_DEBUG_USE_EXCEPTIONS)
Eric Fiselier51d64bb2016-12-28 05:20:27 +000095# if defined(_LIBCPP_NO_EXCEPTIONS)
96# error _LIBCPP_DEBUG_USE_EXCEPTIONS cannot be used when exceptions are disabled.
97# endif
Eric Fiselier01eb99a2016-12-28 04:58:52 +000098static bool __init_dummy = __libcpp_set_debug_function(__libcpp_throw_debug_function);
99#endif
100
101#if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY)
102class _LIBCPP_EXCEPTION_ABI __libcpp_debug_exception : public exception {
103public:
104 __libcpp_debug_exception() _NOEXCEPT;
105 explicit __libcpp_debug_exception(__libcpp_debug_info const& __i);
106 __libcpp_debug_exception(__libcpp_debug_exception const&);
107 ~__libcpp_debug_exception() _NOEXCEPT;
108 const char* what() const _NOEXCEPT;
109private:
110 struct __libcpp_debug_exception_imp;
111 __libcpp_debug_exception_imp *__imp_;
112};
113#endif
114
115#if _LIBCPP_DEBUG_LEVEL >= 2 || defined(_LIBCPP_BUILDING_LIBRARY)
116
Howard Hinnant83eade62013-03-06 23:30:19 +0000117struct _LIBCPP_TYPE_VIS __c_node;
Howard Hinnant7a563db2011-09-14 18:33:51 +0000118
Howard Hinnant83eade62013-03-06 23:30:19 +0000119struct _LIBCPP_TYPE_VIS __i_node
Howard Hinnant7a563db2011-09-14 18:33:51 +0000120{
121 void* __i_;
122 __i_node* __next_;
123 __c_node* __c_;
124
Eric Fiselier8eb066a2017-01-06 20:58:25 +0000125#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant7a563db2011-09-14 18:33:51 +0000126 __i_node(const __i_node&) = delete;
127 __i_node& operator=(const __i_node&) = delete;
Howard Hinnant499cea12013-08-23 17:37:05 +0000128#else
129private:
130 __i_node(const __i_node&);
131 __i_node& operator=(const __i_node&);
132public:
133#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000134 _LIBCPP_INLINE_VISIBILITY
135 __i_node(void* __i, __i_node* __next, __c_node* __c)
136 : __i_(__i), __next_(__next), __c_(__c) {}
137 ~__i_node();
138};
139
Howard Hinnant83eade62013-03-06 23:30:19 +0000140struct _LIBCPP_TYPE_VIS __c_node
Howard Hinnant7a563db2011-09-14 18:33:51 +0000141{
142 void* __c_;
143 __c_node* __next_;
144 __i_node** beg_;
145 __i_node** end_;
146 __i_node** cap_;
147
Eric Fiselier8eb066a2017-01-06 20:58:25 +0000148#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant7a563db2011-09-14 18:33:51 +0000149 __c_node(const __c_node&) = delete;
150 __c_node& operator=(const __c_node&) = delete;
Howard Hinnant499cea12013-08-23 17:37:05 +0000151#else
152private:
153 __c_node(const __c_node&);
154 __c_node& operator=(const __c_node&);
155public:
156#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000157 _LIBCPP_INLINE_VISIBILITY
158 __c_node(void* __c, __c_node* __next)
159 : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {}
160 virtual ~__c_node();
161
162 virtual bool __dereferenceable(const void*) const = 0;
163 virtual bool __decrementable(const void*) const = 0;
164 virtual bool __addable(const void*, ptrdiff_t) const = 0;
165 virtual bool __subscriptable(const void*, ptrdiff_t) const = 0;
166
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000167 void __add(__i_node* __i);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000168 _LIBCPP_HIDDEN void __remove(__i_node* __i);
169};
170
171template <class _Cont>
172struct _C_node
173 : public __c_node
174{
175 _C_node(void* __c, __c_node* __n)
176 : __c_node(__c, __n) {}
177
178 virtual bool __dereferenceable(const void*) const;
179 virtual bool __decrementable(const void*) const;
180 virtual bool __addable(const void*, ptrdiff_t) const;
181 virtual bool __subscriptable(const void*, ptrdiff_t) const;
182};
183
184template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000185inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000186_C_node<_Cont>::__dereferenceable(const void* __i) const
187{
188 typedef typename _Cont::const_iterator iterator;
189 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000190 _Cont* _Cp = static_cast<_Cont*>(__c_);
191 return _Cp->__dereferenceable(__j);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000192}
193
194template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000195inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000196_C_node<_Cont>::__decrementable(const void* __i) const
197{
198 typedef typename _Cont::const_iterator iterator;
199 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000200 _Cont* _Cp = static_cast<_Cont*>(__c_);
201 return _Cp->__decrementable(__j);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000202}
203
204template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000205inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000206_C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const
207{
208 typedef typename _Cont::const_iterator iterator;
209 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000210 _Cont* _Cp = static_cast<_Cont*>(__c_);
211 return _Cp->__addable(__j, __n);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000212}
213
214template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000215inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000216_C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const
217{
218 typedef typename _Cont::const_iterator iterator;
219 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000220 _Cont* _Cp = static_cast<_Cont*>(__c_);
221 return _Cp->__subscriptable(__j, __n);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000222}
223
Howard Hinnant83eade62013-03-06 23:30:19 +0000224class _LIBCPP_TYPE_VIS __libcpp_db
Howard Hinnant7a563db2011-09-14 18:33:51 +0000225{
226 __c_node** __cbeg_;
227 __c_node** __cend_;
228 size_t __csz_;
229 __i_node** __ibeg_;
230 __i_node** __iend_;
231 size_t __isz_;
232
233 __libcpp_db();
234public:
Eric Fiselier8eb066a2017-01-06 20:58:25 +0000235#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant7a563db2011-09-14 18:33:51 +0000236 __libcpp_db(const __libcpp_db&) = delete;
237 __libcpp_db& operator=(const __libcpp_db&) = delete;
Howard Hinnant499cea12013-08-23 17:37:05 +0000238#else
239private:
240 __libcpp_db(const __libcpp_db&);
241 __libcpp_db& operator=(const __libcpp_db&);
242public:
243#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000244 ~__libcpp_db();
245
246 class __db_c_iterator;
247 class __db_c_const_iterator;
248 class __db_i_iterator;
249 class __db_i_const_iterator;
250
251 __db_c_const_iterator __c_end() const;
252 __db_i_const_iterator __i_end() const;
253
254 template <class _Cont>
255 _LIBCPP_INLINE_VISIBILITY
256 void __insert_c(_Cont* __c)
257 {
258 __c_node* __n = __insert_c(static_cast<void*>(__c));
259 ::new(__n) _C_node<_Cont>(__n->__c_, __n->__next_);
260 }
261
Howard Hinnant7608b4a2011-09-16 19:52:23 +0000262 void __insert_i(void* __i);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000263 __c_node* __insert_c(void* __c);
264 void __erase_c(void* __c);
265
266 void __insert_ic(void* __i, const void* __c);
267 void __iterator_copy(void* __i, const void* __i0);
268 void __erase_i(void* __i);
269
270 void* __find_c_from_i(void* __i) const;
271 void __invalidate_all(void* __c);
272 __c_node* __find_c_and_lock(void* __c) const;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000273 __c_node* __find_c(void* __c) const;
Howard Hinnant7a563db2011-09-14 18:33:51 +0000274 void unlock() const;
275
276 void swap(void* __c1, void* __c2);
277
278
279 bool __dereferenceable(const void* __i) const;
280 bool __decrementable(const void* __i) const;
281 bool __addable(const void* __i, ptrdiff_t __n) const;
282 bool __subscriptable(const void* __i, ptrdiff_t __n) const;
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000283 bool __less_than_comparable(const void* __i, const void* __j) const;
Howard Hinnant7a563db2011-09-14 18:33:51 +0000284private:
285 _LIBCPP_HIDDEN
286 __i_node* __insert_iterator(void* __i);
287 _LIBCPP_HIDDEN
288 __i_node* __find_iterator(const void* __i) const;
289
Howard Hinnant83eade62013-03-06 23:30:19 +0000290 friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000291};
292
Howard Hinnant83eade62013-03-06 23:30:19 +0000293_LIBCPP_FUNC_VIS __libcpp_db* __get_db();
294_LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000295
296
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000297#endif // _LIBCPP_DEBUG_LEVEL >= 2 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000298
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000299_LIBCPP_END_NAMESPACE_STD
Howard Hinnantabe26282011-09-16 17:29:17 +0000300
Howard Hinnant7a563db2011-09-14 18:33:51 +0000301#endif // _LIBCPP_DEBUG_H
302