QGpCoreTools/MemoryCheckerPrivate.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2007-01-29
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef MEMORYCHECKERPRIVATE_H
00029 #define MEMORYCHECKERPRIVATE_H
00030 
00031 #ifdef MEMORY_CHECKER
00032 
00033 #include <QtCore>
00034 
00035 namespace QGpCoreTools {
00036 
00037 #ifdef Q_WS_X11           // dll mechanism does not allow a secure allocation
00038                           // unless Qt is linked with this dll (this was the reason for qtbdebugmem.dll)
00039 #define DO_MEMORY_CHECK
00040 #endif
00041 #define ABORT_ON_WARNING
00042 
00043 // defines for new,... must be removed because they are defined here as functions
00044 #undef new
00045 #undef isBlockDamaged
00046 #undef printBlockInfo
00047 
00048 #ifdef DO_MEMORY_CHECK
00049 
00050 struct BlockInfo
00051 {
00052   void * blockAddress;
00053   unsigned int sz;
00054   bool record; // true if this block must be reported as undeleted if still in memory
00055   const char * fName;
00056   int fLine;
00057   BlockInfo * next;
00058   BlockInfo * previous;
00059 };
00060 
00061 #define NOMANSLAND_SIZE 16
00062 #define HEADER_SIZE (sizeof(BlockInfo *)+NOMANSLAND_SIZE)
00063 #define TAIL_SIZE NOMANSLAND_SIZE
00064 
00065 class MemoryCheckerPrivate
00066 {
00067 public:
00068   static inline void* allocateBlock(std::size_t sz, const char* file, int line);
00069   static inline void freeBlock(void* v);
00070 
00071   static void setEnabled(bool e) {_enabled=e;}
00072   static bool isEnabled() {return _enabled;}
00073 
00074   static void setRecord(bool r) {_record=r;}
00075   static void setVerbose(bool v) {_verbose=v;}
00076 
00077   static void checkBlocks(bool showLib, bool showUndeleted, const char* file, int line);
00078   static bool isBlockDamaged(void* v, const char* file, int line);
00079   static void printBlockInfo(void * ptr, const char* file, int line);
00080 private:
00081   static void printHeader(FILE * f, const char * prefix, unsigned char * ptr);
00082   static inline bool testNoMansLand(unsigned char * ptr);
00083   static inline BlockInfo * addBlock(void * blockAddress, unsigned int sz, const char * fName, int fLine);
00084   static inline void delBlock(BlockInfo * b);
00085   static inline BlockInfo * findBlock(void * blockAddress);
00086   static inline void srcLocation(const char * fName, int fLine);
00087   static inline void srcLocation(BlockInfo * b);
00088 
00089   static bool _enabled;
00090   static bool _record;
00091   static bool _verbose;
00092   static BlockInfo * _infos;
00093   static QMutex _mutex;
00094 };
00095 
00096 #endif // DO_MEMORY_CHECK
00097 
00098 } // namespace QGpCoreTools
00099 
00100 #endif // MEMORY_CHECKER
00101 
00102 #endif // MEMORYCHECKERPRIVATE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines