// ************************************************************************** // * 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) 2008-2011 ZenJu (zhnmju123 AT gmx.de) * // ************************************************************************** #ifndef ZSTRING_H_INCLUDED #define ZSTRING_H_INCLUDED #include "string_base.h" #include //strcmp() #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(std::string("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(std::string("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; typedef std::map VoidPtrSizeMap; VoidPtrSizeMap 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 Zbase& lhs, const Zbase& rhs); struct LessFilename //case-insensitive on Windows, case-sensitive on Linux { template class SP, class AP> bool operator()(const Zbase& lhs, const Zbase& rhs) const; }; struct EqualFilename //case-insensitive on Windows, case-sensitive on Linux { template class SP, class AP> bool operator()(const Zbase& lhs, const Zbase& rhs) const; }; #endif #ifdef FFS_WIN template