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 && USE_MEMORY_TRACING) || USE_DEBUG_CONTEXT
36 #include "wx/string.h"
39 #define WXDEBUG_NEW new(__FILE__,__LINE__)
43 void * wxDebugAlloc(size_t size
, char * 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 // Currently, these merely call malloc and free; only the wxObject
48 // operators do something interesting. But this allows WXDEBUG_NEW to
49 // work for all 'new's in a file.
50 #if USE_GLOBAL_MEMORY_OPERATORS
56 void * operator new (size_t size
, char * fileName
, int lineNum
);
57 void operator delete (void * buf
);
59 #if !( defined (_MSC_VER) && (_MSC_VER <= 1000) )
60 void * operator new[] (size_t size
, char * fileName
, int lineNum
);
61 void operator delete[] (void * buf
);
64 #define new WXDEBUG_NEW
69 typedef unsigned int wxMarkerType
;
72 Define the struct which will be placed at the start of all dynamically
76 class WXDLLEXPORT wxMemStruct
{
78 friend class WXDLLEXPORT wxDebugContext
; // access to the m_next pointer for list traversal.
84 size_t RequestSize () { return m_reqSize
; }
85 wxMarkerType
Marker () { return m_firstMarker
; }
87 // When an object is deleted we set the id slot to a specific value.
88 inline void SetDeleted ();
89 inline int IsDeleted ();
94 // Used to determine if the object is really a wxMemStruct.
95 // Not a foolproof test by any means, but better than none I hope!
98 // Do all validation on a node.
101 // Check the integrity of a node and of the list, node by node.
103 int CheckAllPrevious ();
105 // Print a single node.
108 // Called when the memory linking functions get an error.
109 void ErrorMsg (const char *);
112 inline void *GetActualData(void) const { return m_actualData
; }
117 // Check for underwriting. There are 2 of these checks. This one
118 // inside the struct and another right after the struct.
119 wxMarkerType m_firstMarker
;
121 // File name and line number are from cpp.
125 // The amount of memory requested by the caller.
128 // Used to try to verify that we really are dealing with an object
129 // of the required class. Can be 1 of 2 values these indicating a valid
130 // wxMemStruct object, or a deleted wxMemStruct object.
133 wxMemStruct
* m_prev
;
134 wxMemStruct
* m_next
;
141 typedef void (wxMemStruct::*PmSFV
) ();
145 Debugging class. This will only have a single instance, but it\'s
146 a reasonable way to keep everything together and to make this
147 available for change if needed by someone else.
148 A lot of this stuff would be better off within the wxMemStruct class, but
149 it\'s stuff which we need to access at times when there is no wxMemStruct
150 object so we use this class instead. Think of it as a collection of
151 globals which have to do with the wxMemStruct class.
154 class WXDLLEXPORT wxDebugContext
{
157 // Used to set alignment for markers.
158 static size_t CalcAlignment ();
160 // Returns the amount of padding needed after something of the given
161 // size. This is so that when we cast pointers backwards and forwards
162 // the pointer value will be valid for a wxMarkerType.
163 static size_t GetPadding (const size_t size
) ;
165 // Traverse the list.
166 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
168 static streambuf
*m_streamBuf
;
169 static ostream
*m_debugStream
;
171 static int debugLevel
;
175 // Set a checkpoint to dump only the memory from
177 static wxMemStruct
*checkPoint
;
179 wxDebugContext(void);
180 ~wxDebugContext(void);
182 static bool HasStream(void) { return (m_debugStream
!= NULL
); };
183 static ostream
& GetStream(void) { return *m_debugStream
; }
184 static streambuf
*GetStreamBuf(void) { return m_streamBuf
; }
185 static void SetStream(ostream
*stream
, streambuf
*buf
= NULL
);
186 static bool SetFile(const wxString
& file
);
187 static bool SetStandardError(void);
189 static int GetLevel(void) { return debugLevel
; }
190 static void SetLevel(int level
) { debugLevel
= level
; }
192 static bool GetDebugMode(void) { return debugOn
; }
193 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
195 static void SetCheckpoint(bool all
= FALSE
);
196 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
198 // Calculated from the request size and any padding needed
199 // before the final marker.
200 static size_t PaddedSize (const size_t reqSize
);
202 // Calc the total amount of space we need from the system
203 // to satisfy a caller request. This includes all padding.
204 static size_t TotSize (const size_t reqSize
);
206 // Return valid pointers to offsets within the allocated memory.
207 static char * StructPos (const char * buf
);
208 static char * MidMarkerPos (const char * buf
);
209 static char * CallerMemPos (const char * buf
);
210 static char * EndMarkerPos (const char * buf
, const size_t size
);
212 // Given a pointer to the start of the caller requested area
213 // return a pointer to the start of the entire alloc\'d buffer.
214 static char * StartPos (const char * caller
);
216 // Access to the list.
217 static wxMemStruct
* GetHead () { return m_head
; }
218 static wxMemStruct
* GetTail () { return m_tail
; }
220 // Set the list sentinals.
221 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
222 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
224 // If this is set then every new operation checks the validity
225 // of the all previous nodes in the list.
226 static bool GetCheckPrevious () { return m_checkPrevious
; }
227 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
229 // Checks all nodes, or all nodes if checkAll is TRUE
230 static int Check(bool checkAll
= FALSE
);
232 // Print out the list of wxMemStruct nodes.
233 static bool PrintList(void);
236 static bool Dump(void);
239 static bool PrintStatistics(bool detailed
= TRUE
);
241 // Print out the classes in the application.
242 static bool PrintClasses(void);
244 // Count the number of non-wxDebugContext-related objects
245 // that are outstanding
246 static int CountObjectsLeft(void);
249 // Store these here to allow access to the list without
250 // needing to have a wxMemStruct object.
251 static wxMemStruct
* m_head
;
252 static wxMemStruct
* m_tail
;
254 // Set to FALSE if we're not checking all previous nodes when
255 // we do a new. Set to TRUE when we are.
256 static bool m_checkPrevious
;
259 // Output a debug mess., in a system dependent fashion.
260 void WXDLLEXPORT
wxTrace(const char *fmt
...);
261 void WXDLLEXPORT
wxTraceLevel(int level
, const char *fmt
...);
263 #define WXTRACE wxTrace
264 #define WXTRACELEVEL wxTraceLevel
266 #else // else part for the #if WXDEBUG
268 inline void wxTrace(const char *WXUNUSED(fmt
)) {}
269 inline void wxTraceLevel(int WXUNUSED(level
), const char *WXUNUSED(fmt
)) {}
271 #define WXTRACE TRUE ? (void)0 : wxTrace
272 #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
273 #define WXDEBUG_NEW new