// ************************************************************************** // * This file is part of the FreeFileSync project. It is distributed under * // * GNU General Public License: http://www.gnu.org/licenses/gpl.html * // * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved * // ************************************************************************** #ifndef ZSTRING_H_INCLUDED #define ZSTRING_H_INCLUDED #include //strcmp() #include "string_base.h" #ifndef NDEBUG #include "thread.h" //includes #include #endif #ifndef NDEBUG class LeakChecker //small test for memory leaks { public: void insert(const void* ptr, size_t size) { boost::lock_guard dummy(lockActStrings); if (activeStrings.find(ptr) != activeStrings.end()) reportProblem("Fatal Error: New memory points into occupied space: " + rawMemToString(ptr, size)); activeStrings[ptr] = size; } void remove(const void* ptr) { boost::lock_guard dummy(lockActStrings); if (activeStrings.find(ptr) == activeStrings.end()) reportProblem("Fatal Error: No memory available for deallocation at this location!"); activeStrings.erase(ptr); } static LeakChecker& instance(); private: LeakChecker() {} LeakChecker(const LeakChecker&); LeakChecker& operator=(const LeakChecker&); ~LeakChecker(); static std::string rawMemToString(const void* ptr, size_t size); void reportProblem(const std::string& message); //throw std::logic_error boost::mutex lockActStrings; zen::hash_map activeStrings; }; #endif //NDEBUG class AllocatorFreeStoreChecked { public: static void* allocate(size_t size) //throw std::bad_alloc { #ifndef NDEBUG void* newMem = ::operator new(size); LeakChecker::instance().insert(newMem, size); //test Zbase for memory leaks return newMem; #else return ::operator new(size); #endif } static void deallocate(void* ptr) { #ifndef NDEBUG LeakChecker::instance().remove(ptr); //check for memory leaks #endif ::operator delete(ptr); } static size_t calcCapacity(size_t length) { return std::max(16, length + length / 2); } //exponential growth + min size }; //############################## helper functions ############################################# #if defined(FFS_WIN) || defined(FFS_LINUX) //Compare filenames: Windows does NOT distinguish between upper/lower-case, while Linux DOES template class SP, class AP> int cmpFileName(const zen::Zbase& lhs, const zen::Zbase& rhs); struct LessFilename //case-insensitive on Windows, case-sensitive on Linux { template class SP, class AP> bool operator()(const zen::Zbase& lhs, const zen::Zbase& rhs) const; }; struct EqualFilename //case-insensitive on Windows, case-sensitive on Linux { template class SP, class AP> bool operator()(const zen::Zbase& lhs, const zen::Zbase& rhs) const; }; #endif #ifdef FFS_WIN template