1 //===-------------------------- debug.cpp ---------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define _LIBCPP_DEBUG2 1
15 #include "__hash_table"
18 _LIBCPP_BEGIN_NAMESPACE_STD
24 static __libcpp_db db;
38 typedef mutex mutex_type;
39 typedef lock_guard<mutex_type> WLock;
40 typedef lock_guard<mutex_type> RLock;
49 } // unnamed namespace
70 __libcpp_db::__libcpp_db()
80 __libcpp_db::~__libcpp_db()
84 for (__c_node** p = __cbeg_; p != __cend_; ++p)
96 for (__i_node** p = __ibeg_; p != __iend_; ++p)
109 __libcpp_db::__find_c_from_i(void* __i) const
112 __i_node* i = __find_iterator(__i);
113 _LIBCPP_ASSERT(i != nullptr, "iterator not found in debug database.");
114 return i->__c_ != nullptr ? i->__c_->__c_ : nullptr;
118 __libcpp_db::__insert_ic(void* __i, const void* __c)
121 __i_node* i = __insert_iterator(__i);
123 "Container constructed in a translation unit with debug mode disabled."
124 " But it is being used in a translation unit with debug mode enabled."
125 " Enable it in the other translation unit with #define _LIBCPP_DEBUG2 1";
126 _LIBCPP_ASSERT(__cbeg_ != __cend_, errmsg);
127 size_t hc = hash<const void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
128 __c_node* c = __cbeg_[hc];
129 _LIBCPP_ASSERT(c != nullptr, errmsg);
130 while (c->__c_ != __c)
133 _LIBCPP_ASSERT(c != nullptr, errmsg);
140 __libcpp_db::__insert_c(void* __c)
143 if (__csz_ + 1 > static_cast<size_t>(__cend_ - __cbeg_))
145 size_t nc = __next_prime(2*static_cast<size_t>(__cend_ - __cbeg_) + 1);
146 __c_node** cbeg = (__c_node**)calloc(nc, sizeof(void*));
148 #ifndef _LIBCPP_NO_EXCEPTIONS
153 for (__c_node** p = __cbeg_; p != __cend_; ++p)
158 size_t h = hash<void*>()(q->__c_) % nc;
159 __c_node* r = q->__next_;
160 q->__next_ = cbeg[h];
167 __cend_ = __cbeg_ + nc;
169 size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
170 __c_node* p = __cbeg_[hc];
171 __c_node* r = __cbeg_[hc] = (__c_node*)malloc(sizeof(__c_node));
172 if (__cbeg_[hc] == nullptr)
173 #ifndef _LIBCPP_NO_EXCEPTIONS
185 __libcpp_db::__erase_i(void* __i)
188 if (__ibeg_ != __iend_)
190 size_t hi = hash<void*>()(__i) % static_cast<size_t>(__iend_ - __ibeg_);
191 __i_node* p = __ibeg_[hi];
194 __i_node* q = nullptr;
195 while (p->__i_ != __i)
203 __ibeg_[hi] = p->__next_;
205 q->__next_ = p->__next_;
206 __c_node* c = p->__c_;
216 __libcpp_db::__invalidate_all(void* __c)
219 size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
220 __c_node* p = __cbeg_[hc];
221 _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __invalidate_all A");
222 while (p->__c_ != __c)
225 _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __invalidate_all B");
227 while (p->end_ != p->beg_)
230 (*p->end_)->__c_ = nullptr;
235 __libcpp_db::__find_c_and_lock(void* __c) const
238 size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
239 __c_node* p = __cbeg_[hc];
240 _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __find_c_and_lock A");
241 while (p->__c_ != __c)
244 _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __find_c_and_lock B");
250 __libcpp_db::__find_c(void* __c) const
252 size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
253 __c_node* p = __cbeg_[hc];
254 _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __find_c A");
255 while (p->__c_ != __c)
258 _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __find_c B");
264 __libcpp_db::unlock() const
270 __libcpp_db::__erase_c(void* __c)
273 size_t hc = hash<void*>()(__c) % static_cast<size_t>(__cend_ - __cbeg_);
274 __c_node* p = __cbeg_[hc];
275 __c_node* q = nullptr;
276 _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __erase_c A");
277 while (p->__c_ != __c)
281 _LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __erase_c B");
284 __cbeg_[hc] = p->__next_;
286 q->__next_ = p->__next_;
287 while (p->end_ != p->beg_)
290 (*p->end_)->__c_ = nullptr;
298 __libcpp_db::__iterator_copy(void* __i, const void* __i0)
301 __i_node* i = __find_iterator(__i);
302 __i_node* i0 = __find_iterator(__i0);
303 __c_node* c0 = i0 != nullptr ? i0->__c_ : nullptr;
304 if (i == nullptr && i0 != nullptr)
305 i = __insert_iterator(__i);
306 __c_node* c = i != nullptr ? i->__c_ : nullptr;
324 __libcpp_db::__dereferenceable(const void* __i) const
327 __i_node* i = __find_iterator(__i);
328 return i != nullptr && i->__c_ != nullptr && i->__c_->__dereferenceable(__i);
332 __libcpp_db::__decrementable(const void* __i) const
335 __i_node* i = __find_iterator(__i);
336 return i != nullptr && i->__c_ != nullptr && i->__c_->__decrementable(__i);
340 __libcpp_db::__addable(const void* __i, ptrdiff_t __n) const
343 __i_node* i = __find_iterator(__i);
344 return i != nullptr && i->__c_ != nullptr && i->__c_->__addable(__i, __n);
348 __libcpp_db::__subscriptable(const void* __i, ptrdiff_t __n) const
351 __i_node* i = __find_iterator(__i);
352 return i != nullptr && i->__c_ != nullptr && i->__c_->__subscriptable(__i, __n);
356 __libcpp_db::__comparable(const void* __i, const void* __j) const
359 __i_node* i = __find_iterator(__i);
360 __i_node* j = __find_iterator(__j);
361 __c_node* ci = i != nullptr ? i->__c_ : nullptr;
362 __c_node* cj = j != nullptr ? j->__c_ : nullptr;
363 return ci != nullptr && ci == cj;
367 __libcpp_db::swap(void* c1, void* c2)
370 size_t hc = hash<void*>()(c1) % static_cast<size_t>(__cend_ - __cbeg_);
371 __c_node* p1 = __cbeg_[hc];
372 _LIBCPP_ASSERT(p1 != nullptr, "debug mode internal logic error swap A");
373 while (p1->__c_ != c1)
376 _LIBCPP_ASSERT(p1 != nullptr, "debug mode internal logic error swap B");
378 hc = hash<void*>()(c2) % static_cast<size_t>(__cend_ - __cbeg_);
379 __c_node* p2 = __cbeg_[hc];
380 _LIBCPP_ASSERT(p2 != nullptr, "debug mode internal logic error swap C");
381 while (p2->__c_ != c2)
384 _LIBCPP_ASSERT(p2 != nullptr, "debug mode internal logic error swap D");
386 std::swap(p1->beg_, p2->beg_);
387 std::swap(p1->end_, p2->end_);
388 std::swap(p1->cap_, p2->cap_);
389 for (__i_node** p = p1->beg_; p != p1->end_; ++p)
391 for (__i_node** p = p2->beg_; p != p2->end_; ++p)
396 __libcpp_db::__insert_i(void* __i)
399 __insert_iterator(__i);
403 __c_node::__add(__i_node* i)
407 size_t nc = 2*static_cast<size_t>(cap_ - beg_);
410 __i_node** beg = (__i_node**)malloc(nc * sizeof(__i_node*));
412 #ifndef _LIBCPP_NO_EXCEPTIONS
418 memcpy(beg, beg_, nc/2*sizeof(__i_node*));
431 __libcpp_db::__insert_iterator(void* __i)
433 if (__isz_ + 1 > static_cast<size_t>(__iend_ - __ibeg_))
435 size_t nc = __next_prime(2*static_cast<size_t>(__iend_ - __ibeg_) + 1);
436 __i_node** ibeg = (__i_node**)calloc(nc, sizeof(void*));
438 #ifndef _LIBCPP_NO_EXCEPTIONS
443 for (__i_node** p = __ibeg_; p != __iend_; ++p)
448 size_t h = hash<void*>()(q->__i_) % nc;
449 __i_node* r = q->__next_;
450 q->__next_ = ibeg[h];
457 __iend_ = __ibeg_ + nc;
459 size_t hi = hash<void*>()(__i) % static_cast<size_t>(__iend_ - __ibeg_);
460 __i_node* p = __ibeg_[hi];
461 __i_node* r = __ibeg_[hi] = (__i_node*)malloc(sizeof(__i_node));
463 #ifndef _LIBCPP_NO_EXCEPTIONS
468 ::new(r) __i_node(__i, p, nullptr);
475 __libcpp_db::__find_iterator(const void* __i) const
477 __i_node* r = nullptr;
478 if (__ibeg_ != __iend_)
480 size_t h = hash<const void*>()(__i) % static_cast<size_t>(__iend_ - __ibeg_);
481 for (__i_node* nd = __ibeg_[h]; nd != nullptr; nd = nd->__next_)
495 __c_node::__remove(__i_node* p)
497 __i_node** r = find(beg_, end_, p);
498 _LIBCPP_ASSERT(r != end_, "debug mode internal logic error __c_node::__remove");
500 memmove(r, r+1, static_cast<size_t>(end_ - r)*sizeof(__i_node*));
503 _LIBCPP_END_NAMESPACE_STD