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 inline void * operator new (size_t size
, wxChar
* fileName
, int lineNum
)
75 return wxDebugAlloc(size
, fileName
, lineNum
, FALSE
, FALSE
);
78 inline void * operator new (size_t size
)
80 return wxDebugAlloc(size
, NULL
, 0, FALSE
);
83 inline void operator delete (void * buf
)
85 wxDebugFree(buf
, FALSE
);
88 #if wxUSE_ARRAY_MEMORY_OPERATORS
89 inline void * operator new[] (size_t size
)
91 return wxDebugAlloc(size
, NULL
, 0, FALSE
, TRUE
);
94 inline void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
96 return wxDebugAlloc(size
, fileName
, lineNum
, FALSE
, TRUE
);
99 inline void operator delete[] (void * buf
)
101 wxDebugFree(buf
, TRUE
);
105 // VC++ 6.0 and MWERKS
106 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || defined(__MWERKS__)
107 inline void operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
109 wxDebugFree(pData
, FALSE
);
111 inline void operator delete[](void* pData
, wxChar
* /* fileName */, int /* lineNum */)
113 wxDebugFree(pData
, TRUE
);
115 #endif // __VISUALC__>=1200
116 #endif // wxUSE_GLOBAL_MEMORY_OPERATORS
117 #endif // __WXDEBUG__
119 //**********************************************************************************
121 typedef unsigned int wxMarkerType
;
124 Define the struct which will be placed at the start of all dynamically
128 class WXDLLIMPEXP_BASE wxMemStruct
{
130 friend class WXDLLIMPEXP_BASE wxDebugContext
; // access to the m_next pointer for list traversal.
136 size_t RequestSize () { return m_reqSize
; }
137 wxMarkerType
Marker () { return m_firstMarker
; }
139 // When an object is deleted we set the id slot to a specific value.
140 inline void SetDeleted ();
141 inline int IsDeleted ();
146 // Used to determine if the object is really a wxMemStruct.
147 // Not a foolproof test by any means, but better than none I hope!
150 // Do all validation on a node.
153 // Check the integrity of a node and of the list, node by node.
155 int CheckAllPrevious ();
157 // Print a single node.
160 // Called when the memory linking functions get an error.
161 void ErrorMsg (const char *);
164 inline void *GetActualData(void) const { return m_actualData
; }
169 // Check for underwriting. There are 2 of these checks. This one
170 // inside the struct and another right after the struct.
171 wxMarkerType m_firstMarker
;
173 // File name and line number are from cpp.
177 // The amount of memory requested by the caller.
180 // Used to try to verify that we really are dealing with an object
181 // of the required class. Can be 1 of 2 values these indicating a valid
182 // wxMemStruct object, or a deleted wxMemStruct object.
185 wxMemStruct
* m_prev
;
186 wxMemStruct
* m_next
;
193 typedef void (wxMemStruct::*PmSFV
) ();
197 Debugging class. This will only have a single instance, but it's
198 a reasonable way to keep everything together and to make this
199 available for change if needed by someone else.
200 A lot of this stuff would be better off within the wxMemStruct class, but
201 it's stuff which we need to access at times when there is no wxMemStruct
202 object so we use this class instead. Think of it as a collection of
203 globals which have to do with the wxMemStruct class.
206 class WXDLLIMPEXP_BASE wxDebugContext
{
209 // Used to set alignment for markers.
210 static size_t CalcAlignment ();
212 // Returns the amount of padding needed after something of the given
213 // size. This is so that when we cast pointers backwards and forwards
214 // the pointer value will be valid for a wxMarkerType.
215 static size_t GetPadding (const size_t size
) ;
217 // Traverse the list.
218 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
220 static int debugLevel
;
223 static int m_balign
; // byte alignment
224 static int m_balignmask
; // mask for performing byte alignment
226 // Set a checkpoint to dump only the memory from
228 static wxMemStruct
*checkPoint
;
230 wxDebugContext(void);
231 ~wxDebugContext(void);
233 static int GetLevel(void) { return debugLevel
; }
234 static void SetLevel(int level
) { debugLevel
= level
; }
236 static bool GetDebugMode(void) { return debugOn
; }
237 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
239 static void SetCheckpoint(bool all
= FALSE
);
240 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
242 // Calculated from the request size and any padding needed
243 // before the final marker.
244 static size_t PaddedSize (const size_t reqSize
);
246 // Calc the total amount of space we need from the system
247 // to satisfy a caller request. This includes all padding.
248 static size_t TotSize (const size_t reqSize
);
250 // Return valid pointers to offsets within the allocated memory.
251 static char * StructPos (const char * buf
);
252 static char * MidMarkerPos (const char * buf
);
253 static char * CallerMemPos (const char * buf
);
254 static char * EndMarkerPos (const char * buf
, const size_t size
);
256 // Given a pointer to the start of the caller requested area
257 // return a pointer to the start of the entire alloc\'d buffer.
258 static char * StartPos (const char * caller
);
260 // Access to the list.
261 static wxMemStruct
* GetHead () { return m_head
; }
262 static wxMemStruct
* GetTail () { return m_tail
; }
264 // Set the list sentinals.
265 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
266 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
268 // If this is set then every new operation checks the validity
269 // of the all previous nodes in the list.
270 static bool GetCheckPrevious () { return m_checkPrevious
; }
271 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
273 // Checks all nodes, or all nodes if checkAll is TRUE
274 static int Check(bool checkAll
= FALSE
);
276 // Print out the list of wxMemStruct nodes.
277 static bool PrintList(void);
280 static bool Dump(void);
283 static bool PrintStatistics(bool detailed
= TRUE
);
285 // Print out the classes in the application.
286 static bool PrintClasses(void);
288 // Count the number of non-wxDebugContext-related objects
289 // that are outstanding
290 static int CountObjectsLeft(bool sinceCheckpoint
= FALSE
);
292 // This function is used to output the dump
293 static void OutputDumpLine(const wxChar
*szFormat
, ...);
296 // Store these here to allow access to the list without
297 // needing to have a wxMemStruct object.
298 static wxMemStruct
* m_head
;
299 static wxMemStruct
* m_tail
;
301 // Set to FALSE if we're not checking all previous nodes when
302 // we do a new. Set to TRUE when we are.
303 static bool m_checkPrevious
;
306 // Final cleanup (e.g. deleting the log object and doing memory leak checking)
307 // will be delayed until all wxDebugContextDumpDelayCounter objects have been
308 // destructed. Adding one wxDebugContextDumpDelayCounter per file will delay
309 // memory leak checking until after destructing all global objects.
310 class WXDLLIMPEXP_BASE wxDebugContextDumpDelayCounter
313 wxDebugContextDumpDelayCounter() {
317 ~wxDebugContextDumpDelayCounter() {
319 if(!sm_count
) DoDump();
326 // make leak dump after all globals have been destructed
327 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_File
;
328 #define WXDEBUG_DUMPDELAYCOUNTER \
329 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_Extra;
331 // Output a debug message, in a system dependent fashion.
332 void WXDLLIMPEXP_BASE
wxTrace(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
;
333 void WXDLLIMPEXP_BASE
wxTraceLevel(int level
, const wxChar
*fmt
...) ATTRIBUTE_PRINTF_2
;
335 #define WXTRACE wxTrace
336 #define WXTRACELEVEL wxTraceLevel
338 #else // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
340 #define WXDEBUG_DUMPDELAYCOUNTER
342 // Borland C++ Builder 6 seems to have troubles with inline functions (see bug
345 inline void wxTrace(const wxChar
*WXUNUSED(fmt
)) {}
346 inline void wxTraceLevel(int WXUNUSED(level
), const wxChar
*WXUNUSED(fmt
)) {}
349 #define wxTraceLevel(l, fmt)
352 #define WXTRACE TRUE ? (void)0 : wxTrace
353 #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
355 #endif // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT