use hashed set in allocator for O(1) lookup

This commit is contained in:
Eduardo Bart
2011-07-12 19:29:06 -03:00
parent 4e6a1106fa
commit 5299ac0233
2 changed files with 33 additions and 36 deletions

View File

@@ -106,6 +106,25 @@ static void addr2line(void *address, const char* name, bool viewSource = false)
printf("\n");
}
void printBacktrace()
{
void *buffer[128];
int size = backtrace(buffer, 128);
char **strings = backtrace_symbols(buffer, size);
for(int i = 1; i < size; i++) {
if(i == 1) {
printf("\tfrom ");
} else {
printf("\tat ");
}
std::string str = strings[i];
addr2line(buffer[i], str.substr(0, str.find('(')).c_str());
}
printf("\n");
free(strings);
}
Allocator::Allocator()
{
}
@@ -118,16 +137,12 @@ Allocator::~Allocator()
dumpLeaks();
disableAllocator();
for(AllocationBlocksList::iterator it = m_allocationsBlocks.begin(), end = m_allocationsBlocks.end(); it != end; ++it) {
AllocationBlock* block = (*it);
free(block->backtraceBuffer);
free(block);
}
m_allocationsBlocks.clear();
enableAllocator();
}
void Allocator::dumpLeaks()
@@ -136,7 +151,11 @@ void Allocator::dumpLeaks()
boost::recursive_mutex::scoped_lock lock(m_allocatorLock);
#endif
disableAllocator();
bool shouldRenable = false;
if(isAllocatorEnabled()) {
disableAllocator();
shouldRenable = true;
}
unsigned int definitelyLostBytes = 0;
unsigned int blockNumber = 1;
@@ -187,25 +206,8 @@ void Allocator::dumpLeaks()
printf("leaked blocks: %d in %d blocks\n", numberOfLeakedBlocks, numberOfBlocks);
}
enableAllocator();
}
void printBacktrace()
{
void *buffer[128];
int size = backtrace(buffer, 128);
char **strings = backtrace_symbols(buffer, size);
for(int i = 1; i < size; i++) {
if(i == 1) {
printf("\tfrom ");
} else {
printf("\tat ");
}
std::string str = strings[i];
addr2line(buffer[i], str.substr(0, str.find('(')).c_str());
}
printf("\n");
free(strings);
if(shouldRenable)
enableAllocator();
}
AllocationBlock* Allocator::findBlock(void **backtraceBuffer, int backtraceSize, unsigned int bytes)