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"
23 The macro which will be expanded to include the file and line number
24 info, or to be a straight call to the new operator.
27 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
33 WXDLLIMPEXP_BASE
void * wxDebugAlloc(size_t size
, wxChar
* fileName
, int lineNum
, bool isObject
, bool isVect
= FALSE
);
34 WXDLLIMPEXP_BASE
void wxDebugFree(void * buf
, bool isVect
= FALSE
);
36 //**********************************************************************************
38 The global operator new used for everything apart from getting
39 dynamic storage within this function itself.
42 // We'll only do malloc and free for the moment: leave the interesting
43 // stuff for the wxObject versions.
44 // devik 2000-8-29: All new/delete ops are now inline because they can't
45 // be marked as dllexport/dllimport. It then leads to weird bugs when
48 #if wxUSE_GLOBAL_MEMORY_OPERATORS
50 // Undefine temporarily (new is #defined in object.h) because we want to
51 // declare some new operators.
56 #if defined(__SUNCC__)
57 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
58 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
59 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
60 #elif defined (__SGI_CC_)
61 // only supported by -n32 compilers
62 #ifndef __EDG_ABI_COMPATIBILITY_VERSION
63 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
65 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
66 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
68 // ::operator new[] is a recent C++ feature, so assume it's not supported
69 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
72 inline void * operator new (size_t size
, wxChar
* fileName
, int lineNum
)
74 return wxDebugAlloc(size
, fileName
, lineNum
, FALSE
, FALSE
);
77 inline void * operator new (size_t size
)
79 return wxDebugAlloc(size
, NULL
, 0, FALSE
);
82 inline void operator delete (void * buf
)
84 wxDebugFree(buf
, FALSE
);
87 #if wxUSE_ARRAY_MEMORY_OPERATORS
88 inline void * operator new[] (size_t size
)
90 return wxDebugAlloc(size
, NULL
, 0, FALSE
, TRUE
);
93 inline void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
95 return wxDebugAlloc(size
, fileName
, lineNum
, FALSE
, TRUE
);
98 inline void operator delete[] (void * buf
)
100 wxDebugFree(buf
, TRUE
);
104 // VC++ 6.0 and MWERKS
105 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || defined(__MWERKS__)
106 inline void operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
108 wxDebugFree(pData
, FALSE
);
110 inline void operator delete[](void* pData
, wxChar
* /* fileName */, int /* lineNum */)
112 wxDebugFree(pData
, TRUE
);
114 #endif // __VISUALC__>=1200
115 #endif // wxUSE_GLOBAL_MEMORY_OPERATORS
116 #endif // __WXDEBUG__
118 //**********************************************************************************
120 typedef unsigned int wxMarkerType
;
123 Define the struct which will be placed at the start of all dynamically
127 class WXDLLIMPEXP_BASE wxMemStruct
{
129 friend class WXDLLIMPEXP_BASE wxDebugContext
; // access to the m_next pointer for list traversal.
135 size_t RequestSize () { return m_reqSize
; }
136 wxMarkerType
Marker () { return m_firstMarker
; }
138 // When an object is deleted we set the id slot to a specific value.
139 inline void SetDeleted ();
140 inline int IsDeleted ();
145 // Used to determine if the object is really a wxMemStruct.
146 // Not a foolproof test by any means, but better than none I hope!
149 // Do all validation on a node.
152 // Check the integrity of a node and of the list, node by node.
154 int CheckAllPrevious ();
156 // Print a single node.
159 // Called when the memory linking functions get an error.
160 void ErrorMsg (const char *);
163 inline void *GetActualData(void) const { return m_actualData
; }
168 // Check for underwriting. There are 2 of these checks. This one
169 // inside the struct and another right after the struct.
170 wxMarkerType m_firstMarker
;
172 // File name and line number are from cpp.
176 // The amount of memory requested by the caller.
179 // Used to try to verify that we really are dealing with an object
180 // of the required class. Can be 1 of 2 values these indicating a valid
181 // wxMemStruct object, or a deleted wxMemStruct object.
184 wxMemStruct
* m_prev
;
185 wxMemStruct
* m_next
;
192 typedef void (wxMemStruct::*PmSFV
) ();
196 Debugging class. This will only have a single instance, but it's
197 a reasonable way to keep everything together and to make this
198 available for change if needed by someone else.
199 A lot of this stuff would be better off within the wxMemStruct class, but
200 it's stuff which we need to access at times when there is no wxMemStruct
201 object so we use this class instead. Think of it as a collection of
202 globals which have to do with the wxMemStruct class.
205 class WXDLLIMPEXP_BASE wxDebugContext
{
208 // Used to set alignment for markers.
209 static size_t CalcAlignment ();
211 // Returns the amount of padding needed after something of the given
212 // size. This is so that when we cast pointers backwards and forwards
213 // the pointer value will be valid for a wxMarkerType.
214 static size_t GetPadding (const size_t size
) ;
216 // Traverse the list.
217 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
219 static int debugLevel
;
222 static int m_balign
; // byte alignment
223 static int m_balignmask
; // mask for performing byte alignment
225 // Set a checkpoint to dump only the memory from
227 static wxMemStruct
*checkPoint
;
229 wxDebugContext(void);
230 ~wxDebugContext(void);
232 static int GetLevel(void) { return debugLevel
; }
233 static void SetLevel(int level
) { debugLevel
= level
; }
235 static bool GetDebugMode(void) { return debugOn
; }
236 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
238 static void SetCheckpoint(bool all
= FALSE
);
239 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
241 // Calculated from the request size and any padding needed
242 // before the final marker.
243 static size_t PaddedSize (const size_t reqSize
);
245 // Calc the total amount of space we need from the system
246 // to satisfy a caller request. This includes all padding.
247 static size_t TotSize (const size_t reqSize
);
249 // Return valid pointers to offsets within the allocated memory.
250 static char * StructPos (const char * buf
);
251 static char * MidMarkerPos (const char * buf
);
252 static char * CallerMemPos (const char * buf
);
253 static char * EndMarkerPos (const char * buf
, const size_t size
);
255 // Given a pointer to the start of the caller requested area
256 // return a pointer to the start of the entire alloc\'d buffer.
257 static char * StartPos (const char * caller
);
259 // Access to the list.
260 static wxMemStruct
* GetHead () { return m_head
; }
261 static wxMemStruct
* GetTail () { return m_tail
; }
263 // Set the list sentinals.
264 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
265 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
267 // If this is set then every new operation checks the validity
268 // of the all previous nodes in the list.
269 static bool GetCheckPrevious () { return m_checkPrevious
; }
270 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
272 // Checks all nodes, or all nodes if checkAll is TRUE
273 static int Check(bool checkAll
= FALSE
);
275 // Print out the list of wxMemStruct nodes.
276 static bool PrintList(void);
279 static bool Dump(void);
282 static bool PrintStatistics(bool detailed
= TRUE
);
284 // Print out the classes in the application.
285 static bool PrintClasses(void);
287 // Count the number of non-wxDebugContext-related objects
288 // that are outstanding
289 static int CountObjectsLeft(bool sinceCheckpoint
= FALSE
);
292 // Store these here to allow access to the list without
293 // needing to have a wxMemStruct object.
294 static wxMemStruct
* m_head
;
295 static wxMemStruct
* m_tail
;
297 // Set to FALSE if we're not checking all previous nodes when
298 // we do a new. Set to TRUE when we are.
299 static bool m_checkPrevious
;
302 // Output a debug message, in a system dependent fashion.
303 void WXDLLIMPEXP_BASE
wxTrace(const wxChar
*fmt
...) ATTRIBUTE_PRINTF_1
;
304 void WXDLLIMPEXP_BASE
wxTraceLevel(int level
, const wxChar
*fmt
...) ATTRIBUTE_PRINTF_2
;
306 #define WXTRACE wxTrace
307 #define WXTRACELEVEL wxTraceLevel
309 #else // else part for the #if __WXDEBUG__
311 // Borland C++ Builder 6 seems to have troubles with inline functions (see bug
314 inline void wxTrace(const wxChar
*WXUNUSED(fmt
)) {}
315 inline void wxTraceLevel(int WXUNUSED(level
), const wxChar
*WXUNUSED(fmt
)) {}
318 #define wxTraceLevel(l, fmt)
321 #define WXTRACE TRUE ? (void)0 : wxTrace
322 #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
324 #endif // __WXDEBUG__