1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: MDI classes
4 // Author: Arthur Seaton, Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
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
34 // N.B. BC++ doesn't have istream.h, ostream.h
35 # include <iostream.h>
38 # if defined(__VISUALC__) || defined(__MWERKS__)
39 // using namespace std;
46 // devik 2000-8-27: export these because new/delete operators are now inline
47 WXDLLEXPORT
void * wxDebugAlloc(size_t size
, wxChar
* fileName
, int lineNum
, bool isObject
, bool isVect
= FALSE
);
48 WXDLLEXPORT
void wxDebugFree(void * buf
, bool isVect
= FALSE
);
50 //**********************************************************************************
52 The global operator new used for everything apart from getting
53 dynamic storage within this function itself.
56 // We'll only do malloc and free for the moment: leave the interesting
57 // stuff for the wxObject versions.
58 // devik 2000-8-29: All new/delete ops are now inline because they can't
59 // be marked as dllexport/dllimport. It then leads to weird bugs when
62 #if wxUSE_GLOBAL_MEMORY_OPERATORS
64 // Undefine temporarily (new is #defined in object.h) because we want to
65 // declare some new operators.
70 #if defined(__SUNCC__)
71 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
72 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
73 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
74 #elif defined (__SGI_CC_)
75 // only supported by -n32 compilers
76 #ifndef __EDG_ABI_COMPATIBILITY_VERSION
77 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
79 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
80 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
82 // ::operator new[] is a recent C++ feature, so assume it's not supported
83 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
86 inline void * operator new (size_t size
, wxChar
* fileName
, int lineNum
)
88 return wxDebugAlloc(size
, fileName
, lineNum
, FALSE
, FALSE
);
91 inline void * operator new (size_t size
)
93 return wxDebugAlloc(size
, NULL
, 0, FALSE
);
96 inline void operator delete (void * buf
)
98 wxDebugFree(buf
, FALSE
);
101 #if wxUSE_ARRAY_MEMORY_OPERATORS
102 inline void * operator new[] (size_t size
)
104 return wxDebugAlloc(size
, NULL
, 0, FALSE
, TRUE
);
107 inline void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
109 return wxDebugAlloc(size
, fileName
, lineNum
, FALSE
, TRUE
);
112 inline void operator delete[] (void * buf
)
114 wxDebugFree(buf
, TRUE
);
118 // VC++ 6.0 and MWERKS
119 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || defined(__MWERKS__)
120 inline void operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
122 wxDebugFree(pData
, FALSE
);
124 inline void operator delete[](void* pData
, wxChar
* /* fileName */, int /* lineNum */)
126 wxDebugFree(pData
, TRUE
);
128 #endif // __VISUALC__>=1200
129 #endif // wxUSE_GLOBAL_MEMORY_OPERATORS
130 #endif // __WXDEBUG__
132 //**********************************************************************************
134 typedef unsigned int wxMarkerType
;
137 Define the struct which will be placed at the start of all dynamically
141 class WXDLLEXPORT wxMemStruct
{
143 friend class WXDLLEXPORT wxDebugContext
; // access to the m_next pointer for list traversal.
149 size_t RequestSize () { return m_reqSize
; }
150 wxMarkerType
Marker () { return m_firstMarker
; }
152 // When an object is deleted we set the id slot to a specific value.
153 inline void SetDeleted ();
154 inline int IsDeleted ();
159 // Used to determine if the object is really a wxMemStruct.
160 // Not a foolproof test by any means, but better than none I hope!
163 // Do all validation on a node.
166 // Check the integrity of a node and of the list, node by node.
168 int CheckAllPrevious ();
170 // Print a single node.
173 // Called when the memory linking functions get an error.
174 void ErrorMsg (const char *);
177 inline void *GetActualData(void) const { return m_actualData
; }
182 // Check for underwriting. There are 2 of these checks. This one
183 // inside the struct and another right after the struct.
184 wxMarkerType m_firstMarker
;
186 // File name and line number are from cpp.
190 // The amount of memory requested by the caller.
193 // Used to try to verify that we really are dealing with an object
194 // of the required class. Can be 1 of 2 values these indicating a valid
195 // wxMemStruct object, or a deleted wxMemStruct object.
198 wxMemStruct
* m_prev
;
199 wxMemStruct
* m_next
;
206 typedef void (wxMemStruct::*PmSFV
) ();
210 Debugging class. This will only have a single instance, but it\'s
211 a reasonable way to keep everything together and to make this
212 available for change if needed by someone else.
213 A lot of this stuff would be better off within the wxMemStruct class, but
214 it\'s stuff which we need to access at times when there is no wxMemStruct
215 object so we use this class instead. Think of it as a collection of
216 globals which have to do with the wxMemStruct class.
219 class WXDLLEXPORT wxDebugContext
{
222 // Used to set alignment for markers.
223 static size_t CalcAlignment ();
225 // Returns the amount of padding needed after something of the given
226 // size. This is so that when we cast pointers backwards and forwards
227 // the pointer value will be valid for a wxMarkerType.
228 static size_t GetPadding (const size_t size
) ;
230 // Traverse the list.
231 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
235 static wxSTD streambuf
*m_streamBuf
;
236 static wxSTD ostream
*m_debugStream
;
239 static int debugLevel
;
242 static int m_balign
; // byte alignment
243 static int m_balignmask
; // mask for performing byte alignment
245 // Set a checkpoint to dump only the memory from
247 static wxMemStruct
*checkPoint
;
249 wxDebugContext(void);
250 ~wxDebugContext(void);
254 static bool HasStream(void) { return (m_debugStream
!= NULL
); };
255 static wxSTD ostream
& GetStream(void) { return *m_debugStream
; }
256 static wxSTD streambuf
*GetStreamBuf(void) { return m_streamBuf
; }
257 static void SetStream(wxSTD ostream
*stream
, wxSTD streambuf
*buf
= NULL
);
258 static bool SetFile(const wxString
& file
);
259 static bool SetStandardError(void);
262 static int GetLevel(void) { return debugLevel
; }
263 static void SetLevel(int level
) { debugLevel
= level
; }
265 static bool GetDebugMode(void) { return debugOn
; }
266 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
268 static void SetCheckpoint(bool all
= FALSE
);
269 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
271 // Calculated from the request size and any padding needed
272 // before the final marker.
273 static size_t PaddedSize (const size_t reqSize
);
275 // Calc the total amount of space we need from the system
276 // to satisfy a caller request. This includes all padding.
277 static size_t TotSize (const size_t reqSize
);
279 // Return valid pointers to offsets within the allocated memory.
280 static char * StructPos (const char * buf
);
281 static char * MidMarkerPos (const char * buf
);
282 static char * CallerMemPos (const char * buf
);
283 static char * EndMarkerPos (const char * buf
, const size_t size
);
285 // Given a pointer to the start of the caller requested area
286 // return a pointer to the start of the entire alloc\'d buffer.
287 static char * StartPos (const char * caller
);
289 // Access to the list.
290 static wxMemStruct
* GetHead () { return m_head
; }
291 static wxMemStruct
* GetTail () { return m_tail
; }
293 // Set the list sentinals.
294 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
295 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
297 // If this is set then every new operation checks the validity
298 // of the all previous nodes in the list.
299 static bool GetCheckPrevious () { return m_checkPrevious
; }
300 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
302 // Checks all nodes, or all nodes if checkAll is TRUE
303 static int Check(bool checkAll
= FALSE
);
305 // Print out the list of wxMemStruct nodes.
306 static bool PrintList(void);
309 static bool Dump(void);
312 static bool PrintStatistics(bool detailed
= TRUE
);
314 // Print out the classes in the application.
315 static bool PrintClasses(void);
317 // Count the number of non-wxDebugContext-related objects
318 // that are outstanding
319 static int CountObjectsLeft(bool sinceCheckpoint
= FALSE
);
322 // Store these here to allow access to the list without
323 // needing to have a wxMemStruct object.
324 static wxMemStruct
* m_head
;
325 static wxMemStruct
* m_tail
;
327 // Set to FALSE if we're not checking all previous nodes when
328 // we do a new. Set to TRUE when we are.
329 static bool m_checkPrevious
;
332 // Output a debug message, in a system dependent fashion.
333 void WXDLLEXPORT
wxTrace(const wxChar
*fmt
...);
334 void WXDLLEXPORT
wxTraceLevel(int level
, const wxChar
*fmt
...);
336 #define WXTRACE wxTrace
337 #define WXTRACELEVEL wxTraceLevel
339 #else // else part for the #if __WXDEBUG__
341 inline void wxTrace(const wxChar
*WXUNUSED(fmt
)) {}
342 inline void wxTraceLevel(int WXUNUSED(level
), const wxChar
*WXUNUSED(fmt
)) {}
344 #define WXTRACE TRUE ? (void)0 : wxTrace
345 #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
346 // #define WXDEBUG_NEW new
348 #endif // __WXDEBUG__