1 // File: OSD_MAllocHook.hxx
3 // Author: Mikhail SAZONOV
4 // Copyright: Open CASCADE S.A.S. 2011
6 #ifndef _OSD_MAllocHook_HeaderFile
7 #define _OSD_MAllocHook_HeaderFile
9 #include <Standard_TypeDef.hxx>
10 #include <Standard_Mutex.hxx>
14 * This class provides the possibility to set callback for memory
15 * allocation/deallocation.
16 * On MS Windows, it works only in Debug builds. It relies on the
17 * debug CRT function _CrtSetAllocHook (see MSDN for help).
23 * Interface of a class that should handle allocation/deallocation events
28 //! Allocation event handler
30 * It is called when allocation is done
32 * the size of the memory block in bytes
33 * @param theRequestNum
34 * the allocation order number of the memory block
36 virtual void AllocEvent
38 long theRequestNum) = 0;
40 //! Freeing event handler
42 * It is called when the block is freed
44 * the pointer to the user data section of the memory block
46 * the size of the memory block in bytes
47 * @param theRequestNum
48 * the allocation order number of the memory block
50 virtual void FreeEvent
53 long theRequestNum) = 0;
57 * Implementation of the handler that collects all events
58 * to the log file. It contains the method to generate the report
61 class LogFileHandler: public Callback
65 Standard_EXPORT LogFileHandler();
68 Standard_EXPORT ~LogFileHandler();
70 //! Create the file and start collecting events.
71 //! Return false if the file with the given name cannot be created.
72 Standard_EXPORT Standard_Boolean Open(const char* theFileName);
74 //! Close the file and stop collecting events
75 Standard_EXPORT void Close();
77 //! Make synthesized report on the given log file.
79 * Generate an easy to use report in the
80 * new file with the given name, taking the given log file as input.
81 * If theIncludeAlive is true then
82 * include into the report the alive allocation numbers.
84 Standard_EXPORT static Standard_Boolean MakeReport
85 (const char* theLogFile,
86 const char* theOutFile,
87 const Standard_Boolean theIncludeAlive = Standard_False);
89 Standard_EXPORT virtual void AllocEvent(size_t, long);
90 Standard_EXPORT virtual void FreeEvent(void*, size_t, long);
94 Standard_Mutex myMutex;
99 * Implementation of the handler that collects numbers of
100 * allocations/deallocations for each block size directly in the memory.
102 class CollectBySize: public Callback
106 Standard_EXPORT CollectBySize();
109 Standard_EXPORT ~CollectBySize();
111 //! Reset the buffer and start collecting events.
112 Standard_EXPORT void Reset();
114 //! Write report in the given file.
115 Standard_EXPORT Standard_Boolean MakeReport(const char* theOutFile);
117 Standard_EXPORT virtual void AllocEvent(size_t, long);
118 Standard_EXPORT virtual void FreeEvent(void*, size_t, long);
126 Numbers() : nbAlloc(0), nbFree(0), nbLeftPeak(0) {}
129 Standard_Mutex myMutex;
131 ptrdiff_t myTotalLeftSize;
132 size_t myTotalPeakSize;
136 //! Set handler of allocation/deallocation events
138 * You can pass here any implementation. For easy start, you can try
139 * with the predefined handler LogFileHandler, which static instance
140 * is returned by GetLogFileHandler().
141 * To clear the handler, pass NULL here.
143 Standard_EXPORT static void SetCallback
146 //! Get current handler of allocation/deallocation events
147 Standard_EXPORT static Callback* GetCallback();
149 //! Get static instance of LogFileHandler handler
150 Standard_EXPORT static LogFileHandler* GetLogFileHandler();
152 //! Get static instance of CollectBySize handler
153 Standard_EXPORT static CollectBySize* GetCollectBySize();
156 #endif /* _OSD_MAllocHook_HeaderFile */