blob: e3a5ec965b4f0ca73c77ba3846260ebf05d86d3d [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 : \
29 _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 {
56 const char* __file_;
57 int __line_;
58 const char* __pred_;
59 const char* __msg_;
60};
61
62/// __libcpp_debug_function_type - The type of the assertion failure handler.
63typedef void(*__libcpp_debug_function_type)(__libcpp_debug_info const&);
64
65/// __libcpp_debug_function - The handler function called when a _LIBCPP_ASSERT
66/// fails.
67extern __libcpp_debug_function_type __libcpp_debug_function;
68
69/// __libcpp_abort_debug_function - A debug handler that aborts when called.
70_LIBCPP_NORETURN _LIBCPP_FUNC_VIS
71void __libcpp_abort_debug_function(__libcpp_debug_info const&);
72
73/// __libcpp_throw_debug_function - A debug handler that throws
74/// an instance of __libcpp_debug_exception when called.
75 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS
76void __libcpp_throw_debug_function(__libcpp_debug_info const&);
77
78/// __libcpp_set_debug_function - Set the debug handler to the specified
79/// function.
80_LIBCPP_FUNC_VIS
81bool __libcpp_set_debug_function(__libcpp_debug_function_type __func);
82
83// Setup the throwing debug handler during dynamic initialization.
84#if _LIBCPP_DEBUG_LEVEL >= 1 && defined(_LIBCPP_DEBUG_USE_EXCEPTIONS)
85static bool __init_dummy = __libcpp_set_debug_function(__libcpp_throw_debug_function);
86#endif
87
88#if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY)
89class _LIBCPP_EXCEPTION_ABI __libcpp_debug_exception : public exception {
90public:
91 __libcpp_debug_exception() _NOEXCEPT;
92 explicit __libcpp_debug_exception(__libcpp_debug_info const& __i);
93 __libcpp_debug_exception(__libcpp_debug_exception const&);
94 ~__libcpp_debug_exception() _NOEXCEPT;
95 const char* what() const _NOEXCEPT;
96private:
97 struct __libcpp_debug_exception_imp;
98 __libcpp_debug_exception_imp *__imp_;
99};
100#endif
101
102#if _LIBCPP_DEBUG_LEVEL >= 2 || defined(_LIBCPP_BUILDING_LIBRARY)
103
Howard Hinnant83eade62013-03-06 23:30:19 +0000104struct _LIBCPP_TYPE_VIS __c_node;
Howard Hinnant7a563db2011-09-14 18:33:51 +0000105
Howard Hinnant83eade62013-03-06 23:30:19 +0000106struct _LIBCPP_TYPE_VIS __i_node
Howard Hinnant7a563db2011-09-14 18:33:51 +0000107{
108 void* __i_;
109 __i_node* __next_;
110 __c_node* __c_;
111
Howard Hinnant499cea12013-08-23 17:37:05 +0000112#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
Howard Hinnant7a563db2011-09-14 18:33:51 +0000113 __i_node(const __i_node&) = delete;
114 __i_node& operator=(const __i_node&) = delete;
Howard Hinnant499cea12013-08-23 17:37:05 +0000115#else
116private:
117 __i_node(const __i_node&);
118 __i_node& operator=(const __i_node&);
119public:
120#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000121 _LIBCPP_INLINE_VISIBILITY
122 __i_node(void* __i, __i_node* __next, __c_node* __c)
123 : __i_(__i), __next_(__next), __c_(__c) {}
124 ~__i_node();
125};
126
Howard Hinnant83eade62013-03-06 23:30:19 +0000127struct _LIBCPP_TYPE_VIS __c_node
Howard Hinnant7a563db2011-09-14 18:33:51 +0000128{
129 void* __c_;
130 __c_node* __next_;
131 __i_node** beg_;
132 __i_node** end_;
133 __i_node** cap_;
134
Howard Hinnant499cea12013-08-23 17:37:05 +0000135#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
Howard Hinnant7a563db2011-09-14 18:33:51 +0000136 __c_node(const __c_node&) = delete;
137 __c_node& operator=(const __c_node&) = delete;
Howard Hinnant499cea12013-08-23 17:37:05 +0000138#else
139private:
140 __c_node(const __c_node&);
141 __c_node& operator=(const __c_node&);
142public:
143#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000144 _LIBCPP_INLINE_VISIBILITY
145 __c_node(void* __c, __c_node* __next)
146 : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {}
147 virtual ~__c_node();
148
149 virtual bool __dereferenceable(const void*) const = 0;
150 virtual bool __decrementable(const void*) const = 0;
151 virtual bool __addable(const void*, ptrdiff_t) const = 0;
152 virtual bool __subscriptable(const void*, ptrdiff_t) const = 0;
153
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000154 void __add(__i_node* __i);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000155 _LIBCPP_HIDDEN void __remove(__i_node* __i);
156};
157
158template <class _Cont>
159struct _C_node
160 : public __c_node
161{
162 _C_node(void* __c, __c_node* __n)
163 : __c_node(__c, __n) {}
164
165 virtual bool __dereferenceable(const void*) const;
166 virtual bool __decrementable(const void*) const;
167 virtual bool __addable(const void*, ptrdiff_t) const;
168 virtual bool __subscriptable(const void*, ptrdiff_t) const;
169};
170
171template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000172inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000173_C_node<_Cont>::__dereferenceable(const void* __i) const
174{
175 typedef typename _Cont::const_iterator iterator;
176 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000177 _Cont* _Cp = static_cast<_Cont*>(__c_);
178 return _Cp->__dereferenceable(__j);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000179}
180
181template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000182inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000183_C_node<_Cont>::__decrementable(const void* __i) const
184{
185 typedef typename _Cont::const_iterator iterator;
186 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000187 _Cont* _Cp = static_cast<_Cont*>(__c_);
188 return _Cp->__decrementable(__j);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000189}
190
191template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000192inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000193_C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const
194{
195 typedef typename _Cont::const_iterator iterator;
196 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000197 _Cont* _Cp = static_cast<_Cont*>(__c_);
198 return _Cp->__addable(__j, __n);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000199}
200
201template <class _Cont>
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000202inline bool
Howard Hinnant7a563db2011-09-14 18:33:51 +0000203_C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const
204{
205 typedef typename _Cont::const_iterator iterator;
206 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnant99968442011-11-29 18:15:50 +0000207 _Cont* _Cp = static_cast<_Cont*>(__c_);
208 return _Cp->__subscriptable(__j, __n);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000209}
210
Howard Hinnant83eade62013-03-06 23:30:19 +0000211class _LIBCPP_TYPE_VIS __libcpp_db
Howard Hinnant7a563db2011-09-14 18:33:51 +0000212{
213 __c_node** __cbeg_;
214 __c_node** __cend_;
215 size_t __csz_;
216 __i_node** __ibeg_;
217 __i_node** __iend_;
218 size_t __isz_;
219
220 __libcpp_db();
221public:
Howard Hinnant499cea12013-08-23 17:37:05 +0000222#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
Howard Hinnant7a563db2011-09-14 18:33:51 +0000223 __libcpp_db(const __libcpp_db&) = delete;
224 __libcpp_db& operator=(const __libcpp_db&) = delete;
Howard Hinnant499cea12013-08-23 17:37:05 +0000225#else
226private:
227 __libcpp_db(const __libcpp_db&);
228 __libcpp_db& operator=(const __libcpp_db&);
229public:
230#endif
Howard Hinnant7a563db2011-09-14 18:33:51 +0000231 ~__libcpp_db();
232
233 class __db_c_iterator;
234 class __db_c_const_iterator;
235 class __db_i_iterator;
236 class __db_i_const_iterator;
237
238 __db_c_const_iterator __c_end() const;
239 __db_i_const_iterator __i_end() const;
240
241 template <class _Cont>
242 _LIBCPP_INLINE_VISIBILITY
243 void __insert_c(_Cont* __c)
244 {
245 __c_node* __n = __insert_c(static_cast<void*>(__c));
246 ::new(__n) _C_node<_Cont>(__n->__c_, __n->__next_);
247 }
248
Howard Hinnant7608b4a2011-09-16 19:52:23 +0000249 void __insert_i(void* __i);
Howard Hinnant7a563db2011-09-14 18:33:51 +0000250 __c_node* __insert_c(void* __c);
251 void __erase_c(void* __c);
252
253 void __insert_ic(void* __i, const void* __c);
254 void __iterator_copy(void* __i, const void* __i0);
255 void __erase_i(void* __i);
256
257 void* __find_c_from_i(void* __i) const;
258 void __invalidate_all(void* __c);
259 __c_node* __find_c_and_lock(void* __c) const;
Howard Hinnant1c3ec6d2011-09-27 23:55:03 +0000260 __c_node* __find_c(void* __c) const;
Howard Hinnant7a563db2011-09-14 18:33:51 +0000261 void unlock() const;
262
263 void swap(void* __c1, void* __c2);
264
265
266 bool __dereferenceable(const void* __i) const;
267 bool __decrementable(const void* __i) const;
268 bool __addable(const void* __i, ptrdiff_t __n) const;
269 bool __subscriptable(const void* __i, ptrdiff_t __n) const;
Howard Hinnant8b00e6c2013-08-02 00:26:35 +0000270 bool __less_than_comparable(const void* __i, const void* __j) const;
Howard Hinnant7a563db2011-09-14 18:33:51 +0000271private:
272 _LIBCPP_HIDDEN
273 __i_node* __insert_iterator(void* __i);
274 _LIBCPP_HIDDEN
275 __i_node* __find_iterator(const void* __i) const;
276
Howard Hinnant83eade62013-03-06 23:30:19 +0000277 friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000278};
279
Howard Hinnant83eade62013-03-06 23:30:19 +0000280_LIBCPP_FUNC_VIS __libcpp_db* __get_db();
281_LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db();
Howard Hinnant7a563db2011-09-14 18:33:51 +0000282
283
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000284#endif // _LIBCPP_DEBUG_LEVEL >= 2 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant7a563db2011-09-14 18:33:51 +0000285
Eric Fiselier01eb99a2016-12-28 04:58:52 +0000286_LIBCPP_END_NAMESPACE_STD
Howard Hinnantabe26282011-09-16 17:29:17 +0000287
Howard Hinnant7a563db2011-09-14 18:33:51 +0000288#endif // _LIBCPP_DEBUG_H
289