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"
22 The macro which will be expanded to include the file and line number
23 info, or to be a straight call to the new operator.
26 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
39 #include "wx/string.h"
43 // #ifndef WXDEBUG_NEW
44 // #define WXDEBUG_NEW new(__FILE__,__LINE__)
47 void * wxDebugAlloc(size_t size
, char * fileName
, int lineNum
, bool isObject
, bool isVect
= FALSE
);
48 void wxDebugFree(void * buf
, bool isVect
= FALSE
);
50 // Global versions of the new and delete operators.
51 // Currently, these merely call malloc and free; only the wxObject
52 // operators do something interesting. But this allows WXDEBUG_NEW to
53 // work for all 'new's in a file.
54 #if wxUSE_GLOBAL_MEMORY_OPERATORS
60 void * operator new (size_t size
, char * fileName
, int lineNum
);
61 void operator delete (void * buf
);
65 void operator delete(void *buf
, char*, int);
68 #if !( defined (_MSC_VER) && (_MSC_VER <= 1020) )
69 void * operator new[] (size_t size
, char * fileName
, int lineNum
);
70 void operator delete[] (void * buf
);
76 typedef unsigned int wxMarkerType
;
79 Define the struct which will be placed at the start of all dynamically
83 class WXDLLEXPORT wxMemStruct
{
85 friend class WXDLLEXPORT wxDebugContext
; // access to the m_next pointer for list traversal.
91 size_t RequestSize () { return m_reqSize
; }
92 wxMarkerType
Marker () { return m_firstMarker
; }
94 // When an object is deleted we set the id slot to a specific value.
95 inline void SetDeleted ();
96 inline int IsDeleted ();
101 // Used to determine if the object is really a wxMemStruct.
102 // Not a foolproof test by any means, but better than none I hope!
105 // Do all validation on a node.
108 // Check the integrity of a node and of the list, node by node.
110 int CheckAllPrevious ();
112 // Print a single node.
115 // Called when the memory linking functions get an error.
116 void ErrorMsg (const char *);
119 inline void *GetActualData(void) const { return m_actualData
; }
124 // Check for underwriting. There are 2 of these checks. This one
125 // inside the struct and another right after the struct.
126 wxMarkerType m_firstMarker
;
128 // File name and line number are from cpp.
132 // The amount of memory requested by the caller.
135 // Used to try to verify that we really are dealing with an object
136 // of the required class. Can be 1 of 2 values these indicating a valid
137 // wxMemStruct object, or a deleted wxMemStruct object.
140 wxMemStruct
* m_prev
;
141 wxMemStruct
* m_next
;
148 typedef void (wxMemStruct::*PmSFV
) ();
152 Debugging class. This will only have a single instance, but it\'s
153 a reasonable way to keep everything together and to make this
154 available for change if needed by someone else.
155 A lot of this stuff would be better off within the wxMemStruct class, but
156 it\'s stuff which we need to access at times when there is no wxMemStruct
157 object so we use this class instead. Think of it as a collection of
158 globals which have to do with the wxMemStruct class.
161 class WXDLLEXPORT wxDebugContext
{
164 // Used to set alignment for markers.
165 static size_t CalcAlignment ();
167 // Returns the amount of padding needed after something of the given
168 // size. This is so that when we cast pointers backwards and forwards
169 // the pointer value will be valid for a wxMarkerType.
170 static size_t GetPadding (const size_t size
) ;
172 // Traverse the list.
173 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
175 static streambuf
*m_streamBuf
;
176 static ostream
*m_debugStream
;
178 static int debugLevel
;
182 // Set a checkpoint to dump only the memory from
184 static wxMemStruct
*checkPoint
;
186 wxDebugContext(void);
187 ~wxDebugContext(void);
189 static bool HasStream(void) { return (m_debugStream
!= NULL
); };
190 static ostream
& GetStream(void) { return *m_debugStream
; }
191 static streambuf
*GetStreamBuf(void) { return m_streamBuf
; }
192 static void SetStream(ostream
*stream
, streambuf
*buf
= NULL
);
193 static bool SetFile(const wxString
& file
);
194 static bool SetStandardError(void);
196 static int GetLevel(void) { return debugLevel
; }
197 static void SetLevel(int level
) { debugLevel
= level
; }
199 static bool GetDebugMode(void) { return debugOn
; }
200 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
202 static void SetCheckpoint(bool all
= FALSE
);
203 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
205 // Calculated from the request size and any padding needed
206 // before the final marker.
207 static size_t PaddedSize (const size_t reqSize
);
209 // Calc the total amount of space we need from the system
210 // to satisfy a caller request. This includes all padding.
211 static size_t TotSize (const size_t reqSize
);
213 // Return valid pointers to offsets within the allocated memory.
214 static char * StructPos (const char * buf
);
215 static char * MidMarkerPos (const char * buf
);
216 static char * CallerMemPos (const char * buf
);
217 static char * EndMarkerPos (const char * buf
, const size_t size
);
219 // Given a pointer to the start of the caller requested area
220 // return a pointer to the start of the entire alloc\'d buffer.
221 static char * StartPos (const char * caller
);
223 // Access to the list.
224 static wxMemStruct
* GetHead () { return m_head
; }
225 static wxMemStruct
* GetTail () { return m_tail
; }
227 // Set the list sentinals.
228 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
229 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
231 // If this is set then every new operation checks the validity
232 // of the all previous nodes in the list.
233 static bool GetCheckPrevious () { return m_checkPrevious
; }
234 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
236 // Checks all nodes, or all nodes if checkAll is TRUE
237 static int Check(bool checkAll
= FALSE
);
239 // Print out the list of wxMemStruct nodes.
240 static bool PrintList(void);
243 static bool Dump(void);
246 static bool PrintStatistics(bool detailed
= TRUE
);
248 // Print out the classes in the application.
249 static bool PrintClasses(void);
251 // Count the number of non-wxDebugContext-related objects
252 // that are outstanding
253 static int CountObjectsLeft(void);
256 // Store these here to allow access to the list without
257 // needing to have a wxMemStruct object.
258 static wxMemStruct
* m_head
;
259 static wxMemStruct
* m_tail
;
261 // Set to FALSE if we're not checking all previous nodes when
262 // we do a new. Set to TRUE when we are.
263 static bool m_checkPrevious
;
266 // Output a debug mess., in a system dependent fashion.
267 void WXDLLEXPORT
wxTrace(const char *fmt
...);
268 void WXDLLEXPORT
wxTraceLevel(int level
, const char *fmt
...);
270 #define WXTRACE wxTrace
271 #define WXTRACELEVEL wxTraceLevel
273 #else // else part for the #if WXDEBUG
275 inline void wxTrace(const char *WXUNUSED(fmt
)) {}
276 inline void wxTraceLevel(int WXUNUSED(level
), const char *WXUNUSED(fmt
)) {}
278 #define WXTRACE TRUE ? (void)0 : wxTrace
279 #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
280 // #define WXDEBUG_NEW new