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 (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
31 // N.B. BC++ doesn't have istream.h, ostream.h
32 # include <iostream.h>
40 #include "wx/string.h"
44 void * wxDebugAlloc(size_t size
, char * fileName
, int lineNum
, bool isObject
, bool isVect
= FALSE
);
45 void wxDebugFree(void * buf
, bool isVect
= FALSE
);
47 // Global versions of the new and delete operators.
48 #if wxUSE_GLOBAL_MEMORY_OPERATORS
50 // Undefine temporarily (new is #defined in object.h) because we want to
51 // declare some new operators.
56 void * operator new (size_t size
, char * fileName
, int lineNum
);
57 void operator delete (void * buf
);
61 void operator delete(void *buf
, char*, int);
64 #if !( defined (_MSC_VER) && (_MSC_VER <= 1020) )
65 void * operator new[] (size_t size
, char * fileName
, int lineNum
);
66 void operator delete[] (void * buf
);
72 typedef unsigned int wxMarkerType
;
75 Define the struct which will be placed at the start of all dynamically
79 class WXDLLEXPORT wxMemStruct
{
81 friend class WXDLLEXPORT wxDebugContext
; // access to the m_next pointer for list traversal.
87 size_t RequestSize () { return m_reqSize
; }
88 wxMarkerType
Marker () { return m_firstMarker
; }
90 // When an object is deleted we set the id slot to a specific value.
91 inline void SetDeleted ();
92 inline int IsDeleted ();
97 // Used to determine if the object is really a wxMemStruct.
98 // Not a foolproof test by any means, but better than none I hope!
101 // Do all validation on a node.
104 // Check the integrity of a node and of the list, node by node.
106 int CheckAllPrevious ();
108 // Print a single node.
111 // Called when the memory linking functions get an error.
112 void ErrorMsg (const char *);
115 inline void *GetActualData(void) const { return m_actualData
; }
120 // Check for underwriting. There are 2 of these checks. This one
121 // inside the struct and another right after the struct.
122 wxMarkerType m_firstMarker
;
124 // File name and line number are from cpp.
128 // The amount of memory requested by the caller.
131 // Used to try to verify that we really are dealing with an object
132 // of the required class. Can be 1 of 2 values these indicating a valid
133 // wxMemStruct object, or a deleted wxMemStruct object.
136 wxMemStruct
* m_prev
;
137 wxMemStruct
* m_next
;
144 typedef void (wxMemStruct::*PmSFV
) ();
148 Debugging class. This will only have a single instance, but it\'s
149 a reasonable way to keep everything together and to make this
150 available for change if needed by someone else.
151 A lot of this stuff would be better off within the wxMemStruct class, but
152 it\'s stuff which we need to access at times when there is no wxMemStruct
153 object so we use this class instead. Think of it as a collection of
154 globals which have to do with the wxMemStruct class.
157 class WXDLLEXPORT wxDebugContext
{
160 // Used to set alignment for markers.
161 static size_t CalcAlignment ();
163 // Returns the amount of padding needed after something of the given
164 // size. This is so that when we cast pointers backwards and forwards
165 // the pointer value will be valid for a wxMarkerType.
166 static size_t GetPadding (const size_t size
) ;
168 // Traverse the list.
169 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
171 static streambuf
*m_streamBuf
;
172 static ostream
*m_debugStream
;
174 static int debugLevel
;
178 // Set a checkpoint to dump only the memory from
180 static wxMemStruct
*checkPoint
;
182 wxDebugContext(void);
183 ~wxDebugContext(void);
185 static bool HasStream(void) { return (m_debugStream
!= NULL
); };
186 static ostream
& GetStream(void) { return *m_debugStream
; }
187 static streambuf
*GetStreamBuf(void) { return m_streamBuf
; }
188 static void SetStream(ostream
*stream
, streambuf
*buf
= NULL
);
189 static bool SetFile(const wxString
& file
);
190 static bool SetStandardError(void);
192 static int GetLevel(void) { return debugLevel
; }
193 static void SetLevel(int level
) { debugLevel
= level
; }
195 static bool GetDebugMode(void) { return debugOn
; }
196 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
198 static void SetCheckpoint(bool all
= FALSE
);
199 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
201 // Calculated from the request size and any padding needed
202 // before the final marker.
203 static size_t PaddedSize (const size_t reqSize
);
205 // Calc the total amount of space we need from the system
206 // to satisfy a caller request. This includes all padding.
207 static size_t TotSize (const size_t reqSize
);
209 // Return valid pointers to offsets within the allocated memory.
210 static char * StructPos (const char * buf
);
211 static char * MidMarkerPos (const char * buf
);
212 static char * CallerMemPos (const char * buf
);
213 static char * EndMarkerPos (const char * buf
, const size_t size
);
215 // Given a pointer to the start of the caller requested area
216 // return a pointer to the start of the entire alloc\'d buffer.
217 static char * StartPos (const char * caller
);
219 // Access to the list.
220 static wxMemStruct
* GetHead () { return m_head
; }
221 static wxMemStruct
* GetTail () { return m_tail
; }
223 // Set the list sentinals.
224 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
225 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
227 // If this is set then every new operation checks the validity
228 // of the all previous nodes in the list.
229 static bool GetCheckPrevious () { return m_checkPrevious
; }
230 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
232 // Checks all nodes, or all nodes if checkAll is TRUE
233 static int Check(bool checkAll
= FALSE
);
235 // Print out the list of wxMemStruct nodes.
236 static bool PrintList(void);
239 static bool Dump(void);
242 static bool PrintStatistics(bool detailed
= TRUE
);
244 // Print out the classes in the application.
245 static bool PrintClasses(void);
247 // Count the number of non-wxDebugContext-related objects
248 // that are outstanding
249 static int CountObjectsLeft(void);
252 // Store these here to allow access to the list without
253 // needing to have a wxMemStruct object.
254 static wxMemStruct
* m_head
;
255 static wxMemStruct
* m_tail
;
257 // Set to FALSE if we're not checking all previous nodes when
258 // we do a new. Set to TRUE when we are.
259 static bool m_checkPrevious
;
262 // Output a debug message, in a system dependent fashion.
263 void WXDLLEXPORT
wxTrace(const char *fmt
...);
264 void WXDLLEXPORT
wxTraceLevel(int level
, const char *fmt
...);
266 #define WXTRACE wxTrace
267 #define WXTRACELEVEL wxTraceLevel
269 #else // else part for the #if __WXDEBUG__
271 inline void wxTrace(const char *WXUNUSED(fmt
)) {}
272 inline void wxTraceLevel(int WXUNUSED(level
), const char *WXUNUSED(fmt
)) {}
274 #define WXTRACE TRUE ? (void)0 : wxTrace
275 #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
276 // #define WXDEBUG_NEW new
278 #endif // __WXDEBUG__