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
32 // N.B. BC++ doesn't have istream.h, ostream.h
33 # include <iostream.h>
36 # if defined(__VISUALC__) || defined(__MWERKS__)
43 void * wxDebugAlloc(size_t size
, wxChar
* fileName
, int lineNum
, bool isObject
, bool isVect
= FALSE
);
44 void wxDebugFree(void * buf
, bool isVect
= FALSE
);
46 // Global versions of the new and delete operators.
47 #if wxUSE_GLOBAL_MEMORY_OPERATORS
49 // Undefine temporarily (new is #defined in object.h) because we want to
50 // declare some new operators.
55 #if defined(__SUNCC__)
56 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
57 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
58 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
59 #elif defined (__SGI_CC_)
60 // only supported by -n32 compilers
61 #ifndef __EDG_ABI_COMPATIBILITY_VERSION
62 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
65 // ::operator new[] is a recent C++ feature, so assume it's not supported
66 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
69 // Added JACS 25/11/98: needed for some compilers
70 void * operator new (size_t size
);
71 WXDLLEXPORT
void * operator new (size_t size
, wxChar
* fileName
, int lineNum
);
73 #if !defined(__VISAGECPP__)
74 void operator delete (void * buf
);
77 #if wxUSE_ARRAY_MEMORY_OPERATORS
78 WXDLLEXPORT
void* operator new[] (size_t size
);
79 WXDLLEXPORT
void* operator new[] (size_t size
, wxChar
* fileName
, int lineNum
);
80 WXDLLEXPORT
void operator delete[] (void * buf
);
84 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
85 WXDLLEXPORT
void operator delete(void *buf
, wxChar
*, int);
86 WXDLLEXPORT
void operator delete[](void *buf
, wxChar
*, int);
90 // wxUSE_GLOBAL_MEMORY_OPERATORS
94 typedef unsigned int wxMarkerType
;
97 Define the struct which will be placed at the start of all dynamically
101 class WXDLLEXPORT wxMemStruct
{
103 friend class WXDLLEXPORT wxDebugContext
; // access to the m_next pointer for list traversal.
109 size_t RequestSize () { return m_reqSize
; }
110 wxMarkerType
Marker () { return m_firstMarker
; }
112 // When an object is deleted we set the id slot to a specific value.
113 inline void SetDeleted ();
114 inline int IsDeleted ();
119 // Used to determine if the object is really a wxMemStruct.
120 // Not a foolproof test by any means, but better than none I hope!
123 // Do all validation on a node.
126 // Check the integrity of a node and of the list, node by node.
128 int CheckAllPrevious ();
130 // Print a single node.
133 // Called when the memory linking functions get an error.
134 void ErrorMsg (const char *);
137 inline void *GetActualData(void) const { return m_actualData
; }
142 // Check for underwriting. There are 2 of these checks. This one
143 // inside the struct and another right after the struct.
144 wxMarkerType m_firstMarker
;
146 // File name and line number are from cpp.
150 // The amount of memory requested by the caller.
153 // Used to try to verify that we really are dealing with an object
154 // of the required class. Can be 1 of 2 values these indicating a valid
155 // wxMemStruct object, or a deleted wxMemStruct object.
158 wxMemStruct
* m_prev
;
159 wxMemStruct
* m_next
;
166 typedef void (wxMemStruct::*PmSFV
) ();
170 Debugging class. This will only have a single instance, but it\'s
171 a reasonable way to keep everything together and to make this
172 available for change if needed by someone else.
173 A lot of this stuff would be better off within the wxMemStruct class, but
174 it\'s stuff which we need to access at times when there is no wxMemStruct
175 object so we use this class instead. Think of it as a collection of
176 globals which have to do with the wxMemStruct class.
179 class WXDLLEXPORT wxDebugContext
{
182 // Used to set alignment for markers.
183 static size_t CalcAlignment ();
185 // Returns the amount of padding needed after something of the given
186 // size. This is so that when we cast pointers backwards and forwards
187 // the pointer value will be valid for a wxMarkerType.
188 static size_t GetPadding (const size_t size
) ;
190 // Traverse the list.
191 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
193 static streambuf
*m_streamBuf
;
194 static ostream
*m_debugStream
;
196 static int debugLevel
;
200 // Set a checkpoint to dump only the memory from
202 static wxMemStruct
*checkPoint
;
204 wxDebugContext(void);
205 ~wxDebugContext(void);
207 static bool HasStream(void) { return (m_debugStream
!= NULL
); };
208 static ostream
& GetStream(void) { return *m_debugStream
; }
209 static streambuf
*GetStreamBuf(void) { return m_streamBuf
; }
210 static void SetStream(ostream
*stream
, streambuf
*buf
= NULL
);
211 static bool SetFile(const wxString
& file
);
212 static bool SetStandardError(void);
214 static int GetLevel(void) { return debugLevel
; }
215 static void SetLevel(int level
) { debugLevel
= level
; }
217 static bool GetDebugMode(void) { return debugOn
; }
218 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
220 static void SetCheckpoint(bool all
= FALSE
);
221 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
223 // Calculated from the request size and any padding needed
224 // before the final marker.
225 static size_t PaddedSize (const size_t reqSize
);
227 // Calc the total amount of space we need from the system
228 // to satisfy a caller request. This includes all padding.
229 static size_t TotSize (const size_t reqSize
);
231 // Return valid pointers to offsets within the allocated memory.
232 static char * StructPos (const char * buf
);
233 static char * MidMarkerPos (const char * buf
);
234 static char * CallerMemPos (const char * buf
);
235 static char * EndMarkerPos (const char * buf
, const size_t size
);
237 // Given a pointer to the start of the caller requested area
238 // return a pointer to the start of the entire alloc\'d buffer.
239 static char * StartPos (const char * caller
);
241 // Access to the list.
242 static wxMemStruct
* GetHead () { return m_head
; }
243 static wxMemStruct
* GetTail () { return m_tail
; }
245 // Set the list sentinals.
246 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
247 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
249 // If this is set then every new operation checks the validity
250 // of the all previous nodes in the list.
251 static bool GetCheckPrevious () { return m_checkPrevious
; }
252 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
254 // Checks all nodes, or all nodes if checkAll is TRUE
255 static int Check(bool checkAll
= FALSE
);
257 // Print out the list of wxMemStruct nodes.
258 static bool PrintList(void);
261 static bool Dump(void);
264 static bool PrintStatistics(bool detailed
= TRUE
);
266 // Print out the classes in the application.
267 static bool PrintClasses(void);
269 // Count the number of non-wxDebugContext-related objects
270 // that are outstanding
271 static int CountObjectsLeft(bool sinceCheckpoint
= FALSE
);
274 // Store these here to allow access to the list without
275 // needing to have a wxMemStruct object.
276 static wxMemStruct
* m_head
;
277 static wxMemStruct
* m_tail
;
279 // Set to FALSE if we're not checking all previous nodes when
280 // we do a new. Set to TRUE when we are.
281 static bool m_checkPrevious
;
284 // Output a debug message, in a system dependent fashion.
285 void WXDLLEXPORT
wxTrace(const wxChar
*fmt
...);
286 void WXDLLEXPORT
wxTraceLevel(int level
, const wxChar
*fmt
...);
288 #define WXTRACE wxTrace
289 #define WXTRACELEVEL wxTraceLevel
291 #else // else part for the #if __WXDEBUG__
293 inline void wxTrace(const wxChar
*WXUNUSED(fmt
)) {}
294 inline void wxTraceLevel(int WXUNUSED(level
), const wxChar
*WXUNUSED(fmt
)) {}
296 #define WXTRACE TRUE ? (void)0 : wxTrace
297 #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
298 // #define WXDEBUG_NEW new
300 #endif // __WXDEBUG__