|
||||
File indexing completed on 2025-01-18 10:04:37
0001 // Created on: 2011-02-03 0002 // Created by: Mikhail SAZONOV 0003 // Copyright (c) 2011-2014 OPEN CASCADE SAS 0004 // 0005 // This file is part of Open CASCADE Technology software library. 0006 // 0007 // This library is free software; you can redistribute it and/or modify it under 0008 // the terms of the GNU Lesser General Public License version 2.1 as published 0009 // by the Free Software Foundation, with special exception defined in the file 0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT 0011 // distribution for complete text of the license and disclaimer of any warranty. 0012 // 0013 // Alternatively, this file may be used under the terms of Open CASCADE 0014 // commercial license or contractual agreement. 0015 0016 #ifndef _OSD_MAllocHook_HeaderFile 0017 #define _OSD_MAllocHook_HeaderFile 0018 0019 #include <Standard_Mutex.hxx> 0020 #include <fstream> 0021 0022 /** 0023 * This class provides the possibility to set callback for memory 0024 * allocation/deallocation. 0025 * On MS Windows, it works only in Debug builds. It relies on the 0026 * debug CRT function _CrtSetAllocHook (see MSDN for help). 0027 */ 0028 class OSD_MAllocHook 0029 { 0030 public: 0031 /** 0032 * Interface of a class that should handle allocation/deallocation events 0033 */ 0034 class Callback 0035 { 0036 public: 0037 //! Allocation event handler 0038 /** 0039 * It is called when allocation is done 0040 * @param theSize 0041 * the size of the memory block in bytes 0042 * @param theRequestNum 0043 * the allocation order number of the memory block 0044 */ 0045 virtual void AllocEvent 0046 (size_t theSize, 0047 long theRequestNum) = 0; 0048 0049 //! Freeing event handler 0050 /** 0051 * It is called when the block is freed 0052 * @param theData 0053 * the pointer to the user data section of the memory block 0054 * @param theSize 0055 * the size of the memory block in bytes 0056 * @param theRequestNum 0057 * the allocation order number of the memory block 0058 */ 0059 virtual void FreeEvent 0060 (void* theData, 0061 size_t theSize, 0062 long theRequestNum) = 0; 0063 }; 0064 0065 /** 0066 * Implementation of the handler that collects all events 0067 * to the log file. It contains the method to generate the report 0068 * from the log file. 0069 */ 0070 class LogFileHandler: public Callback 0071 { 0072 public: 0073 //! Constructor 0074 Standard_EXPORT LogFileHandler(); 0075 0076 //! Destructor 0077 Standard_EXPORT ~LogFileHandler(); 0078 0079 //! Create the file and start collecting events. 0080 //! Return false if the file with the given name cannot be created. 0081 Standard_EXPORT Standard_Boolean Open(const char* theFileName); 0082 0083 //! Close the file and stop collecting events 0084 Standard_EXPORT void Close(); 0085 0086 //! Make synthesized report on the given log file. 0087 /** 0088 * Generate an easy to use report in the 0089 * new file with the given name, taking the given log file as input. 0090 * If theIncludeAlive is true then 0091 * include into the report the alive allocation numbers. 0092 */ 0093 Standard_EXPORT static Standard_Boolean MakeReport 0094 (const char* theLogFile, 0095 const char* theOutFile, 0096 const Standard_Boolean theIncludeAlive = Standard_False); 0097 0098 Standard_EXPORT virtual void AllocEvent(size_t, long); 0099 Standard_EXPORT virtual void FreeEvent(void*, size_t, long); 0100 0101 private: 0102 std::ofstream myLogFile; 0103 Standard_Mutex myMutex; 0104 size_t myBreakSize; 0105 }; 0106 0107 /** 0108 * Implementation of the handler that collects numbers of 0109 * allocations/deallocations for each block size directly in the memory. 0110 */ 0111 class CollectBySize: public Callback 0112 { 0113 public: 0114 //! Constructor 0115 Standard_EXPORT CollectBySize(); 0116 0117 //! Destructor 0118 Standard_EXPORT ~CollectBySize(); 0119 0120 //! Reset the buffer and start collecting events. 0121 Standard_EXPORT void Reset(); 0122 0123 //! Write report in the given file. 0124 Standard_EXPORT Standard_Boolean MakeReport(const char* theOutFile); 0125 0126 Standard_EXPORT virtual void AllocEvent(size_t, long); 0127 Standard_EXPORT virtual void FreeEvent(void*, size_t, long); 0128 0129 public: 0130 struct Numbers 0131 { 0132 int nbAlloc; 0133 int nbFree; 0134 int nbLeftPeak; 0135 Numbers() : nbAlloc(0), nbFree(0), nbLeftPeak(0) {} 0136 }; 0137 0138 static const size_t myMaxAllocSize; //!< maximum tracked size 0139 0140 Standard_Mutex myMutex; //!< used for thread-safe access 0141 Numbers* myArray; //!< indexed from 0 to myMaxAllocSize-1 0142 ptrdiff_t myTotalLeftSize; //!< currently remained allocated size 0143 size_t myTotalPeakSize; //!< maximum cumulative allocated size 0144 size_t myBreakSize; //!< user defined allocation size to debug (see place_for_breakpoint()) 0145 size_t myBreakPeak; //!< user defined peak size limit to debug 0146 }; 0147 0148 //! Set handler of allocation/deallocation events 0149 /** 0150 * You can pass here any implementation. For easy start, you can try 0151 * with the predefined handler LogFileHandler, which static instance 0152 * is returned by GetLogFileHandler(). 0153 * To clear the handler, pass NULL here. 0154 */ 0155 Standard_EXPORT static void SetCallback 0156 (Callback* theCB); 0157 0158 //! Get current handler of allocation/deallocation events 0159 Standard_EXPORT static Callback* GetCallback(); 0160 0161 //! Get static instance of LogFileHandler handler 0162 Standard_EXPORT static LogFileHandler* GetLogFileHandler(); 0163 0164 //! Get static instance of CollectBySize handler 0165 Standard_EXPORT static CollectBySize* GetCollectBySize(); 0166 }; 0167 0168 #endif /* _OSD_MAllocHook_HeaderFile */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |