blob: 2a4571024016162779d63ad261e8468645b49764 [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 Fiselier01eb99a2016-12-28 04:58:52 +000020#if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant7a563db2011-09-14 18:33:51 +000021# include <cstdlib>
22# include <cstdio>
23# include <cstddef>
Eric Fiselier01eb99a2016-12-28 04:58:52 +000024# include <exception>
25#endif
26
27#if _LIBCPP_DEBUG_LEVEL >= 1 && !defined(_LIBCPP_ASSERT)
28# define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : \
Eric Fiselierfa31c102016-12-28 05:26:56 +000029 _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m)))
Eric Fiselierb9536102014-08-10 23:53:08 +000030#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +000031
Eric Fiselierf6535882016-07-23 20:36:55 +000032#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 Fiselierb9536102014-08-10 23:53:08 +000039#ifndef _LIBCPP_ASSERT
40# define _LIBCPP_ASSERT(x, m) ((void)0)
Howard Hinnantabe26282011-09-16 17:29:17 +000041#endif
Eric Fiselierf6535882016-07-23 20:36:55 +000042#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 Hinnantabe26282011-09-16 17:29:17 +000048
Eric Fiselier01eb99a2016-12-28 04:58:52 +000049#if _LIBCPP_DEBUG_LEVEL < 1
50class _LIBCPP_EXCEPTION_ABI __libcpp_debug_exception;
51#endif
Howard Hinnantabe26282011-09-16 17:29:17 +000052
Howard Hinnant7a563db2011-09-14 18:33:51 +000053_LIBCPP_BEGIN_NAMESPACE_STD
54
Eric Fiselier01eb99a2016-12-28 04:58:52 +000055struct _LIBCPP_TYPE_VIS_ONLY __libcpp_debug_info {
Eric Fiselierfa31c102016-12-28 05:26:56 +000056 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier72a67ff2016-12-28 05:56:16 +000057 __libcpp_debug_info()
58 : __file_(nullptr), __line_(-1), __pred_(nullptr), __msg_(nullptr) {}
59 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselierfa31c102016-12-28 05:26:56 +000060 __libcpp_debug_info(const char* __f, int __l, const char* __p, const char* __m)
61 : __file_(__f), __line_(__l), __pred_(__p), __msg_(__m) {}
Eric Fiselier01eb99a2016-12-28 04:58:52 +000062 const char* __file_;
63 int __line_;
64 const char* __pred_;
65 const char* __msg_;
66};
67
68/// __libcpp_debug_function_type - The type of the assertion failure handler.
69typedef void(*__libcpp_debug_function_type)(__libcpp_debug_info const&);
70
71/// __libcpp_debug_function - The handler function called when a _LIBCPP_ASSERT
72/// fails.
73extern __libcpp_debug_function_type __libcpp_debug_function;
74
75/// __libcpp_abort_debug_function - A debug handler that aborts when called.
76_LIBCPP_NORETURN _LIBCPP_FUNC_VIS
77void __libcpp_abort_debug_function(__libcpp_debug_info const&);
78
79/// __libcpp_throw_debug_function - A debug handler that throws
80/// an instance of __libcpp_debug_exception when called.
81 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS
82void __libcpp_throw_debug_function(__libcpp_debug_info const&);
83
84/// __libcpp_set_debug_function - Set the debug handler to the specified
85/// function.
86_LIBCPP_FUNC_VIS
87bool __libcpp_set_debug_function(__libcpp_debug_function_type __func);
88
89// Setup the throwing debug handler during dynamic initialization.
90#if _LIBCPP_DEBUG_LEVEL >= 1 && defined(_LIBCPP_DEBUG_USE_EXCEPTIONS)
Eric Fiselier51d64bb2016-12-28 05:20:27 +000091# if defined(_LIBCPP_NO_EXCEPTIONS)
92# error _LIBCPP_DEBUG_USE_EXCEPTIONS cannot be used when exceptions are disabled.
93# endif
Eric Fiselier01eb99a2016-12-28 04:58:52 +000094static bool __init_dummy = __libcpp_set_debug_function(__libcpp_throw_debug_function);
95#endif
96
97#if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY)
98class _LIBCPP_EXCEPTION_ABI __libcpp_debug_exception : public exception {
99public:
100 __libcpp_debug_exception() _NOEXCEPT;
101 explicit __libcpp_debug_exception(__libcpp_debug_info const& __i);
102 __libcpp_debug_exception(__libcpp_debug_exception const&);
103 ~__libcpp_debug_exception() _NOEXCEPT;
104 const char* what() const _NOEXCEPT;
105private:
106 struct __libcpp_debug_exception_imp;
107 __libcpp_debug_exception_imp *__imp_;
108};
109#endif
110
111#if _LIBCPP_DEBUG_LEVEL >= 2 || defined(_LIBCPP_BUILDING_LIBRARY)
112
Howard Hinnant83eade62013-03-06 23:30:19 +0000113struct _LIBCPP_TYPE_VIS __c_node;
Howard Hinnant7a563db2011-09-14 18:33:51 +0000114
Howard Hinnant83eade62013-03-06 23:30:19 +0000115struct _LIBCPP_TYPE_VIS __i_node
Howard Hinnant7a563db2011-09-14 18:33:51 +0000116{
117 void* __i_;
118 __i_node* __next_;
119 __c_node* __c_;
120
Howard Hinnant499cea12013-08-23 17:37:05 +0000121#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
Howard Hinnant7a563db2011-09-14 18:33:51 +0000122 __i_node(const __i_node&) = delete;
123 __i_node& operator=(const __i_node&) = delete;
Howard Hinnant499cea12013-08-23 17:37:05 +0000124#else
125private:
126 __i_node(const __i_node&);
127 __i_node& operator=(const __i_node&);
128public:
129#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000130 _LIBCPP_INLINE_VISIBILITY
131 __i_node(void* __i, __i_node* __next, __c_node* __c)
132 : __i_(__i), __next_(__next), __c_(__c) {}
133 ~__i_node();
134};
135
Howard Hinnant83eade62013-03-06 23:30:19 +0000136struct _LIBCPP_TYPE_VIS __c_node
Howard Hinnant7a563db2011-09-14 18:33:51 +0000137{
138 void* __c_;
139 __c_node* __next_;
140 __i_node** beg_;
141 __i_node** end_;
142 __i_node** cap_;
143
Howard Hinnant499cea12013-08-23 17:37:05 +0000144#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
Howard Hinnant7a563db2011-09-14 18:33:51 +0000145 __c_node(const __c_node&) = delete;
146 __c_node& operator=(const __c_node&) = delete;
Howard Hinnant499cea12013-08-23 17:37:05 +0000147#else
148private:
149 __c_node(const __c_node&);
150 __c_node& operator=(const __c_node&);
151public:
152#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000153 _LIBCPP_INLINE_VISIBILITY
154 __c_node(void* __c, __c_node* __next)
155 : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {}
156 virtual ~__c_node();
157
158 virtual bool __dereferenceable(const void*) const = 0;
159 virtual bool __decrementable(const void*) const = 0;
160 virtual bool __addable(const void*, ptrdiff_t) const = 0;
161 virtual bool __subscriptable(const void*, ptrdiff_t) const = 0;
162
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000163 void __add(__i_node* __i);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000164 _LIBCPP_HIDDEN void __remove(__i_node* __i);
165};
166
167template <class _Cont>
168struct _C_node
169 : public __c_node
170{
171 _C_node(void* __c, __c_node* __n)
172 : __c_node(__c, __n) {}
173
174 virtual bool __dereferenceable(const void*) const;
175 virtual bool __decrementable(const void*) const;
176 virtual bool __addable(const void*, ptrdiff_t) const;
177 virtual bool __subscriptable(const void*, ptrdiff_t) const;
178};
179
180template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000181inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000182_C_node<_Cont>::__dereferenceable(const void* __i) const
183{
184 typedef typename _Cont::const_iterator iterator;
185 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000186 _Cont* _Cp = static_cast<_Cont*>(__c_);
187 return _Cp->__dereferenceable(__j);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000188}
189
190template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000191inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000192_C_node<_Cont>::__decrementable(const void* __i) const
193{
194 typedef typename _Cont::const_iterator iterator;
195 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000196 _Cont* _Cp = static_cast<_Cont*>(__c_);
197 return _Cp->__decrementable(__j);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000198}
199
200template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000201inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000202_C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const
203{
204 typedef typename _Cont::const_iterator iterator;
205 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000206 _Cont* _Cp = static_cast<_Cont*>(__c_);
207 return _Cp->__addable(__j, __n);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000208}
209
210template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000211inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000212_C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const
213{
214 typedef typename _Cont::const_iterator iterator;
215 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000216 _Cont* _Cp = static_cast<_Cont*>(__c_);
217 return _Cp->__subscriptable(__j, __n);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000218}
219
Howard Hinnant83eade62013-03-06 23:30:19 +0000220class _LIBCPP_TYPE_VIS __libcpp_db
Howard Hinnant7a563db2011-09-14 18:33:51 +0000221{
222 __c_node** __cbeg_;
223 __c_node** __cend_;
224 size_t __csz_;
225 __i_node** __ibeg_;
226 __i_node** __iend_;
227 size_t __isz_;
228
229 __libcpp_db();
230public:
Howard Hinnant499cea12013-08-23 17:37:05 +0000231#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
Howard Hinnant7a563db2011-09-14 18:33:51 +0000232 __libcpp_db(const __libcpp_db&) = delete;
233 __libcpp_db& operator=(const __libcpp_db&) = delete;
Howard Hinnant499cea12013-08-23 17:37:05 +0000234#else
235private:
236 __libcpp_db(const __libcpp_db&);
237 __libcpp_db& operator=(const __libcpp_db&);
238public:
239#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000240 ~__libcpp_db();
241
242 class __db_c_iterator;
243 class __db_c_const_iterator;
244 class __db_i_iterator;
245 class __db_i_const_iterator;
246
247 __db_c_const_iterator __c_end() const;
248 __db_i_const_iterator __i_end() const;
249
250 template <class _Cont>
251 _LIBCPP_INLINE_VISIBILITY
252 void __insert_c(_Cont* __c)
253 {
254 __c_node* __n = __insert_c(static_cast<void*>(__c));
255 ::new(__n) _C_node<_Cont>(__n->__c_, __n->__next_);
256 }
257
Howard Hinnant7608b4a2011-09-16 19:52:23 +0000258 void __insert_i(void* __i);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000259 __c_node* __insert_c(void* __c);
260 void __erase_c(void* __c);
261
262 void __insert_ic(void* __i, const void* __c);
263 void __iterator_copy(void* __i, const void* __i0);
264 void __erase_i(void* __i);
265
266 void* __find_c_from_i(void* __i) const;
267 void __invalidate_all(void* __c);
268 __c_node* __find_c_and_lock(void* __c) const;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000269 __c_node* __find_c(void* __c) const;
Howard Hinnant7a563db2011-09-14 18:33:51 +0000270 void unlock() const;
271
272 void swap(void* __c1, void* __c2);
273
274
275 bool __dereferenceable(const void* __i) const;
276 bool __decrementable(const void* __i) const;
277 bool __addable(const void* __i, ptrdiff_t __n) const;
278 bool __subscriptable(const void* __i, ptrdiff_t __n) const;
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000279 bool __less_than_comparable(const void* __i, const void* __j) const;
Howard Hinnant7a563db2011-09-14 18:33:51 +0000280private:
281 _LIBCPP_HIDDEN
282 __i_node* __insert_iterator(void* __i);
283 _LIBCPP_HIDDEN
284 __i_node* __find_iterator(const void* __i) const;
285
Howard Hinnant83eade62013-03-06 23:30:19 +0000286 friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000287};
288
Howard Hinnant83eade62013-03-06 23:30:19 +0000289_LIBCPP_FUNC_VIS __libcpp_db* __get_db();
290_LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000291
292
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000293#endif // _LIBCPP_DEBUG_LEVEL >= 2 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000294
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000295_LIBCPP_END_NAMESPACE_STD
Howard Hinnantabe26282011-09-16 17:29:17 +0000296
Howard Hinnant7a563db2011-09-14 18:33:51 +0000297#endif // _LIBCPP_DEBUG_H
298