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
);
72 void * operator new (size_t size
, wxChar
* fileName
, int lineNum
);
73 void operator delete (void * buf
);
75 #if wxUSE_ARRAY_MEMORY_OPERATORS
76 void * operator new[] (size_t size
);
77 void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
);
78 void operator delete[] (void * buf
);
82 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
83 void operator delete(void *buf
, wxChar
*, int);
84 void operator delete[](void *buf
, wxChar
*, int);
88 // wxUSE_GLOBAL_MEMORY_OPERATORS
92 typedef unsigned int wxMarkerType
;
95 Define the struct which will be placed at the start of all dynamically
99 class WXDLLEXPORT wxMemStruct
{
101 friend class WXDLLEXPORT wxDebugContext
; // access to the m_next pointer for list traversal.
107 size_t RequestSize () { return m_reqSize
; }
108 wxMarkerType
Marker () { return m_firstMarker
; }
110 // When an object is deleted we set the id slot to a specific value.
111 inline void SetDeleted ();
112 inline int IsDeleted ();
117 // Used to determine if the object is really a wxMemStruct.
118 // Not a foolproof test by any means, but better than none I hope!
121 // Do all validation on a node.
124 // Check the integrity of a node and of the list, node by node.
126 int CheckAllPrevious ();
128 // Print a single node.
131 // Called when the memory linking functions get an error.
132 void ErrorMsg (const char *);
135 inline void *GetActualData(void) const { return m_actualData
; }
140 // Check for underwriting. There are 2 of these checks. This one
141 // inside the struct and another right after the struct.
142 wxMarkerType m_firstMarker
;
144 // File name and line number are from cpp.
148 // The amount of memory requested by the caller.
151 // Used to try to verify that we really are dealing with an object
152 // of the required class. Can be 1 of 2 values these indicating a valid
153 // wxMemStruct object, or a deleted wxMemStruct object.
156 wxMemStruct
* m_prev
;
157 wxMemStruct
* m_next
;
164 typedef void (wxMemStruct::*PmSFV
) ();
168 Debugging class. This will only have a single instance, but it\'s
169 a reasonable way to keep everything together and to make this
170 available for change if needed by someone else.
171 A lot of this stuff would be better off within the wxMemStruct class, but
172 it\'s stuff which we need to access at times when there is no wxMemStruct
173 object so we use this class instead. Think of it as a collection of
174 globals which have to do with the wxMemStruct class.
177 class WXDLLEXPORT wxDebugContext
{
180 // Used to set alignment for markers.
181 static size_t CalcAlignment ();
183 // Returns the amount of padding needed after something of the given
184 // size. This is so that when we cast pointers backwards and forwards
185 // the pointer value will be valid for a wxMarkerType.
186 static size_t GetPadding (const size_t size
) ;
188 // Traverse the list.
189 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
191 static streambuf
*m_streamBuf
;
192 static ostream
*m_debugStream
;
194 static int debugLevel
;
198 // Set a checkpoint to dump only the memory from
200 static wxMemStruct
*checkPoint
;
202 wxDebugContext(void);
203 ~wxDebugContext(void);
205 static bool HasStream(void) { return (m_debugStream
!= NULL
); };
206 static ostream
& GetStream(void) { return *m_debugStream
; }
207 static streambuf
*GetStreamBuf(void) { return m_streamBuf
; }
208 static void SetStream(ostream
*stream
, streambuf
*buf
= NULL
);
209 static bool SetFile(const wxString
& file
);
210 static bool SetStandardError(void);
212 static int GetLevel(void) { return debugLevel
; }
213 static void SetLevel(int level
) { debugLevel
= level
; }
215 static bool GetDebugMode(void) { return debugOn
; }
216 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
218 static void SetCheckpoint(bool all
= FALSE
);
219 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
221 // Calculated from the request size and any padding needed
222 // before the final marker.
223 static size_t PaddedSize (const size_t reqSize
);
225 // Calc the total amount of space we need from the system
226 // to satisfy a caller request. This includes all padding.
227 static size_t TotSize (const size_t reqSize
);
229 // Return valid pointers to offsets within the allocated memory.
230 static char * StructPos (const char * buf
);
231 static char * MidMarkerPos (const char * buf
);
232 static char * CallerMemPos (const char * buf
);
233 static char * EndMarkerPos (const char * buf
, const size_t size
);
235 // Given a pointer to the start of the caller requested area
236 // return a pointer to the start of the entire alloc\'d buffer.
237 static char * StartPos (const char * caller
);
239 // Access to the list.
240 static wxMemStruct
* GetHead () { return m_head
; }
241 static wxMemStruct
* GetTail () { return m_tail
; }
243 // Set the list sentinals.
244 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
245 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
247 // If this is set then every new operation checks the validity
248 // of the all previous nodes in the list.
249 static bool GetCheckPrevious () { return m_checkPrevious
; }
250 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
252 // Checks all nodes, or all nodes if checkAll is TRUE
253 static int Check(bool checkAll
= FALSE
);
255 // Print out the list of wxMemStruct nodes.
256 static bool PrintList(void);
259 static bool Dump(void);
262 static bool PrintStatistics(bool detailed
= TRUE
);
264 // Print out the classes in the application.
265 static bool PrintClasses(void);
267 // Count the number of non-wxDebugContext-related objects
268 // that are outstanding
269 static int CountObjectsLeft(bool sinceCheckpoint
= FALSE
);
272 // Store these here to allow access to the list without
273 // needing to have a wxMemStruct object.
274 static wxMemStruct
* m_head
;
275 static wxMemStruct
* m_tail
;
277 // Set to FALSE if we're not checking all previous nodes when
278 // we do a new. Set to TRUE when we are.
279 static bool m_checkPrevious
;
282 // Output a debug message, in a system dependent fashion.
283 void WXDLLEXPORT
wxTrace(const wxChar
*fmt
...);
284 void WXDLLEXPORT
wxTraceLevel(int level
, const wxChar
*fmt
...);
286 #define WXTRACE wxTrace
287 #define WXTRACELEVEL wxTraceLevel
289 #else // else part for the #if __WXDEBUG__
291 inline void wxTrace(const wxChar
*WXUNUSED(fmt
)) {}
292 inline void wxTraceLevel(int WXUNUSED(level
), const wxChar
*WXUNUSED(fmt
)) {}
294 #define WXTRACE TRUE ? (void)0 : wxTrace
295 #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
296 // #define WXDEBUG_NEW new
298 #endif // __WXDEBUG__