blob: 43554dee5c9edcce2343899d55924d49eeb4a2e6 [file] [log] [blame]
Howard Hinnant27e0e772011-09-14 18:33:51 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnant27e0e772011-09-14 18:33:51 +00003//
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>
Eric Fiselier873b8d32019-03-18 21:50:12 +000014#include <iosfwd>
Nikolas Klauserf2807732022-01-11 00:33:35 +010015#include <type_traits>
Eric Fiselier14b6de92014-08-10 23:53:08 +000016
Howard Hinnant8ea98242013-08-23 17:37:05 +000017#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -050018# pragma GCC system_header
Howard Hinnant8ea98242013-08-23 17:37:05 +000019#endif
20
Eric Fiselierfb825432016-12-28 04:58:52 +000021#if _LIBCPP_DEBUG_LEVEL >= 1 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant27e0e772011-09-14 18:33:51 +000022# include <cstddef>
Arthur O'Dwyerd180ef82022-01-07 09:45:05 -050023# include <cstdio>
24# include <cstdlib>
Eric Fiselierfb825432016-12-28 04:58:52 +000025#endif
26
Louis Dionneba400782020-10-02 15:02:52 -040027#if _LIBCPP_DEBUG_LEVEL == 0
Eric Fiselierd8f24a42016-07-23 20:36:55 +000028# define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0)
Louis Dionneba400782020-10-02 15:02:52 -040029# define _LIBCPP_ASSERT_IMPL(x, m) ((void)0)
30#elif _LIBCPP_DEBUG_LEVEL == 1
31# define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0)
Louis Dionneba400782020-10-02 15:02:52 -040032# define _LIBCPP_ASSERT_IMPL(x, m) ((x) ? (void)0 : _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m)))
33#elif _LIBCPP_DEBUG_LEVEL == 2
Nikolas Klauser1a7d9f02021-12-16 14:55:03 +010034# define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(__libcpp_is_constant_evaluated() || (x), m)
Louis Dionneba400782020-10-02 15:02:52 -040035# define _LIBCPP_ASSERT_IMPL(x, m) ((x) ? (void)0 : _VSTD::__libcpp_debug_function(_VSTD::__libcpp_debug_info(__FILE__, __LINE__, #x, m)))
36#else
37# error _LIBCPP_DEBUG_LEVEL must be one of 0, 1, 2
Eric Fiselierd8f24a42016-07-23 20:36:55 +000038#endif
Louis Dionneba400782020-10-02 15:02:52 -040039
40#if !defined(_LIBCPP_ASSERT)
41# define _LIBCPP_ASSERT(x, m) _LIBCPP_ASSERT_IMPL(x, m)
Eric Fiselierd8f24a42016-07-23 20:36:55 +000042#endif
Howard Hinnanta47c6d52011-09-16 17:29:17 +000043
Howard Hinnant27e0e772011-09-14 18:33:51 +000044_LIBCPP_BEGIN_NAMESPACE_STD
45
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +000046struct _LIBCPP_TEMPLATE_VIS __libcpp_debug_info {
Eric Fiselier3253fff2016-12-28 05:26:56 +000047 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier30ee6932016-12-28 05:56:16 +000048 __libcpp_debug_info()
49 : __file_(nullptr), __line_(-1), __pred_(nullptr), __msg_(nullptr) {}
50 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier3253fff2016-12-28 05:26:56 +000051 __libcpp_debug_info(const char* __f, int __l, const char* __p, const char* __m)
52 : __file_(__f), __line_(__l), __pred_(__p), __msg_(__m) {}
Eric Fiselier873b8d32019-03-18 21:50:12 +000053
Arthur O'Dwyer07b22492020-11-27 11:02:06 -050054 _LIBCPP_FUNC_VIS string what() const;
Eric Fiselier873b8d32019-03-18 21:50:12 +000055
Eric Fiselierfb825432016-12-28 04:58:52 +000056 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.
Louis Dionne5254b372018-10-25 12:13:43 +000067extern _LIBCPP_EXPORTED_FROM_ABI __libcpp_debug_function_type __libcpp_debug_function;
Eric Fiselierfb825432016-12-28 04:58:52 +000068
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
Eric Fiselierfb825432016-12-28 04:58:52 +000073/// __libcpp_set_debug_function - Set the debug handler to the specified
74/// function.
75_LIBCPP_FUNC_VIS
76bool __libcpp_set_debug_function(__libcpp_debug_function_type __func);
77
Louis Dionneba400782020-10-02 15:02:52 -040078#if _LIBCPP_DEBUG_LEVEL == 2 || defined(_LIBCPP_BUILDING_LIBRARY)
Eric Fiselierfb825432016-12-28 04:58:52 +000079
Howard Hinnant8331b762013-03-06 23:30:19 +000080struct _LIBCPP_TYPE_VIS __c_node;
Howard Hinnant27e0e772011-09-14 18:33:51 +000081
Howard Hinnant8331b762013-03-06 23:30:19 +000082struct _LIBCPP_TYPE_VIS __i_node
Howard Hinnant27e0e772011-09-14 18:33:51 +000083{
84 void* __i_;
85 __i_node* __next_;
86 __c_node* __c_;
87
Eric Fiselier2d8515f2017-01-06 20:58:25 +000088#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant27e0e772011-09-14 18:33:51 +000089 __i_node(const __i_node&) = delete;
90 __i_node& operator=(const __i_node&) = delete;
Howard Hinnant8ea98242013-08-23 17:37:05 +000091#else
92private:
93 __i_node(const __i_node&);
94 __i_node& operator=(const __i_node&);
95public:
96#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +000097 _LIBCPP_INLINE_VISIBILITY
98 __i_node(void* __i, __i_node* __next, __c_node* __c)
99 : __i_(__i), __next_(__next), __c_(__c) {}
100 ~__i_node();
101};
102
Howard Hinnant8331b762013-03-06 23:30:19 +0000103struct _LIBCPP_TYPE_VIS __c_node
Howard Hinnant27e0e772011-09-14 18:33:51 +0000104{
105 void* __c_;
106 __c_node* __next_;
107 __i_node** beg_;
108 __i_node** end_;
109 __i_node** cap_;
110
Eric Fiselier2d8515f2017-01-06 20:58:25 +0000111#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant27e0e772011-09-14 18:33:51 +0000112 __c_node(const __c_node&) = delete;
113 __c_node& operator=(const __c_node&) = delete;
Howard Hinnant8ea98242013-08-23 17:37:05 +0000114#else
115private:
116 __c_node(const __c_node&);
117 __c_node& operator=(const __c_node&);
118public:
119#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000120 _LIBCPP_INLINE_VISIBILITY
121 __c_node(void* __c, __c_node* __next)
122 : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {}
123 virtual ~__c_node();
124
125 virtual bool __dereferenceable(const void*) const = 0;
126 virtual bool __decrementable(const void*) const = 0;
127 virtual bool __addable(const void*, ptrdiff_t) const = 0;
128 virtual bool __subscriptable(const void*, ptrdiff_t) const = 0;
129
Howard Hinnantb399c602011-09-27 23:55:03 +0000130 void __add(__i_node* __i);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000131 _LIBCPP_HIDDEN void __remove(__i_node* __i);
132};
133
134template <class _Cont>
135struct _C_node
136 : public __c_node
137{
138 _C_node(void* __c, __c_node* __n)
139 : __c_node(__c, __n) {}
140
141 virtual bool __dereferenceable(const void*) const;
142 virtual bool __decrementable(const void*) const;
143 virtual bool __addable(const void*, ptrdiff_t) const;
144 virtual bool __subscriptable(const void*, ptrdiff_t) const;
145};
146
147template <class _Cont>
Eric Fiselierfb825432016-12-28 04:58:52 +0000148inline bool
Howard Hinnant27e0e772011-09-14 18:33:51 +0000149_C_node<_Cont>::__dereferenceable(const void* __i) const
150{
151 typedef typename _Cont::const_iterator iterator;
152 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnantc834c512011-11-29 18:15:50 +0000153 _Cont* _Cp = static_cast<_Cont*>(__c_);
154 return _Cp->__dereferenceable(__j);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000155}
156
157template <class _Cont>
Eric Fiselierfb825432016-12-28 04:58:52 +0000158inline bool
Howard Hinnant27e0e772011-09-14 18:33:51 +0000159_C_node<_Cont>::__decrementable(const void* __i) const
160{
161 typedef typename _Cont::const_iterator iterator;
162 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnantc834c512011-11-29 18:15:50 +0000163 _Cont* _Cp = static_cast<_Cont*>(__c_);
164 return _Cp->__decrementable(__j);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000165}
166
167template <class _Cont>
Eric Fiselierfb825432016-12-28 04:58:52 +0000168inline bool
Howard Hinnant27e0e772011-09-14 18:33:51 +0000169_C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const
170{
171 typedef typename _Cont::const_iterator iterator;
172 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnantc834c512011-11-29 18:15:50 +0000173 _Cont* _Cp = static_cast<_Cont*>(__c_);
174 return _Cp->__addable(__j, __n);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000175}
176
177template <class _Cont>
Eric Fiselierfb825432016-12-28 04:58:52 +0000178inline bool
Howard Hinnant27e0e772011-09-14 18:33:51 +0000179_C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const
180{
181 typedef typename _Cont::const_iterator iterator;
182 const iterator* __j = static_cast<const iterator*>(__i);
Howard Hinnantc834c512011-11-29 18:15:50 +0000183 _Cont* _Cp = static_cast<_Cont*>(__c_);
184 return _Cp->__subscriptable(__j, __n);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000185}
186
Howard Hinnant8331b762013-03-06 23:30:19 +0000187class _LIBCPP_TYPE_VIS __libcpp_db
Howard Hinnant27e0e772011-09-14 18:33:51 +0000188{
189 __c_node** __cbeg_;
190 __c_node** __cend_;
191 size_t __csz_;
192 __i_node** __ibeg_;
193 __i_node** __iend_;
194 size_t __isz_;
195
196 __libcpp_db();
197public:
Eric Fiselier2d8515f2017-01-06 20:58:25 +0000198#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant27e0e772011-09-14 18:33:51 +0000199 __libcpp_db(const __libcpp_db&) = delete;
200 __libcpp_db& operator=(const __libcpp_db&) = delete;
Howard Hinnant8ea98242013-08-23 17:37:05 +0000201#else
202private:
203 __libcpp_db(const __libcpp_db&);
204 __libcpp_db& operator=(const __libcpp_db&);
205public:
206#endif
Howard Hinnant27e0e772011-09-14 18:33:51 +0000207 ~__libcpp_db();
208
209 class __db_c_iterator;
210 class __db_c_const_iterator;
211 class __db_i_iterator;
212 class __db_i_const_iterator;
213
214 __db_c_const_iterator __c_end() const;
215 __db_i_const_iterator __i_end() const;
216
Eric Fiselieraed4eab2019-03-05 02:10:31 +0000217 typedef __c_node*(_InsertConstruct)(void*, void*, __c_node*);
218
219 template <class _Cont>
220 _LIBCPP_INLINE_VISIBILITY static __c_node* __create_C_node(void *__mem, void *__c, __c_node *__next) {
Arthur O'Dwyer0c4472a2020-12-11 20:30:28 -0500221 return ::new (__mem) _C_node<_Cont>(__c, __next);
Eric Fiselieraed4eab2019-03-05 02:10:31 +0000222 }
223
Howard Hinnant27e0e772011-09-14 18:33:51 +0000224 template <class _Cont>
225 _LIBCPP_INLINE_VISIBILITY
226 void __insert_c(_Cont* __c)
227 {
Eric Fiselieraed4eab2019-03-05 02:10:31 +0000228 __insert_c(static_cast<void*>(__c), &__create_C_node<_Cont>);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000229 }
230
Howard Hinnantc90aa632011-09-16 19:52:23 +0000231 void __insert_i(void* __i);
Eric Fiselieraed4eab2019-03-05 02:10:31 +0000232 void __insert_c(void* __c, _InsertConstruct* __fn);
Howard Hinnant27e0e772011-09-14 18:33:51 +0000233 void __erase_c(void* __c);
234
235 void __insert_ic(void* __i, const void* __c);
236 void __iterator_copy(void* __i, const void* __i0);
237 void __erase_i(void* __i);
238
239 void* __find_c_from_i(void* __i) const;
240 void __invalidate_all(void* __c);
241 __c_node* __find_c_and_lock(void* __c) const;
Howard Hinnantb399c602011-09-27 23:55:03 +0000242 __c_node* __find_c(void* __c) const;
Howard Hinnant27e0e772011-09-14 18:33:51 +0000243 void unlock() const;
244
245 void swap(void* __c1, void* __c2);
246
247
248 bool __dereferenceable(const void* __i) const;
249 bool __decrementable(const void* __i) const;
250 bool __addable(const void* __i, ptrdiff_t __n) const;
251 bool __subscriptable(const void* __i, ptrdiff_t __n) const;
Howard Hinnante6ff0b62013-08-02 00:26:35 +0000252 bool __less_than_comparable(const void* __i, const void* __j) const;
Howard Hinnant27e0e772011-09-14 18:33:51 +0000253private:
254 _LIBCPP_HIDDEN
255 __i_node* __insert_iterator(void* __i);
256 _LIBCPP_HIDDEN
257 __i_node* __find_iterator(const void* __i) const;
258
Howard Hinnant8331b762013-03-06 23:30:19 +0000259 friend _LIBCPP_FUNC_VIS __libcpp_db* __get_db();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000260};
261
Howard Hinnant8331b762013-03-06 23:30:19 +0000262_LIBCPP_FUNC_VIS __libcpp_db* __get_db();
263_LIBCPP_FUNC_VIS const __libcpp_db* __get_const_db();
Howard Hinnant27e0e772011-09-14 18:33:51 +0000264
265
Louis Dionneba400782020-10-02 15:02:52 -0400266#endif // _LIBCPP_DEBUG_LEVEL == 2 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnant27e0e772011-09-14 18:33:51 +0000267
Nikolas Klauserf2807732022-01-11 00:33:35 +0100268template <class _Tp>
269_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 inline void __debug_db_insert_c(_Tp* __c) {
270#if _LIBCPP_DEBUG_LEVEL == 2
271 if (!__libcpp_is_constant_evaluated())
272 __get_db()->__insert_c(__c);
273#else
274 (void)(__c);
275#endif
276}
277
Nikolas Klauserfb7b4952022-01-17 19:25:21 +0100278template <class _Tp>
279_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11 inline void __debug_db_insert_i(_Tp* __i) {
280#if _LIBCPP_DEBUG_LEVEL == 2
281 if (!__libcpp_is_constant_evaluated())
282 __get_db()->__insert_i(__i);
283#else
284 (void)(__i);
285#endif
286}
287
Eric Fiselierfb825432016-12-28 04:58:52 +0000288_LIBCPP_END_NAMESPACE_STD
Howard Hinnanta47c6d52011-09-16 17:29:17 +0000289
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400290#endif // _LIBCPP_DEBUG_H