1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Memory operations
4 // Author: Arthur Seaton, Julian Smart
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #include "wx/string.h"
17 #include "wx/msgout.h"
19 #if wxUSE_MEMORY_TRACING || wxUSE_DEBUG_CONTEXT
23 WXDLLIMPEXP_BASE
void * wxDebugAlloc(size_t size
, wxChar
* fileName
, int lineNum
, bool isObject
, bool isVect
= false);
24 WXDLLIMPEXP_BASE
void wxDebugFree(void * buf
, bool isVect
= false);
26 //**********************************************************************************
28 The global operator new used for everything apart from getting
29 dynamic storage within this function itself.
32 // We'll only do malloc and free for the moment: leave the interesting
33 // stuff for the wxObject versions.
36 #if wxUSE_GLOBAL_MEMORY_OPERATORS
38 // Undefine temporarily (new is #defined in object.h) because we want to
39 // declare some new operators.
44 #if defined(__SUNCC__)
45 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
46 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
47 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
48 #elif defined (__SGI_CC_)
49 // only supported by -n32 compilers
50 #ifndef __EDG_ABI_COMPATIBILITY_VERSION
51 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
53 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
54 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
56 // ::operator new[] is a recent C++ feature, so assume it's not supported
57 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
60 // devik 2000-8-29: All new/delete ops are now inline because they can't
61 // be marked as dllexport/dllimport. It then leads to weird bugs when
63 #if defined(__WXMSW__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
64 inline void * operator new (size_t size
, wxChar
* fileName
, int lineNum
)
66 return wxDebugAlloc(size
, fileName
, lineNum
, false, false);
69 inline void * operator new (size_t size
)
71 return wxDebugAlloc(size
, NULL
, 0, false);
74 inline void operator delete (void * buf
)
76 wxDebugFree(buf
, false);
79 #if wxUSE_ARRAY_MEMORY_OPERATORS
80 inline void * operator new[] (size_t size
)
82 return wxDebugAlloc(size
, NULL
, 0, false, true);
85 inline void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
87 return wxDebugAlloc(size
, fileName
, lineNum
, false, true);
90 inline void operator delete[] (void * buf
)
92 wxDebugFree(buf
, true);
94 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
98 void * operator new (size_t size
, wxChar
* fileName
, int lineNum
);
100 void * operator new (size_t size
);
102 void operator delete (void * buf
);
104 #if wxUSE_ARRAY_MEMORY_OPERATORS
105 void * operator new[] (size_t size
);
107 void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
);
109 void operator delete[] (void * buf
);
110 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
111 #endif // defined(__WXMSW__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
113 // VC++ 6.0 and MWERKS
114 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || defined(__MWERKS__)
115 inline void operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
117 wxDebugFree(pData
, false);
119 inline void operator delete[](void* pData
, wxChar
* /* fileName */, int /* lineNum */)
121 wxDebugFree(pData
, true);
123 #endif // __VISUALC__>=1200
124 #endif // wxUSE_GLOBAL_MEMORY_OPERATORS
126 //**********************************************************************************
128 typedef unsigned int wxMarkerType
;
131 Define the struct which will be placed at the start of all dynamically
135 class WXDLLIMPEXP_BASE wxMemStruct
{
137 friend class WXDLLIMPEXP_FWD_BASE wxDebugContext
; // access to the m_next pointer for list traversal.
143 size_t RequestSize () { return m_reqSize
; }
144 wxMarkerType
Marker () { return m_firstMarker
; }
146 // When an object is deleted we set the id slot to a specific value.
147 inline void SetDeleted ();
148 inline int IsDeleted ();
153 // Used to determine if the object is really a wxMemStruct.
154 // Not a foolproof test by any means, but better than none I hope!
157 // Do all validation on a node.
160 // Check the integrity of a node and of the list, node by node.
162 int CheckAllPrevious ();
164 // Print a single node.
167 // Called when the memory linking functions get an error.
168 void ErrorMsg (const char *);
171 inline void *GetActualData(void) const { return m_actualData
; }
176 // Check for underwriting. There are 2 of these checks. This one
177 // inside the struct and another right after the struct.
178 wxMarkerType m_firstMarker
;
180 // File name and line number are from cpp.
184 // The amount of memory requested by the caller.
187 // Used to try to verify that we really are dealing with an object
188 // of the required class. Can be 1 of 2 values these indicating a valid
189 // wxMemStruct object, or a deleted wxMemStruct object.
192 wxMemStruct
* m_prev
;
193 wxMemStruct
* m_next
;
200 typedef void (wxMemStruct::*PmSFV
) ();
202 // Type of the app function that can be installed and called at wxWidgets shutdown
203 // (after all other registered files with global destructors have been closed down).
204 typedef void (*wxShutdownNotifyFunction
)();
207 Debugging class. This will only have a single instance, but it's
208 a reasonable way to keep everything together and to make this
209 available for change if needed by someone else.
210 A lot of this stuff would be better off within the wxMemStruct class, but
211 it's stuff which we need to access at times when there is no wxMemStruct
212 object so we use this class instead. Think of it as a collection of
213 globals which have to do with the wxMemStruct class.
216 class WXDLLIMPEXP_BASE wxDebugContext
{
219 // Used to set alignment for markers.
220 static size_t CalcAlignment ();
222 // Returns the amount of padding needed after something of the given
223 // size. This is so that when we cast pointers backwards and forwards
224 // the pointer value will be valid for a wxMarkerType.
225 static size_t GetPadding (const size_t size
) ;
227 // Traverse the list.
228 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
230 static int debugLevel
;
233 static int m_balign
; // byte alignment
234 static int m_balignmask
; // mask for performing byte alignment
236 // Set a checkpoint to dump only the memory from
238 static wxMemStruct
*checkPoint
;
240 wxDebugContext(void);
241 ~wxDebugContext(void);
243 static int GetLevel(void) { return debugLevel
; }
244 static void SetLevel(int level
) { debugLevel
= level
; }
246 static bool GetDebugMode(void) { return debugOn
; }
247 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
249 static void SetCheckpoint(bool all
= false);
250 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
252 // Calculated from the request size and any padding needed
253 // before the final marker.
254 static size_t PaddedSize (const size_t reqSize
);
256 // Calc the total amount of space we need from the system
257 // to satisfy a caller request. This includes all padding.
258 static size_t TotSize (const size_t reqSize
);
260 // Return valid pointers to offsets within the allocated memory.
261 static char * StructPos (const char * buf
);
262 static char * MidMarkerPos (const char * buf
);
263 static char * CallerMemPos (const char * buf
);
264 static char * EndMarkerPos (const char * buf
, const size_t size
);
266 // Given a pointer to the start of the caller requested area
267 // return a pointer to the start of the entire alloc\'d buffer.
268 static char * StartPos (const char * caller
);
270 // Access to the list.
271 static wxMemStruct
* GetHead () { return m_head
; }
272 static wxMemStruct
* GetTail () { return m_tail
; }
274 // Set the list sentinals.
275 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
276 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
278 // If this is set then every new operation checks the validity
279 // of the all previous nodes in the list.
280 static bool GetCheckPrevious () { return m_checkPrevious
; }
281 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
283 // Checks all nodes, or all nodes if checkAll is true
284 static int Check(bool checkAll
= false);
286 // Print out the list of wxMemStruct nodes.
287 static bool PrintList(void);
290 static bool Dump(void);
293 static bool PrintStatistics(bool detailed
= true);
295 // Print out the classes in the application.
296 static bool PrintClasses(void);
298 // Count the number of non-wxDebugContext-related objects
299 // that are outstanding
300 static int CountObjectsLeft(bool sinceCheckpoint
= false);
302 // This function is used to output the dump
303 static void OutputDumpLine(const wxChar
*szFormat
, ...);
305 static void SetShutdownNotifyFunction(wxShutdownNotifyFunction shutdownFn
);
308 // Store these here to allow access to the list without
309 // needing to have a wxMemStruct object.
310 static wxMemStruct
* m_head
;
311 static wxMemStruct
* m_tail
;
313 // Set to false if we're not checking all previous nodes when
314 // we do a new. Set to true when we are.
315 static bool m_checkPrevious
;
317 // Holds a pointer to an optional application function to call at shutdown.
318 static wxShutdownNotifyFunction sm_shutdownFn
;
320 // Have to access our shutdown hook
321 friend class wxDebugContextDumpDelayCounter
;
324 // Final cleanup (e.g. deleting the log object and doing memory leak checking)
325 // will be delayed until all wxDebugContextDumpDelayCounter objects have been
326 // destructed. Adding one wxDebugContextDumpDelayCounter per file will delay
327 // memory leak checking until after destructing all global objects.
329 class WXDLLIMPEXP_BASE wxDebugContextDumpDelayCounter
332 wxDebugContextDumpDelayCounter();
333 ~wxDebugContextDumpDelayCounter();
340 // make leak dump after all globals have been destructed
341 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_File
;
342 #define WXDEBUG_DUMPDELAYCOUNTER \
343 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_Extra;
345 // Output a debug message, in a system dependent fashion.
346 void WXDLLIMPEXP_BASE
wxTrace(const wxChar
*fmt
...) WX_ATTRIBUTE_PRINTF_1
;
347 void WXDLLIMPEXP_BASE
wxTraceLevel(int level
, const wxChar
*fmt
...) WX_ATTRIBUTE_PRINTF_2
;
349 #define WXTRACE wxTrace
350 #define WXTRACELEVEL wxTraceLevel
352 #else // wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
354 #define WXDEBUG_DUMPDELAYCOUNTER
356 // Borland C++ Builder 6 seems to have troubles with inline functions (see bug
359 inline void wxTrace(const wxChar
*WXUNUSED(fmt
)) {}
360 inline void wxTraceLevel(int WXUNUSED(level
), const wxChar
*WXUNUSED(fmt
)) {}
363 #define wxTraceLevel(l, fmt)
366 #define WXTRACE true ? (void)0 : wxTrace
367 #define WXTRACELEVEL true ? (void)0 : wxTraceLevel
369 #endif // wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
371 #endif // _WX_MEMORY_H_