From 5299ac0233dc7d63f0734bf42dd1bb0a845c4062 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Tue, 12 Jul 2011 19:29:06 -0300 Subject: [PATCH] use hashed set in allocator for O(1) lookup --- src/allocator.cpp | 50 ++++++++++++++++++++++++----------------------- src/allocator.h | 19 +++++++----------- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/allocator.cpp b/src/allocator.cpp index a66f2ddb..015b95b3 100644 --- a/src/allocator.cpp +++ b/src/allocator.cpp @@ -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) diff --git a/src/allocator.h b/src/allocator.h index 58f86c77..dfec9eef 100644 --- a/src/allocator.h +++ b/src/allocator.h @@ -4,6 +4,7 @@ #ifdef _DEBUG_MEMORY #include +#include #ifdef _REENTRANT #include @@ -19,23 +20,17 @@ struct AllocationBlock struct block_hash : std::unary_function { std::size_t operator()(const AllocationBlock *block) const { - struct HashKey { - unsigned int bytes; - void *backtraceTop[3]; - unsigned char backtraceSize; - } hashKey; + std::size_t seed = 0; - hashKey.bytes = block->bytes; + boost::hash_combine(seed, block->bytes); for(int i=0;i<3;++i) { if(i < block->backtraceSize) - hashKey.backtraceTop[i] = block->backtraceBuffer[i]; + boost::hash_combine(seed, block->backtraceBuffer[i]); else - hashKey.backtraceTop[i] = NULL; + boost::hash_combine(seed, 0); } - hashKey.backtraceSize = block->backtraceSize; - - //std::hash hasher; - return 1;//hasher(hashKey); + boost::hash_combine(seed, block->backtraceSize); + return seed; } };