1 // Created on: 2011-02-03
2 // Created by: Mikhail SAZONOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OSD_MAllocHook_HeaderFile
17 #define _OSD_MAllocHook_HeaderFile
19 #include <Standard_TypeDef.hxx>
20 #include <Standard_Mutex.hxx>
25 * This class provides the possibility to set callback for memory
26 * allocation/deallocation.
27 * On MS Windows, it works only in Debug builds. It relies on the
28 * debug CRT function _CrtSetAllocHook (see MSDN for help).
34 * Interface of a class that should handle allocation/deallocation events
39 //! Allocation event handler
41 * It is called when allocation is done
43 * the size of the memory block in bytes
44 * @param theRequestNum
45 * the allocation order number of the memory block
47 virtual void AllocEvent
49 long theRequestNum) = 0;
51 //! Freeing event handler
53 * It is called when the block is freed
55 * the pointer to the user data section of the memory block
57 * the size of the memory block in bytes
58 * @param theRequestNum
59 * the allocation order number of the memory block
61 virtual void FreeEvent
64 long theRequestNum) = 0;
68 * Implementation of the handler that collects all events
69 * to the log file. It contains the method to generate the report
72 class LogFileHandler: public Callback
76 Standard_EXPORT LogFileHandler();
79 Standard_EXPORT ~LogFileHandler();
81 //! Create the file and start collecting events.
82 //! Return false if the file with the given name cannot be created.
83 Standard_EXPORT Standard_Boolean Open(const char* theFileName);
85 //! Close the file and stop collecting events
86 Standard_EXPORT void Close();
88 //! Make synthesized report on the given log file.
90 * Generate an easy to use report in the
91 * new file with the given name, taking the given log file as input.
92 * If theIncludeAlive is true then
93 * include into the report the alive allocation numbers.
95 Standard_EXPORT static Standard_Boolean MakeReport
96 (const char* theLogFile,
97 const char* theOutFile,
98 const Standard_Boolean theIncludeAlive = Standard_False);
100 Standard_EXPORT virtual void AllocEvent(size_t, long);
101 Standard_EXPORT virtual void FreeEvent(void*, size_t, long);
104 std::ofstream myLogFile;
105 Standard_Mutex myMutex;
110 * Implementation of the handler that collects numbers of
111 * allocations/deallocations for each block size directly in the memory.
113 class CollectBySize: public Callback
117 Standard_EXPORT CollectBySize();
120 Standard_EXPORT ~CollectBySize();
122 //! Reset the buffer and start collecting events.
123 Standard_EXPORT void Reset();
125 //! Write report in the given file.
126 Standard_EXPORT Standard_Boolean MakeReport(const char* theOutFile);
128 Standard_EXPORT virtual void AllocEvent(size_t, long);
129 Standard_EXPORT virtual void FreeEvent(void*, size_t, long);
137 Numbers() : nbAlloc(0), nbFree(0), nbLeftPeak(0) {}
140 static const size_t myMaxAllocSize; //!< maximum tracked size
142 Standard_Mutex myMutex; //!< used for thread-safe access
143 Numbers* myArray; //!< indexed from 0 to myMaxAllocSize-1
144 ptrdiff_t myTotalLeftSize; //!< currently remained allocated size
145 size_t myTotalPeakSize; //!< maxium cumulative allocated size
146 size_t myBreakSize; //!< user defined allocation size to debug (see place_for_breakpoint())
147 size_t myBreakPeak; //!< user defined peak size limit to debug
150 //! Set handler of allocation/deallocation events
152 * You can pass here any implementation. For easy start, you can try
153 * with the predefined handler LogFileHandler, which static instance
154 * is returned by GetLogFileHandler().
155 * To clear the handler, pass NULL here.
157 Standard_EXPORT static void SetCallback
160 //! Get current handler of allocation/deallocation events
161 Standard_EXPORT static Callback* GetCallback();
163 //! Get static instance of LogFileHandler handler
164 Standard_EXPORT static LogFileHandler* GetLogFileHandler();
166 //! Get static instance of CollectBySize handler
167 Standard_EXPORT static CollectBySize* GetCollectBySize();
170 #endif /* _OSD_MAllocHook_HeaderFile */