blob: 6ccb72cb8ad0e25e416fcf7a87ea4129bf241f72 [file] [log] [blame]
Howard Hinnant27e0e772011-09-14 18:33:51 +00001// -*- C++ -*-
2//===--------------------------- __debug ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnant27e0e772011-09-14 18:33:51 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_DEBUG_H
11#define _LIBCPP_DEBUG_H
12
Eric Fiselier14b6de92014-08-10 23:53:08 +000013#include <__config>
14
Howard Hinnant8ea98242013-08-23 17:37:05 +000015#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16#pragma GCC system_header
17#endif
18
Eric Fiselier02e6fbc2016-12-28 06:15:01 +000019#if defined(_LIBCPP_HAS_NO_NULLPTR)
20# include <cstddef>
21#endif
22
Eric Fiselierfb825432016-12-28 04:58:52 +000023#if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant27e0e772011-09-14 18:33:51 +000024# include <cstdlib>
25# include <cstdio>
26# include <cstddef>
Eric Fiselierfb825432016-12-28 04:58:52 +000027# include <exception>
28#endif
29
30#if _LIBCPP_DEBUG_LEVEL >= 1 && !defined(_LIBCPP_ASSERT)
31# define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : \
Eric Fiselier3253fff2016-12-28 05:26:56 +000032 _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m)))
Eric Fiselier14b6de92014-08-10 23:53:08 +000033#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +000034
Eric Fiselierd8f24a42016-07-23 20:36:55 +000035#if _LIBCPP_DEBUG_LEVEL >= 2
36#ifndef _LIBCPP_DEBUG_ASSERT
37#define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(x, m)
38#endif
39#define _LIBCPP_DEBUG_MODE(...) __VA_ARGS__
40#endif
41
Eric Fiselier14b6de92014-08-10 23:53:08 +000042#ifndef _LIBCPP_ASSERT
43# define _LIBCPP_ASSERT(x, m) ((void)0)
Howard Hinnanta47c6d52011-09-16 17:29:17 +000044#endif
Eric Fiselierd8f24a42016-07-23 20:36:55 +000045#ifndef _LIBCPP_DEBUG_ASSERT
46# define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0)
47#endif
48#ifndef _LIBCPP_DEBUG_MODE
49#define _LIBCPP_DEBUG_MODE(...) ((void)0)
50#endif
Howard Hinnanta47c6d52011-09-16 17:29:17 +000051
Eric Fiselierfb825432016-12-28 04:58:52 +000052#if _LIBCPP_DEBUG_LEVEL < 1
53class _LIBCPP_EXCEPTION_ABI __libcpp_debug_exception;
54#endif
Howard Hinnanta47c6d52011-09-16 17:29:17 +000055
Howard Hinnant27e0e772011-09-14 18:33:51 +000056_LIBCPP_BEGIN_NAMESPACE_STD
57
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +000058struct _LIBCPP_TEMPLATE_VIS __libcpp_debug_info {
Eric Fiselier3253fff2016-12-28 05:26:56 +000059 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier30ee6932016-12-28 05:56:16 +000060 __libcpp_debug_info()
61 : __file_(nullptr), __line_(-1), __pred_(nullptr), __msg_(nullptr) {}
62 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier3253fff2016-12-28 05:26:56 +000063 __libcpp_debug_info(const char* __f, int __l, const char* __p, const char* __m)
64 : __file_(__f), __line_(__l), __pred_(__p), __msg_(__m) {}
Eric Fiselierfb825432016-12-28 04:58:52 +000065 const char* __file_;
66 int __line_;
67 const char* __pred_;
68 const char* __msg_;
69};
70
71/// __libcpp_debug_function_type - The type of the assertion failure handler.
72typedef void(*__libcpp_debug_function_type)(__libcpp_debug_info const&);
73
74/// __libcpp_debug_function - The handler function called when a _LIBCPP_ASSERT
75/// fails.
Louis Dionne5254b372018-10-25 12:13:43 +000076extern _LIBCPP_EXPORTED_FROM_ABI __libcpp_debug_function_type __libcpp_debug_function;
Eric Fiselierfb825432016-12-28 04:58:52 +000077
78/// __libcpp_abort_debug_function - A debug handler that aborts when called.
79_LIBCPP_NORETURN _LIBCPP_FUNC_VIS
80void __libcpp_abort_debug_function(__libcpp_debug_info const&);
81
82/// __libcpp_throw_debug_function - A debug handler that throws
83/// an instance of __libcpp_debug_exception when called.
84 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS
85void __libcpp_throw_debug_function(__libcpp_debug_info const&);
86
87/// __libcpp_set_debug_function - Set the debug handler to the specified
88/// function.
89_LIBCPP_FUNC_VIS
90bool __libcpp_set_debug_function(__libcpp_debug_function_type __func);
91
92// Setup the throwing debug handler during dynamic initialization.
93#if _LIBCPP_DEBUG_LEVEL >= 1 && defined(_LIBCPP_DEBUG_USE_EXCEPTIONS)
Eric Fiselierb18682d2016-12-28 05:20:27 +000094# if defined(_LIBCPP_NO_EXCEPTIONS)
95# error _LIBCPP_DEBUG_USE_EXCEPTIONS cannot be used when exceptions are disabled.
96# endif
Eric Fiselierfb825432016-12-28 04:58:52 +000097static bool __init_dummy = __libcpp_set_debug_function(__libcpp_throw_debug_function);
98#endif
99
100#if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY)
101class _LIBCPP_EXCEPTION_ABI __libcpp_debug_exception : public exception {
102public:
103 __libcpp_debug_exception() _NOEXCEPT;
104 explicit __libcpp_debug_exception(__libcpp_debug_info const& __i);
105 __libcpp_debug_exception(__libcpp_debug_exception const&);
106 ~__libcpp_debug_exception() _NOEXCEPT;
107 const char* what() const _NOEXCEPT;
108private:
109 struct __libcpp_debug_exception_imp;
110 __libcpp_debug_exception_imp *__imp_;
111};
112#endif
113
114#if _LIBCPP_DEBUG_LEVEL >= 2 || defined(_LIBCPP_BUILDING_LIBRARY)
115
Howard Hinnant8331b762013-03-06 23:30:19 +0000116struct _LIBCPP_TYPE_VIS __c_node;
Howard Hinnant27e0e772011-09-14 18:33:51 +0000117
Howard Hinnant8331b762013-03-06 23:30:19 +0000118struct _LIBCPP_TYPE_VIS __i_node
Howard Hinnant27e0e772011-09-14 18:33:51 +0000119{
120 void* __i_;
121 __i_node* __next_;
122 __c_node* __c_;
123
Eric Fiselier2d8515f2017-01-06 20:58:25 +0000124#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant27e0e772011-09-14 18:33:51 +0000125 __i_node(const __i_node&) = delete;
126 __i_node& operator=(const __i_node&) = delete;
Howard Hinnant8ea98242013-08-23 17:37:05 +0000127#else
128private:
129 __i_node(const __i_node&);
130 __i_node& operator=(const __i_node&);
131public:
132#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000133 _LIBCPP_INLINE_VISIBILITY
134 __i_node(void* __i, __i_node* __next, __c_node* __c)
135 : __i_(__i), __next_(__next), __c_(__c) {}
136 ~__i_node();
137};
138
Howard Hinnant8331b762013-03-06 23:30:19 +0000139struct _LIBCPP_TYPE_VIS __c_node
Howard Hinnant27e0e772011-09-14 18:33:51 +0000140{
141 void* __c_;
142 __c_node* __next_;
143 __i_node** beg_;
144 __i_node** end_;
145 __i_node** cap_;
146
Eric Fiselier2d8515f2017-01-06 20:58:25 +0000147#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant27e0e772011-09-14 18:33:51 +0000148 __c_node(const __c_node&) = delete;
149 __c_node& operator=(const __c_node&) = delete;
Howard Hinnant8ea98242013-08-23 17:37:05 +0000150#else
151private:
152 __c_node(const __c_node&);
153 __c_node& operator=(const __c_node&);
154public:
155#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000156 _LIBCPP_INLINE_VISIBILITY
157 __c_node(void* __c, __c_node* __next)
158 : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {}
159 virtual ~__c_node();
160
161 virtual bool __dereferenceable(const void*) const = 0;
162 virtual bool __decrementable(const void*) const = 0;
163 virtual bool __addable(const void*, ptrdiff_t) const = 0;
164 virtual bool __subscriptable(const void*, ptrdiff_t) const = 0;
165
Howard Hinnantb399c602011-09-27 23:55:03 +0000166 void __add(__i_node* __i);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000167 _LIBCPP_HIDDEN void __remove(__i_node* __i);
168};
169
170template <class _Cont>
171struct _C_node
172 : public __c_node
173{
174 _C_node(void* __c, __c_node* __n)
175 : __c_node(__c, __n) {}
176
177 virtual bool __dereferenceable(const void*) const;
178 virtual bool __decrementable(const void*) const;
179 virtual bool __addable(const void*, ptrdiff_t) const;
180 virtual bool __subscriptable(const void*, ptrdiff_t) const;
181};
182
183template <class _Cont>
Eric Fiselierfb825432016-12-28 04:58:52 +0000184inline bool
Howard Hinnant27e0e772011-09-14 18:33:51 +0000185_C_node<_Cont>::__dereferenceable(const void* __i) const
186{
187 typedef typename _Cont::const_iterator iterator;
188 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnantc834c512011-11-29 18:15:50 +0000189 _Cont* _Cp = static_cast<_Cont*>(__c_);
190 return _Cp->__dereferenceable(__j);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000191}
192
193template <class _Cont>
Eric Fiselierfb825432016-12-28 04:58:52 +0000194inline bool
Howard Hinnant27e0e772011-09-14 18:33:51 +0000195_C_node<_Cont>::__decrementable(const void* __i) const
196{
197 typedef typename _Cont::const_iterator iterator;
198 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnantc834c512011-11-29 18:15:50 +0000199 _Cont* _Cp = static_cast<_Cont*>(__c_);
200 return _Cp->__decrementable(__j);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000201}
202
203template <class _Cont>
Eric Fiselierfb825432016-12-28 04:58:52 +0000204inline bool
Howard Hinnant27e0e772011-09-14 18:33:51 +0000205_C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const
206{
207 typedef typename _Cont::const_iterator iterator;
208 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnantc834c512011-11-29 18:15:50 +0000209 _Cont* _Cp = static_cast<_Cont*>(__c_);
210 return _Cp->__addable(__j, __n);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000211}
212
213template <class _Cont>
Eric Fiselierfb825432016-12-28 04:58:52 +0000214inline bool
Howard Hinnant27e0e772011-09-14 18:33:51 +0000215_C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const
216{
217 typedef typename _Cont::const_iterator iterator;
218 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnantc834c512011-11-29 18:15:50 +0000219 _Cont* _Cp = static_cast<_Cont*>(__c_);
220 return _Cp->__subscriptable(__j, __n);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000221}
222
Howard Hinnant8331b762013-03-06 23:30:19 +0000223class _LIBCPP_TYPE_VIS __libcpp_db
Howard Hinnant27e0e772011-09-14 18:33:51 +0000224{
225 __c_node** __cbeg_;
226 __c_node** __cend_;
227 size_t __csz_;
228 __i_node** __ibeg_;
229 __i_node** __iend_;
230 size_t __isz_;
231
232 __libcpp_db();
233public:
Eric Fiselier2d8515f2017-01-06 20:58:25 +0000234#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant27e0e772011-09-14 18:33:51 +0000235 __libcpp_db(const __libcpp_db&) = delete;
236 __libcpp_db& operator=(const __libcpp_db&) = delete;
Howard Hinnant8ea98242013-08-23 17:37:05 +0000237#else
238private:
239 __libcpp_db(const __libcpp_db&);
240 __libcpp_db& operator=(const __libcpp_db&);
241public:
242#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000243 ~__libcpp_db();
244
245 class __db_c_iterator;
246 class __db_c_const_iterator;
247 class __db_i_iterator;
248 class __db_i_const_iterator;
249
250 __db_c_const_iterator __c_end() const;
251 __db_i_const_iterator __i_end() const;
252
253 template <class _Cont>
254 _LIBCPP_INLINE_VISIBILITY
255 void __insert_c(_Cont* __c)
256 {
257 __c_node* __n = __insert_c(static_cast<void*>(__c));
258 ::new(__n) _C_node<_Cont>(__n->__c_, __n->__next_);
259 }
260
Howard Hinnantc90aa632011-09-16 19:52:23 +0000261 void __insert_i(void* __i);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000262 __c_node* __insert_c(void* __c);
263 void __erase_c(void* __c);
264
265 void __insert_ic(void* __i, const void* __c);
266 void __iterator_copy(void* __i, const void* __i0);
267 void __erase_i(void* __i);
268
269 void* __find_c_from_i(void* __i) const;
270 void __invalidate_all(void* __c);
271 __c_node* __find_c_and_lock(void* __c) const;
Howard Hinnantb399c602011-09-27 23:55:03 +0000272 __c_node* __find_c(void* __c) const;
Howard Hinnant27e0e772011-09-14 18:33:51 +0000273 void unlock() const;
274
275 void swap(void* __c1, void* __c2);
276
277
278 bool __dereferenceable(const void* __i) const;
279 bool __decrementable(const void* __i) const;
280 bool __addable(const void* __i, ptrdiff_t __n) const;
281 bool __subscriptable(const void* __i, ptrdiff_t __n) const;
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000282 bool __less_than_comparable(const void* __i, const void* __j) const;
Howard Hinnant27e0e772011-09-14 18:33:51 +0000283private:
284 _LIBCPP_HIDDEN
285 __i_node* __insert_iterator(void* __i);
286 _LIBCPP_HIDDEN
287 __i_node* __find_iterator(const void* __i) const;
288
Howard Hinnant8331b762013-03-06 23:30:19 +0000289 friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000290};
291
Howard Hinnant8331b762013-03-06 23:30:19 +0000292_LIBCPP_FUNC_VIS __libcpp_db* __get_db();
293_LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000294
295
Eric Fiselierfb825432016-12-28 04:58:52 +0000296#endif // _LIBCPP_DEBUG_LEVEL >= 2 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000297
Eric Fiselierfb825432016-12-28 04:58:52 +0000298_LIBCPP_END_NAMESPACE_STD
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000299
Howard Hinnant27e0e772011-09-14 18:33:51 +0000300#endif // _LIBCPP_DEBUG_H
301