Another installment on debug mode. This addresses list. However this should be considered a temporary state. The API of the debug database and how vector and list use it, is unsatisfactory at the moment. It is both inefficient and overly verbose. I wanted to get this functionality checked in though. In the next day or so I'll refactor what is there in an attempt to streamline things.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@140660 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant 2011-09-27 23:55:03 +00:00
parent 6cd05eeb35
commit 1c3ec6d480
4 changed files with 672 additions and 62 deletions

View File

@ -60,7 +60,7 @@ struct _LIBCPP_VISIBLE __c_node
virtual bool __addable(const void*, ptrdiff_t) const = 0; virtual bool __addable(const void*, ptrdiff_t) const = 0;
virtual bool __subscriptable(const void*, ptrdiff_t) const = 0; virtual bool __subscriptable(const void*, ptrdiff_t) const = 0;
_LIBCPP_HIDDEN void __add(__i_node* __i); void __add(__i_node* __i);
_LIBCPP_HIDDEN void __remove(__i_node* __i); _LIBCPP_HIDDEN void __remove(__i_node* __i);
}; };
@ -159,6 +159,7 @@ public:
void* __find_c_from_i(void* __i) const; void* __find_c_from_i(void* __i) const;
void __invalidate_all(void* __c); void __invalidate_all(void* __c);
__c_node* __find_c_and_lock(void* __c) const; __c_node* __find_c_and_lock(void* __c) const;
__c_node* __find_c(void* __c) const;
void unlock() const; void unlock() const;
void swap(void* __c1, void* __c2); void swap(void* __c1, void* __c2);

File diff suppressed because it is too large Load Diff

View File

@ -120,20 +120,18 @@ __libcpp_db::__insert_ic(void* __i, const void* __c)
{ {
WLock _(mut()); WLock _(mut());
__i_node* i = __insert_iterator(__i); __i_node* i = __insert_iterator(__i);
_LIBCPP_ASSERT(__cbeg_ != __cend_, "Container constructed in a translation unit with debug mode disabled." const char* errmsg =
"Container constructed in a translation unit with debug mode disabled."
" But it is being used in a translation unit with debug mode enabled." " But it is being used in a translation unit with debug mode enabled."
" Enable it in the other translation unit with #define _LIBCPP_DEBUG2 1"); " Enable it in the other translation unit with #define _LIBCPP_DEBUG2 1";
_LIBCPP_ASSERT(__cbeg_ != __cend_, errmsg);
size_t hc = hash<const void*>()(__c) % (__cend_ - __cbeg_); size_t hc = hash<const void*>()(__c) % (__cend_ - __cbeg_);
__c_node* c = __cbeg_[hc]; __c_node* c = __cbeg_[hc];
_LIBCPP_ASSERT(c != nullptr, "Container constructed in a translation unit with debug mode disabled." _LIBCPP_ASSERT(c != nullptr, errmsg);
" But it is being used in a translation unit with debug mode enabled."
" Enable it in the other translation unit with #define _LIBCPP_DEBUG2 1");
while (c->__c_ != __c) while (c->__c_ != __c)
{ {
c = c->__next_; c = c->__next_;
_LIBCPP_ASSERT(c != nullptr, "Container constructed in a translation unit with debug mode disabled." _LIBCPP_ASSERT(c != nullptr, errmsg);
" But it is being used in a translation unit with debug mode enabled."
" Enable it in the other translation unit with #define _LIBCPP_DEBUG2 1");
} }
c->__add(i); c->__add(i);
i->__c_ = c; i->__c_ = c;
@ -241,6 +239,20 @@ __libcpp_db::__find_c_and_lock(void* __c) const
return p; return p;
} }
__c_node*
__libcpp_db::__find_c(void* __c) const
{
size_t hc = hash<void*>()(__c) % (__cend_ - __cbeg_);
__c_node* p = __cbeg_[hc];
_LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __find_c A");
while (p->__c_ != __c)
{
p = p->__next_;
_LIBCPP_ASSERT(p != nullptr, "debug mode internal logic error __find_c B");
}
return p;
}
void void
__libcpp_db::unlock() const __libcpp_db::unlock() const
{ {
@ -380,6 +392,27 @@ __libcpp_db::__insert_i(void* __i)
__insert_iterator(__i); __insert_iterator(__i);
} }
void
__c_node::__add(__i_node* i)
{
if (end_ == cap_)
{
size_t nc = 2*(cap_ - beg_);
if (nc == 0)
nc = 1;
__i_node** beg = (__i_node**)malloc(nc * sizeof(__i_node*));
if (beg == nullptr)
throw bad_alloc();
if (nc > 1)
memcpy(beg, beg_, nc/2*sizeof(__i_node*));
free(beg_);
beg_ = beg;
end_ = beg_ + nc/2;
cap_ = beg_ + nc;
}
*end_++ = i;
}
// private api // private api
_LIBCPP_HIDDEN _LIBCPP_HIDDEN
@ -438,28 +471,6 @@ __libcpp_db::__find_iterator(const void* __i) const
return r; return r;
} }
_LIBCPP_HIDDEN
void
__c_node::__add(__i_node* i)
{
if (end_ == cap_)
{
size_t nc = 2*(cap_ - beg_);
if (nc == 0)
nc = 1;
__i_node** beg = (__i_node**)malloc(nc * sizeof(__i_node*));
if (beg == nullptr)
throw bad_alloc();
if (nc > 1)
memcpy(beg, beg_, nc/2*sizeof(__i_node*));
free(beg_);
beg_ = beg;
end_ = beg_ + nc/2;
cap_ = beg_ + nc;
}
*end_++ = i;
}
_LIBCPP_HIDDEN _LIBCPP_HIDDEN
void void
__c_node::__remove(__i_node* p) __c_node::__remove(__i_node* p)

View File

@ -58,6 +58,8 @@ int main()
assert(c2.empty()); assert(c2.empty());
assert(distance(c2.begin(), c2.end()) == 0); assert(distance(c2.begin(), c2.end()) == 0);
} }
#ifndef _LIBCPP_DEBUG_LEVEL
// This test known to result in undefined behavior detected by _LIBCPP_DEBUG_LEVEL >= 1
{ {
int a1[] = {1, 3, 7, 9, 10}; int a1[] = {1, 3, 7, 9, 10};
int a2[] = {0, 2, 4, 5, 6, 8, 11}; int a2[] = {0, 2, 4, 5, 6, 8, 11};
@ -70,6 +72,7 @@ int main()
assert((c2 == std::list<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0])))); assert((c2 == std::list<int, A>(a1, a1+sizeof(a1)/sizeof(a1[0]))));
assert(c2.get_allocator() == A(2)); assert(c2.get_allocator() == A(2));
} }
#endif
{ {
int a1[] = {1, 3, 7, 9, 10}; int a1[] = {1, 3, 7, 9, 10};
int a2[] = {0, 2, 4, 5, 6, 8, 11}; int a2[] = {0, 2, 4, 5, 6, 8, 11};