1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Memory operations
4 // Author: Arthur Seaton, Julian Smart
7 // Copyright: (c) 1998 Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
15 #include "wx/string.h"
16 #include "wx/msgout.h"
18 #if wxUSE_MEMORY_TRACING || wxUSE_DEBUG_CONTEXT
22 WXDLLIMPEXP_BASE
void * wxDebugAlloc(size_t size
, wxChar
* fileName
, int lineNum
, bool isObject
, bool isVect
= false);
23 WXDLLIMPEXP_BASE
void wxDebugFree(void * buf
, bool isVect
= false);
25 //**********************************************************************************
27 The global operator new used for everything apart from getting
28 dynamic storage within this function itself.
31 // We'll only do malloc and free for the moment: leave the interesting
32 // stuff for the wxObject versions.
35 #if wxUSE_GLOBAL_MEMORY_OPERATORS
37 // Undefine temporarily (new is #defined in object.h) because we want to
38 // declare some new operators.
43 #if defined(__SUNCC__)
44 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
45 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) )
46 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
47 #elif defined (__SGI_CC_)
48 // only supported by -n32 compilers
49 #ifndef __EDG_ABI_COMPATIBILITY_VERSION
50 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
52 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) )
53 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
55 // ::operator new[] is a recent C++ feature, so assume it's not supported
56 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
59 // devik 2000-8-29: All new/delete ops are now inline because they can't
60 // be marked as dllexport/dllimport. It then leads to weird bugs when
62 #if defined(__WINDOWS__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
63 inline void * operator new (size_t size
, wxChar
* fileName
, int lineNum
)
65 return wxDebugAlloc(size
, fileName
, lineNum
, false, false);
68 inline void * operator new (size_t size
)
70 return wxDebugAlloc(size
, NULL
, 0, false);
73 inline void operator delete (void * buf
)
75 wxDebugFree(buf
, false);
78 #if wxUSE_ARRAY_MEMORY_OPERATORS
79 inline void * operator new[] (size_t size
)
81 return wxDebugAlloc(size
, NULL
, 0, false, true);
84 inline void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
)
86 return wxDebugAlloc(size
, fileName
, lineNum
, false, true);
89 inline void operator delete[] (void * buf
)
91 wxDebugFree(buf
, true);
93 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
97 void * operator new (size_t size
, wxChar
* fileName
, int lineNum
);
99 void * operator new (size_t size
);
101 void operator delete (void * buf
);
103 #if wxUSE_ARRAY_MEMORY_OPERATORS
104 void * operator new[] (size_t size
);
106 void * operator new[] (size_t size
, wxChar
* fileName
, int lineNum
);
108 void operator delete[] (void * buf
);
109 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
110 #endif // defined(__WINDOWS__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
113 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) )
114 inline void operator delete(void* pData
, wxChar
* /* fileName */, int /* lineNum */)
116 wxDebugFree(pData
, false);
118 inline void operator delete[](void* pData
, wxChar
* /* fileName */, int /* lineNum */)
120 wxDebugFree(pData
, true);
122 #endif // __VISUALC__>=1200
123 #endif // wxUSE_GLOBAL_MEMORY_OPERATORS
125 //**********************************************************************************
127 typedef unsigned int wxMarkerType
;
130 Define the struct which will be placed at the start of all dynamically
134 class WXDLLIMPEXP_BASE wxMemStruct
{
136 friend class WXDLLIMPEXP_FWD_BASE wxDebugContext
; // access to the m_next pointer for list traversal.
142 size_t RequestSize () { return m_reqSize
; }
143 wxMarkerType
Marker () { return m_firstMarker
; }
145 // When an object is deleted we set the id slot to a specific value.
146 inline void SetDeleted ();
147 inline int IsDeleted ();
152 // Used to determine if the object is really a wxMemStruct.
153 // Not a foolproof test by any means, but better than none I hope!
156 // Do all validation on a node.
159 // Check the integrity of a node and of the list, node by node.
161 int CheckAllPrevious ();
163 // Print a single node.
166 // Called when the memory linking functions get an error.
167 void ErrorMsg (const char *);
170 inline void *GetActualData(void) const { return m_actualData
; }
175 // Check for underwriting. There are 2 of these checks. This one
176 // inside the struct and another right after the struct.
177 wxMarkerType m_firstMarker
;
179 // File name and line number are from cpp.
183 // The amount of memory requested by the caller.
186 // Used to try to verify that we really are dealing with an object
187 // of the required class. Can be 1 of 2 values these indicating a valid
188 // wxMemStruct object, or a deleted wxMemStruct object.
191 wxMemStruct
* m_prev
;
192 wxMemStruct
* m_next
;
199 typedef void (wxMemStruct::*PmSFV
) ();
201 // Type of the app function that can be installed and called at wxWidgets shutdown
202 // (after all other registered files with global destructors have been closed down).
203 typedef void (*wxShutdownNotifyFunction
)();
206 Debugging class. This will only have a single instance, but it's
207 a reasonable way to keep everything together and to make this
208 available for change if needed by someone else.
209 A lot of this stuff would be better off within the wxMemStruct class, but
210 it's stuff which we need to access at times when there is no wxMemStruct
211 object so we use this class instead. Think of it as a collection of
212 globals which have to do with the wxMemStruct class.
215 class WXDLLIMPEXP_BASE wxDebugContext
{
218 // Used to set alignment for markers.
219 static size_t CalcAlignment ();
221 // Returns the amount of padding needed after something of the given
222 // size. This is so that when we cast pointers backwards and forwards
223 // the pointer value will be valid for a wxMarkerType.
224 static size_t GetPadding (const size_t size
) ;
226 // Traverse the list.
227 static void TraverseList (PmSFV
, wxMemStruct
*from
= NULL
);
229 static int debugLevel
;
232 static int m_balign
; // byte alignment
233 static int m_balignmask
; // mask for performing byte alignment
235 // Set a checkpoint to dump only the memory from
237 static wxMemStruct
*checkPoint
;
239 wxDebugContext(void);
240 ~wxDebugContext(void);
242 static int GetLevel(void) { return debugLevel
; }
243 static void SetLevel(int level
) { debugLevel
= level
; }
245 static bool GetDebugMode(void) { return debugOn
; }
246 static void SetDebugMode(bool flag
) { debugOn
= flag
; }
248 static void SetCheckpoint(bool all
= false);
249 static wxMemStruct
*GetCheckpoint(void) { return checkPoint
; }
251 // Calculated from the request size and any padding needed
252 // before the final marker.
253 static size_t PaddedSize (const size_t reqSize
);
255 // Calc the total amount of space we need from the system
256 // to satisfy a caller request. This includes all padding.
257 static size_t TotSize (const size_t reqSize
);
259 // Return valid pointers to offsets within the allocated memory.
260 static char * StructPos (const char * buf
);
261 static char * MidMarkerPos (const char * buf
);
262 static char * CallerMemPos (const char * buf
);
263 static char * EndMarkerPos (const char * buf
, const size_t size
);
265 // Given a pointer to the start of the caller requested area
266 // return a pointer to the start of the entire alloc\'d buffer.
267 static char * StartPos (const char * caller
);
269 // Access to the list.
270 static wxMemStruct
* GetHead () { return m_head
; }
271 static wxMemStruct
* GetTail () { return m_tail
; }
273 // Set the list sentinals.
274 static wxMemStruct
* SetHead (wxMemStruct
* st
) { return (m_head
= st
); }
275 static wxMemStruct
* SetTail (wxMemStruct
* st
) { return (m_tail
= st
); }
277 // If this is set then every new operation checks the validity
278 // of the all previous nodes in the list.
279 static bool GetCheckPrevious () { return m_checkPrevious
; }
280 static void SetCheckPrevious (bool value
) { m_checkPrevious
= value
; }
282 // Checks all nodes, or all nodes if checkAll is true
283 static int Check(bool checkAll
= false);
285 // Print out the list of wxMemStruct nodes.
286 static bool PrintList(void);
289 static bool Dump(void);
292 static bool PrintStatistics(bool detailed
= true);
294 // Print out the classes in the application.
295 static bool PrintClasses(void);
297 // Count the number of non-wxDebugContext-related objects
298 // that are outstanding
299 static int CountObjectsLeft(bool sinceCheckpoint
= false);
301 // This function is used to output the dump
302 static void OutputDumpLine(const wxChar
*szFormat
, ...);
304 static void SetShutdownNotifyFunction(wxShutdownNotifyFunction shutdownFn
);
307 // Store these here to allow access to the list without
308 // needing to have a wxMemStruct object.
309 static wxMemStruct
* m_head
;
310 static wxMemStruct
* m_tail
;
312 // Set to false if we're not checking all previous nodes when
313 // we do a new. Set to true when we are.
314 static bool m_checkPrevious
;
316 // Holds a pointer to an optional application function to call at shutdown.
317 static wxShutdownNotifyFunction sm_shutdownFn
;
319 // Have to access our shutdown hook
320 friend class wxDebugContextDumpDelayCounter
;
323 // Final cleanup (e.g. deleting the log object and doing memory leak checking)
324 // will be delayed until all wxDebugContextDumpDelayCounter objects have been
325 // destructed. Adding one wxDebugContextDumpDelayCounter per file will delay
326 // memory leak checking until after destructing all global objects.
328 class WXDLLIMPEXP_BASE wxDebugContextDumpDelayCounter
331 wxDebugContextDumpDelayCounter();
332 ~wxDebugContextDumpDelayCounter();
339 // make leak dump after all globals have been destructed
340 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_File
;
341 #define WXDEBUG_DUMPDELAYCOUNTER \
342 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_Extra;
344 // Output a debug message, in a system dependent fashion.
345 void WXDLLIMPEXP_BASE
wxTrace(const wxChar
*fmt
...) WX_ATTRIBUTE_PRINTF_1
;
346 void WXDLLIMPEXP_BASE
wxTraceLevel(int level
, const wxChar
*fmt
...) WX_ATTRIBUTE_PRINTF_2
;
348 #define WXTRACE wxTrace
349 #define WXTRACELEVEL wxTraceLevel
351 #else // wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
353 #define WXDEBUG_DUMPDELAYCOUNTER
355 // Borland C++ Builder 6 seems to have troubles with inline functions (see bug
358 inline void wxTrace(const wxChar
*WXUNUSED(fmt
)) {}
359 inline void wxTraceLevel(int WXUNUSED(level
), const wxChar
*WXUNUSED(fmt
)) {}
362 #define wxTraceLevel(l, fmt)
365 #define WXTRACE true ? (void)0 : wxTrace
366 #define WXTRACELEVEL true ? (void)0 : wxTraceLevel
368 #endif // wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
370 #endif // _WX_MEMORY_H_