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 (DEBUG && 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 void * operator new[] (size_t size
, char * fileName
, int lineNum
);
60 void operator delete[] (void * buf
);
62 #define new WXDEBUG_NEW
67 typedef unsigned int wxMarkerType
;
70 Define the struct which will be placed at the start of all dynamically
74 class WXDLLEXPORT wxMemStruct
{
76 friend class WXDLLEXPORT wxDebugContext
; // access to the m_next pointer for list traversal.
82 size_t RequestSize () { return m_reqSize
; }
83 wxMarkerType
Marker () { return m_firstMarker
; }
85 // When an object is deleted we set the id slot to a specific value.
86 inline void SetDeleted ();
87 inline int IsDeleted ();
92 // Used to determine if the object is really a wxMemStruct.
93 // Not a foolproof test by any means, but better than none I hope!
96 // Do all validation on a node.
99 // Check the integrity of a node and of the list, node by node.
101 int CheckAllPrevious ();
103 // Print a single node.
106 // Called when the memory linking functions get an error.
107 void ErrorMsg (const char *);
110 inline void *GetActualData(void) const { return m_actualData
; }
115 // Check for underwriting. There are 2 of these checks. This one
116 // inside the struct and another right after the struct.
117 wxMarkerType m_firstMarker
;
119 // File name and line number are from cpp.
123 // The amount of memory requested by the caller.
126 // Used to try to verify that we really are dealing with an object
127 // of the required class. Can be 1 of 2 values these indicating a valid
128 // wxMemStruct object, or a deleted wxMemStruct object.
131 wxMemStruct
* m_prev
;
132 wxMemStruct
* m_next
;
139 typedef void (wxMemStruct::*PmSFV
) ();
143 Debugging class. This will only have a single instance, but it\'s
144 a reasonable way to keep everything together and to make this
145 available for change if needed by someone else.
146 A lot of this stuff would be better off within the wxMemStruct class, but
147 it\'s stuff which we need to access at times when there is no wxMemStruct
148 object so we use this class instead. Think of it as a collection of
149 globals which have to do with the wxMemStruct class.
152 class WXDLLEXPORT wxDebugContext
{
155 // Used to set alignment for markers.
156 static size_t CalcAlignment ();
158 // Returns the amount of padding needed after something of the given
159 // size. This is so that when we cast pointers backwards and forwards
160 // the pointer value will be valid for a wxMarkerType.
161 static size_t GetPadding (const size_t size
) ;
163 // Traverse the list.
164 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
166 static streambuf
*m_streamBuf
;
167 static ostream
*m_debugStream
;
169 static int debugLevel
;
173 // Set a checkpoint to dump only the memory from
175 static wxMemStruct
*checkPoint
;
177 wxDebugContext(void);
178 ~wxDebugContext(void);
180 static bool HasStream(void) { return (m_debugStream
!= NULL
); };
181 static ostream
& GetStream(void) { return *m_debugStream
; }
182 static streambuf
*GetStreamBuf(void) { return m_streamBuf
; }
183 static void SetStream(ostream
*stream
, streambuf
*buf
= NULL
);
184 static bool SetFile(const wxString
& file
);
185 static bool SetStandardError(void);
187 static int GetLevel(void) { return debugLevel
; }
188 static void SetLevel(int level
) { debugLevel
= level
; }
190 static bool GetDebugMode(void) { return debugOn
; }
191 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
193 static void SetCheckpoint(bool all
= FALSE
);
194 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
196 // Calculated from the request size and any padding needed
197 // before the final marker.
198 static size_t PaddedSize (const size_t reqSize
);
200 // Calc the total amount of space we need from the system
201 // to satisfy a caller request. This includes all padding.
202 static size_t TotSize (const size_t reqSize
);
204 // Return valid pointers to offsets within the allocated memory.
205 static char * StructPos (const char * buf
);
206 static char * MidMarkerPos (const char * buf
);
207 static char * CallerMemPos (const char * buf
);
208 static char * EndMarkerPos (const char * buf
, const size_t size
);
210 // Given a pointer to the start of the caller requested area
211 // return a pointer to the start of the entire alloc\'d buffer.
212 static char * StartPos (const char * caller
);
214 // Access to the list.
215 static wxMemStruct
* GetHead () { return m_head
; }
216 static wxMemStruct
* GetTail () { return m_tail
; }
218 // Set the list sentinals.
219 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
220 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
222 // If this is set then every new operation checks the validity
223 // of the all previous nodes in the list.
224 static bool GetCheckPrevious () { return m_checkPrevious
; }
225 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
227 // Checks all nodes, or all nodes if checkAll is TRUE
228 static int Check(bool checkAll
= FALSE
);
230 // Print out the list of wxMemStruct nodes.
231 static bool PrintList(void);
234 static bool Dump(void);
237 static bool PrintStatistics(bool detailed
= TRUE
);
239 // Print out the classes in the application.
240 static bool PrintClasses(void);
242 // Count the number of non-wxDebugContext-related objects
243 // that are outstanding
244 static int CountObjectsLeft(void);
247 // Store these here to allow access to the list without
248 // needing to have a wxMemStruct object.
249 static wxMemStruct
* m_head
;
250 static wxMemStruct
* m_tail
;
252 // Set to FALSE if we're not checking all previous nodes when
253 // we do a new. Set to TRUE when we are.
254 static bool m_checkPrevious
;
257 // Output a debug mess., in a system dependent fashion.
258 void WXDLLEXPORT
wxTrace(const char *fmt
...);
259 void WXDLLEXPORT
wxTraceLevel(int level
, const char *fmt
...);
261 #define WXTRACE wxTrace
262 #define WXTRACELEVEL wxTraceLevel
264 #else // else part for the #if DEBUG
266 inline void wxTrace(const char *WXUNUSED(fmt
)) {}
267 inline void wxTraceLevel(int WXUNUSED(level
), const char *WXUNUSED(fmt
)) {}
269 #define WXTRACE TRUE ? (void)0 : wxTrace
270 #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
271 #define WXDEBUG_NEW new