1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// 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 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP___DEBUG 11#define _LIBCPP___DEBUG 12 13#include <__assert> 14#include <__config> 15#include <__type_traits/is_constant_evaluated.h> 16#include <cstddef> 17 18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19# pragma GCC system_header 20#endif 21 22#if defined(_LIBCPP_ENABLE_DEBUG_MODE) && !defined(_LIBCPP_CXX03_LANG) && !defined(_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY) 23# define _LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY 24#endif 25 26#if defined(_LIBCPP_ENABLE_DEBUG_MODE) && !defined(_LIBCPP_DEBUG_STRICT_WEAK_ORDERING_CHECK) 27# define _LIBCPP_DEBUG_STRICT_WEAK_ORDERING_CHECK 28#endif 29 30#if defined(_LIBCPP_ENABLE_DEBUG_MODE) && !defined(_LIBCPP_ABI_BOUNDED_ITERATORS) 31# define _LIBCPP_ABI_BOUNDED_ITERATORS 32#endif 33 34#ifdef _LIBCPP_ENABLE_DEBUG_MODE 35# define _LIBCPP_DEBUG_ASSERT(x, m) _LIBCPP_ASSERT(::std::__libcpp_is_constant_evaluated() || (x), m) 36#else 37# define _LIBCPP_DEBUG_ASSERT(x, m) ((void)0) 38#endif 39 40#if defined(_LIBCPP_ENABLE_DEBUG_MODE) || defined(_LIBCPP_BUILDING_LIBRARY) 41 42_LIBCPP_BEGIN_NAMESPACE_STD 43 44struct _LIBCPP_EXPORTED_FROM_ABI __c_node; 45 46struct _LIBCPP_EXPORTED_FROM_ABI __i_node 47{ 48 void* __i_; 49 __i_node* __next_; 50 __c_node* __c_; 51 52 __i_node(const __i_node&) = delete; 53 __i_node& operator=(const __i_node&) = delete; 54 55 _LIBCPP_INLINE_VISIBILITY 56 __i_node(void* __i, __i_node* __next, __c_node* __c) 57 : __i_(__i), __next_(__next), __c_(__c) {} 58 ~__i_node(); 59}; 60 61struct _LIBCPP_EXPORTED_FROM_ABI __c_node 62{ 63 void* __c_; 64 __c_node* __next_; 65 __i_node** beg_; 66 __i_node** end_; 67 __i_node** cap_; 68 69 __c_node(const __c_node&) = delete; 70 __c_node& operator=(const __c_node&) = delete; 71 72 _LIBCPP_INLINE_VISIBILITY 73 explicit __c_node(void* __c, __c_node* __next) 74 : __c_(__c), __next_(__next), beg_(nullptr), end_(nullptr), cap_(nullptr) {} 75 virtual ~__c_node(); 76 77 virtual bool __dereferenceable(const void*) const = 0; 78 virtual bool __decrementable(const void*) const = 0; 79 virtual bool __addable(const void*, ptrdiff_t) const = 0; 80 virtual bool __subscriptable(const void*, ptrdiff_t) const = 0; 81 82 void __add(__i_node* __i); 83 _LIBCPP_HIDDEN void __remove(__i_node* __i); 84}; 85 86template <class _Cont> 87struct _C_node 88 : public __c_node 89{ 90 _LIBCPP_HIDE_FROM_ABI explicit _C_node(void* __c, __c_node* __n) 91 : __c_node(__c, __n) {} 92 93 _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool __dereferenceable(const void*) const override; 94 _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool __decrementable(const void*) const override; 95 _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool __addable(const void*, ptrdiff_t) const override; 96 _LIBCPP_HIDE_FROM_ABI_VIRTUAL bool __subscriptable(const void*, ptrdiff_t) const override; 97}; 98 99template <class _Cont> 100inline bool 101_C_node<_Cont>::__dereferenceable(const void* __i) const 102{ 103 typedef typename _Cont::const_iterator iterator; 104 const iterator* __j = static_cast<const iterator*>(__i); 105 _Cont* __cp = static_cast<_Cont*>(__c_); 106 return __cp->__dereferenceable(__j); 107} 108 109template <class _Cont> 110inline bool 111_C_node<_Cont>::__decrementable(const void* __i) const 112{ 113 typedef typename _Cont::const_iterator iterator; 114 const iterator* __j = static_cast<const iterator*>(__i); 115 _Cont* __cp = static_cast<_Cont*>(__c_); 116 return __cp->__decrementable(__j); 117} 118 119template <class _Cont> 120inline bool 121_C_node<_Cont>::__addable(const void* __i, ptrdiff_t __n) const 122{ 123 typedef typename _Cont::const_iterator iterator; 124 const iterator* __j = static_cast<const iterator*>(__i); 125 _Cont* __cp = static_cast<_Cont*>(__c_); 126 return __cp->__addable(__j, __n); 127} 128 129template <class _Cont> 130inline bool 131_C_node<_Cont>::__subscriptable(const void* __i, ptrdiff_t __n) const 132{ 133 typedef typename _Cont::const_iterator iterator; 134 const iterator* __j = static_cast<const iterator*>(__i); 135 _Cont* __cp = static_cast<_Cont*>(__c_); 136 return __cp->__subscriptable(__j, __n); 137} 138 139class _LIBCPP_EXPORTED_FROM_ABI __libcpp_db 140{ 141 __c_node** __cbeg_; 142 __c_node** __cend_; 143 size_t __csz_; 144 __i_node** __ibeg_; 145 __i_node** __iend_; 146 size_t __isz_; 147 148 explicit __libcpp_db(); 149public: 150 __libcpp_db(const __libcpp_db&) = delete; 151 __libcpp_db& operator=(const __libcpp_db&) = delete; 152 153 ~__libcpp_db(); 154 155 class __db_c_iterator; 156 class __db_c_const_iterator; 157 class __db_i_iterator; 158 class __db_i_const_iterator; 159 160 __db_c_const_iterator __c_end() const; 161 __db_i_const_iterator __i_end() const; 162 163 typedef __c_node*(_InsertConstruct)(void*, void*, __c_node*); 164 165 template <class _Cont> 166 _LIBCPP_INLINE_VISIBILITY static __c_node* __create_C_node(void *__mem, void *__c, __c_node *__next) { 167 return ::new (__mem) _C_node<_Cont>(__c, __next); 168 } 169 170 template <class _Cont> 171 _LIBCPP_INLINE_VISIBILITY 172 void __insert_c(_Cont* __c) 173 { 174 __insert_c(static_cast<void*>(__c), &__create_C_node<_Cont>); 175 } 176 177 void __insert_i(void* __i); 178 void __insert_c(void* __c, _InsertConstruct* __fn); 179 void __erase_c(void* __c); 180 181 void __insert_ic(void* __i, const void* __c); 182 void __iterator_copy(void* __i, const void* __i0); 183 void __erase_i(void* __i); 184 185 void* __find_c_from_i(void* __i) const; 186 void __invalidate_all(void* __c); 187 __c_node* __find_c_and_lock(void* __c) const; 188 __c_node* __find_c(void* __c) const; 189 void unlock() const; 190 191 void swap(void* __c1, void* __c2); 192 193 194 bool __dereferenceable(const void* __i) const; 195 bool __decrementable(const void* __i) const; 196 bool __addable(const void* __i, ptrdiff_t __n) const; 197 bool __subscriptable(const void* __i, ptrdiff_t __n) const; 198 bool __less_than_comparable(const void* __i, const void* __j) const; 199private: 200 _LIBCPP_HIDDEN 201 __i_node* __insert_iterator(void* __i); 202 _LIBCPP_HIDDEN 203 __i_node* __find_iterator(const void* __i) const; 204 205 friend _LIBCPP_EXPORTED_FROM_ABI __libcpp_db* __get_db(); 206}; 207 208_LIBCPP_EXPORTED_FROM_ABI __libcpp_db* __get_db(); 209_LIBCPP_EXPORTED_FROM_ABI const __libcpp_db* __get_const_db(); 210 211_LIBCPP_END_NAMESPACE_STD 212 213#endif // defined(_LIBCPP_ENABLE_DEBUG_MODE) || defined(_LIBCPP_BUILDING_LIBRARY) 214 215_LIBCPP_BEGIN_NAMESPACE_STD 216 217template <class _Tp> 218_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 inline void __debug_db_insert_c(_Tp* __c) { 219#ifdef _LIBCPP_ENABLE_DEBUG_MODE 220 if (!__libcpp_is_constant_evaluated()) 221 __get_db()->__insert_c(__c); 222#else 223 (void)(__c); 224#endif 225} 226 227template <class _Tp> 228_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 inline void __debug_db_insert_i(_Tp* __i) { 229#ifdef _LIBCPP_ENABLE_DEBUG_MODE 230 if (!__libcpp_is_constant_evaluated()) 231 __get_db()->__insert_i(__i); 232#else 233 (void)(__i); 234#endif 235} 236 237template <class _Tp> 238_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 inline void __debug_db_erase_c(_Tp* __c) { 239#ifdef _LIBCPP_ENABLE_DEBUG_MODE 240 if (!__libcpp_is_constant_evaluated()) 241 __get_db()->__erase_c(__c); 242#else 243 (void)(__c); 244#endif 245} 246 247template <class _Tp> 248_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 inline void __debug_db_swap(_Tp* __lhs, _Tp* __rhs) { 249#ifdef _LIBCPP_ENABLE_DEBUG_MODE 250 if (!__libcpp_is_constant_evaluated()) 251 __get_db()->swap(__lhs, __rhs); 252#else 253 (void)(__lhs); 254 (void)(__rhs); 255#endif 256} 257 258template <class _Tp> 259_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 inline void __debug_db_invalidate_all(_Tp* __c) { 260#ifdef _LIBCPP_ENABLE_DEBUG_MODE 261 if (!__libcpp_is_constant_evaluated()) 262 __get_db()->__invalidate_all(__c); 263#else 264 (void)(__c); 265#endif 266} 267 268_LIBCPP_END_NAMESPACE_STD 269 270#endif // _LIBCPP___DEBUG 271