1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: MDI classes
4 // Author: Arthur Seaton, Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "memory.h"
20 #include "wx/string.h"
21 #include "wx/msgout.h"
24 The macro which will be expanded to include the file and line number
25 info, or to be a straight call to the new operator.
28 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
34 WXDLLIMPEXP_BASE
void * wxDebugAlloc(size_t size
, wxChar
* fileName
, int lineNum
, bool isObject
, bool isVect
= false);
35 WXDLLIMPEXP_BASE
void wxDebugFree(void * buf
, bool isVect
= false);
37 //**********************************************************************************
39 The global operator new used for everything apart from getting
40 dynamic storage within this function itself.
43 // We'll only do malloc and free for the moment: leave the interesting
44 // stuff for the wxObject versions.
45 // devik 2000-8-29: All new/delete ops are now inline because they can't
46 // be marked as dllexport/dllimport. It then leads to weird bugs when
49 #if wxUSE_GLOBAL_MEMORY_OPERATORS
51 // Undefine temporarily (new is #defined in object.h) because we want to
52 // declare some new operators.
57 #if defined(__SUNCC__)
58 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
59 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
60 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
61 #elif defined (__SGI_CC_)
62 // only supported by -n32 compilers
63 #ifndef __EDG_ABI_COMPATIBILITY_VERSION
64 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
66 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
67 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
69 // ::operator new[] is a recent C++ feature, so assume it's not supported
70 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
73 void * operator new (size_t size
, wxChar
* fileName
, int lineNum
);
75 void * operator new (size_t size
);
77 void operator delete (void * buf
);
79 #if wxUSE_ARRAY_MEMORY_OPERATORS
80 void * operator new[] (size_t size
);
82 void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
);
84 void operator delete[] (void * buf
);
87 // VC++ 6.0 and MWERKS
88 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || defined(__MWERKS__)
89 inline void operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
91 wxDebugFree(pData
, false);
93 inline void operator delete[](void* pData
, wxChar
* /* fileName */, int /* lineNum */)
95 wxDebugFree(pData
, true);
97 #endif // __VISUALC__>=1200
98 #endif // wxUSE_GLOBAL_MEMORY_OPERATORS
101 //**********************************************************************************
103 typedef unsigned int wxMarkerType
;
106 Define the struct which will be placed at the start of all dynamically
110 class WXDLLIMPEXP_BASE wxMemStruct
{
112 friend class WXDLLIMPEXP_BASE wxDebugContext
; // access to the m_next pointer for list traversal.
118 size_t RequestSize () { return m_reqSize
; }
119 wxMarkerType
Marker () { return m_firstMarker
; }
121 // When an object is deleted we set the id slot to a specific value.
122 inline void SetDeleted ();
123 inline int IsDeleted ();
128 // Used to determine if the object is really a wxMemStruct.
129 // Not a foolproof test by any means, but better than none I hope!
132 // Do all validation on a node.
135 // Check the integrity of a node and of the list, node by node.
137 int CheckAllPrevious ();
139 // Print a single node.
142 // Called when the memory linking functions get an error.
143 void ErrorMsg (const char *);
146 inline void *GetActualData(void) const { return m_actualData
; }
151 // Check for underwriting. There are 2 of these checks. This one
152 // inside the struct and another right after the struct.
153 wxMarkerType m_firstMarker
;
155 // File name and line number are from cpp.
159 // The amount of memory requested by the caller.
162 // Used to try to verify that we really are dealing with an object
163 // of the required class. Can be 1 of 2 values these indicating a valid
164 // wxMemStruct object, or a deleted wxMemStruct object.
167 wxMemStruct
* m_prev
;
168 wxMemStruct
* m_next
;
175 typedef void (wxMemStruct::*PmSFV
) ();
179 Debugging class. This will only have a single instance, but it's
180 a reasonable way to keep everything together and to make this
181 available for change if needed by someone else.
182 A lot of this stuff would be better off within the wxMemStruct class, but
183 it's stuff which we need to access at times when there is no wxMemStruct
184 object so we use this class instead. Think of it as a collection of
185 globals which have to do with the wxMemStruct class.
188 class WXDLLIMPEXP_BASE wxDebugContext
{
191 // Used to set alignment for markers.
192 static size_t CalcAlignment ();
194 // Returns the amount of padding needed after something of the given
195 // size. This is so that when we cast pointers backwards and forwards
196 // the pointer value will be valid for a wxMarkerType.
197 static size_t GetPadding (const size_t size
) ;
199 // Traverse the list.
200 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
202 static int debugLevel
;
205 static int m_balign
; // byte alignment
206 static int m_balignmask
; // mask for performing byte alignment
208 // Set a checkpoint to dump only the memory from
210 static wxMemStruct
*checkPoint
;
212 wxDebugContext(void);
213 ~wxDebugContext(void);
215 static int GetLevel(void) { return debugLevel
; }
216 static void SetLevel(int level
) { debugLevel
= level
; }
218 static bool GetDebugMode(void) { return debugOn
; }
219 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
221 static void SetCheckpoint(bool all
= false);
222 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
224 // Calculated from the request size and any padding needed
225 // before the final marker.
226 static size_t PaddedSize (const size_t reqSize
);
228 // Calc the total amount of space we need from the system
229 // to satisfy a caller request. This includes all padding.
230 static size_t TotSize (const size_t reqSize
);
232 // Return valid pointers to offsets within the allocated memory.
233 static char * StructPos (const char * buf
);
234 static char * MidMarkerPos (const char * buf
);
235 static char * CallerMemPos (const char * buf
);
236 static char * EndMarkerPos (const char * buf
, const size_t size
);
238 // Given a pointer to the start of the caller requested area
239 // return a pointer to the start of the entire alloc\'d buffer.
240 static char * StartPos (const char * caller
);
242 // Access to the list.
243 static wxMemStruct
* GetHead () { return m_head
; }
244 static wxMemStruct
* GetTail () { return m_tail
; }
246 // Set the list sentinals.
247 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
248 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
250 // If this is set then every new operation checks the validity
251 // of the all previous nodes in the list.
252 static bool GetCheckPrevious () { return m_checkPrevious
; }
253 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
255 // Checks all nodes, or all nodes if checkAll is true
256 static int Check(bool checkAll
= false);
258 // Print out the list of wxMemStruct nodes.
259 static bool PrintList(void);
262 static bool Dump(void);
265 static bool PrintStatistics(bool detailed
= true);
267 // Print out the classes in the application.
268 static bool PrintClasses(void);
270 // Count the number of non-wxDebugContext-related objects
271 // that are outstanding
272 static int CountObjectsLeft(bool sinceCheckpoint
= false);
274 // This function is used to output the dump
275 static void OutputDumpLine(const wxChar
*szFormat
, ...);
278 // Store these here to allow access to the list without
279 // needing to have a wxMemStruct object.
280 static wxMemStruct
* m_head
;
281 static wxMemStruct
* m_tail
;
283 // Set to false if we're not checking all previous nodes when
284 // we do a new. Set to true when we are.
285 static bool m_checkPrevious
;
288 // Final cleanup (e.g. deleting the log object and doing memory leak checking)
289 // will be delayed until all wxDebugContextDumpDelayCounter objects have been
290 // destructed. Adding one wxDebugContextDumpDelayCounter per file will delay
291 // memory leak checking until after destructing all global objects.
292 class WXDLLIMPEXP_BASE wxDebugContextDumpDelayCounter
295 wxDebugContextDumpDelayCounter() {
299 ~wxDebugContextDumpDelayCounter() {
301 if(!sm_count
) DoDump();
308 // make leak dump after all globals have been destructed
309 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_File
;
310 #define WXDEBUG_DUMPDELAYCOUNTER \
311 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_Extra;
313 // Output a debug message, in a system dependent fashion.
314 void WXDLLIMPEXP_BASE
wxTrace(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
;
315 void WXDLLIMPEXP_BASE
wxTraceLevel(int level
, const wxChar
*fmt
...) ATTRIBUTE_PRINTF_2
;
317 #define WXTRACE wxTrace
318 #define WXTRACELEVEL wxTraceLevel
320 #else // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
322 #define WXDEBUG_DUMPDELAYCOUNTER
324 // Borland C++ Builder 6 seems to have troubles with inline functions (see bug
327 inline void wxTrace(const wxChar
*WXUNUSED(fmt
)) {}
328 inline void wxTraceLevel(int WXUNUSED(level
), const wxChar
*WXUNUSED(fmt
)) {}
331 #define wxTraceLevel(l, fmt)
334 #define WXTRACE true ? (void)0 : wxTrace
335 #define WXTRACELEVEL true ? (void)0 : wxTraceLevel
337 #endif // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT