From: <hv...@us...> - 2013-07-25 14:21:48
|
Revision: 58413 http://sourceforge.net/p/firebird/code/58413 Author: hvlad Date: 2013-07-25 14:21:46 +0000 (Thu, 25 Jul 2013) Log Message: ----------- Fixed memory leak when fbclient library is unloaded manually Modified Paths: -------------- firebird/trunk/src/common/classes/alloc.cpp firebird/trunk/src/common/classes/alloc.h Modified: firebird/trunk/src/common/classes/alloc.cpp =================================================================== --- firebird/trunk/src/common/classes/alloc.cpp 2013-07-25 11:07:06 UTC (rev 58412) +++ firebird/trunk/src/common/classes/alloc.cpp 2013-07-25 14:21:46 UTC (rev 58413) @@ -115,6 +115,45 @@ MemoryStats* MemoryPool::default_stats_group = NULL; Mutex* cache_mutex = NULL; + + +namespace { + +// We cache this amount of extents to avoid memory mapping overhead +const int MAP_CACHE_SIZE = 16; // == 1 MB + +Vector<void*, MAP_CACHE_SIZE> extents_cache; + +volatile size_t map_page_size = 0; +int dev_zero_fd = 0; + +#if defined(WIN_NT) +size_t get_page_size() +{ + SYSTEM_INFO info; + GetSystemInfo(&info); + return info.dwPageSize; +} +#else +size_t get_page_size() +{ + return sysconf(_SC_PAGESIZE); +} +#endif + +inline size_t get_map_page_size() +{ + if (!map_page_size) + { + MutexLockGuard guard(*cache_mutex, "get_map_page_size"); + if (!map_page_size) + map_page_size = get_page_size(); + } + return map_page_size; +} + +} + // Initialize process memory pool (called from InstanceControl). void MemoryPool::init() @@ -157,6 +196,9 @@ default_stats_group = NULL; } + while (extents_cache.getCount()) + releaseRaw(extents_cache.pop(), DEFAULT_ALLOCATION, false); + if (cache_mutex) { cache_mutex->~Mutex(); @@ -748,43 +790,7 @@ freeBlock->nextTwin = freeBlock->priorTwin = freeBlock; } -namespace { -// We cache this amount of extents to avoid memory mapping overhead -const int MAP_CACHE_SIZE = 16; // == 1 MB - -Vector<void*, MAP_CACHE_SIZE> extents_cache; - -volatile size_t map_page_size = 0; -int dev_zero_fd = 0; - -#if defined(WIN_NT) -size_t get_page_size() -{ - SYSTEM_INFO info; - GetSystemInfo(&info); - return info.dwPageSize; -} -#else -size_t get_page_size() -{ - return sysconf(_SC_PAGESIZE); -} -#endif - -inline size_t get_map_page_size() -{ - if (!map_page_size) - { - MutexLockGuard guard(*cache_mutex, "get_map_page_size"); - if (!map_page_size) - map_page_size = get_page_size(); - } - return map_page_size; -} - -} - void* MemoryPool::allocRaw(size_t size) throw (OOM_EXCEPTION) { #ifndef USE_VALGRIND @@ -884,10 +890,10 @@ } } -void MemoryPool::releaseRaw(void* block, size_t size) throw () +void MemoryPool::releaseRaw(void* block, size_t size, bool use_cache) throw () { #ifndef USE_VALGRIND - if (size == DEFAULT_ALLOCATION) + if (use_cache && (size == DEFAULT_ALLOCATION)) { MutexLockGuard guard(*cache_mutex, "MemoryPool::releaseRaw"); if (extents_cache.getCount() < extents_cache.getCapacity()) Modified: firebird/trunk/src/common/classes/alloc.h =================================================================== --- firebird/trunk/src/common/classes/alloc.h 2013-07-25 11:07:06 UTC (rev 58412) +++ firebird/trunk/src/common/classes/alloc.h 2013-07-25 14:21:46 UTC (rev 58413) @@ -292,7 +292,7 @@ ) throw (OOM_EXCEPTION); protected: - void corrupt(const char* text) throw (); + static void corrupt(const char* text) throw (); private: virtual void memoryIsExhausted(void) throw (OOM_EXCEPTION); @@ -302,7 +302,7 @@ void validateFreeList(void) throw (); void validateBigBlock(MemBigObject* block) throw (); static void release(void* block) throw (); - void releaseRaw(void *block, size_t size) throw (); + static void releaseRaw(void *block, size_t size, bool use_cache = true) throw (); #ifdef USE_VALGRIND // Circular FIFO buffer of read/write protected blocks pending free operation This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |