]> git.saurik.com Git - wxWidgets.git/blob - include/wx/memory.h
Add wxActivateEvent::GetActivationReason().
[wxWidgets.git] / include / wx / memory.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/memory.h
3 // Purpose: Memory operations
4 // Author: Arthur Seaton, Julian Smart
5 // Modified by:
6 // Created: 29/01/98
7 // Copyright: (c) 1998 Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_MEMORY_H_
12 #define _WX_MEMORY_H_
13
14 #include "wx/defs.h"
15 #include "wx/string.h"
16 #include "wx/msgout.h"
17
18 #if wxUSE_MEMORY_TRACING || wxUSE_DEBUG_CONTEXT
19
20 #include <stddef.h>
21
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);
24
25 //**********************************************************************************
26 /*
27 The global operator new used for everything apart from getting
28 dynamic storage within this function itself.
29 */
30
31 // We'll only do malloc and free for the moment: leave the interesting
32 // stuff for the wxObject versions.
33
34
35 #if wxUSE_GLOBAL_MEMORY_OPERATORS
36
37 // Undefine temporarily (new is #defined in object.h) because we want to
38 // declare some new operators.
39 #ifdef new
40 #undef new
41 #endif
42
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
51 #endif
52 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) )
53 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
54 #else
55 // ::operator new[] is a recent C++ feature, so assume it's not supported
56 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
57 #endif
58
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
61 // used on MSW as DLL
62 #if defined(__WINDOWS__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
63 inline void * operator new (size_t size, wxChar * fileName, int lineNum)
64 {
65 return wxDebugAlloc(size, fileName, lineNum, false, false);
66 }
67
68 inline void * operator new (size_t size)
69 {
70 return wxDebugAlloc(size, NULL, 0, false);
71 }
72
73 inline void operator delete (void * buf)
74 {
75 wxDebugFree(buf, false);
76 }
77
78 #if wxUSE_ARRAY_MEMORY_OPERATORS
79 inline void * operator new[] (size_t size)
80 {
81 return wxDebugAlloc(size, NULL, 0, false, true);
82 }
83
84 inline void * operator new[] (size_t size, wxChar * fileName, int lineNum)
85 {
86 return wxDebugAlloc(size, fileName, lineNum, false, true);
87 }
88
89 inline void operator delete[] (void * buf)
90 {
91 wxDebugFree(buf, true);
92 }
93 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
94
95 #else
96
97 void * operator new (size_t size, wxChar * fileName, int lineNum);
98
99 void * operator new (size_t size);
100
101 void operator delete (void * buf);
102
103 #if wxUSE_ARRAY_MEMORY_OPERATORS
104 void * operator new[] (size_t size);
105
106 void * operator new[] (size_t size, wxChar * fileName, int lineNum);
107
108 void operator delete[] (void * buf);
109 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
110 #endif // defined(__WINDOWS__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
111
112 // VC++ 6.0
113 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) )
114 inline void operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */)
115 {
116 wxDebugFree(pData, false);
117 }
118 inline void operator delete[](void* pData, wxChar* /* fileName */, int /* lineNum */)
119 {
120 wxDebugFree(pData, true);
121 }
122 #endif // __VISUALC__>=1200
123 #endif // wxUSE_GLOBAL_MEMORY_OPERATORS
124
125 //**********************************************************************************
126
127 typedef unsigned int wxMarkerType;
128
129 /*
130 Define the struct which will be placed at the start of all dynamically
131 allocated memory.
132 */
133
134 class WXDLLIMPEXP_BASE wxMemStruct {
135
136 friend class WXDLLIMPEXP_FWD_BASE wxDebugContext; // access to the m_next pointer for list traversal.
137
138 public:
139 public:
140 int AssertList ();
141
142 size_t RequestSize () { return m_reqSize; }
143 wxMarkerType Marker () { return m_firstMarker; }
144
145 // When an object is deleted we set the id slot to a specific value.
146 inline void SetDeleted ();
147 inline int IsDeleted ();
148
149 int Append ();
150 int Unlink ();
151
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!
154 int AssertIt ();
155
156 // Do all validation on a node.
157 int ValidateNode ();
158
159 // Check the integrity of a node and of the list, node by node.
160 int CheckBlock ();
161 int CheckAllPrevious ();
162
163 // Print a single node.
164 void PrintNode ();
165
166 // Called when the memory linking functions get an error.
167 void ErrorMsg (const char *);
168 void ErrorMsg ();
169
170 inline void *GetActualData(void) const { return m_actualData; }
171
172 void Dump(void);
173
174 public:
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;
178
179 // File name and line number are from cpp.
180 wxChar* m_fileName;
181 int m_lineNum;
182
183 // The amount of memory requested by the caller.
184 size_t m_reqSize;
185
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.
189 wxMarkerType m_id;
190
191 wxMemStruct * m_prev;
192 wxMemStruct * m_next;
193
194 void * m_actualData;
195 bool m_isObject;
196 };
197
198
199 typedef void (wxMemStruct::*PmSFV) ();
200
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)();
204
205 /*
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.
213 */
214
215 class WXDLLIMPEXP_BASE wxDebugContext {
216
217 protected:
218 // Used to set alignment for markers.
219 static size_t CalcAlignment ();
220
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) ;
225
226 // Traverse the list.
227 static void TraverseList (PmSFV, wxMemStruct *from = NULL);
228
229 static int debugLevel;
230 static bool debugOn;
231
232 static int m_balign; // byte alignment
233 static int m_balignmask; // mask for performing byte alignment
234 public:
235 // Set a checkpoint to dump only the memory from
236 // a given point
237 static wxMemStruct *checkPoint;
238
239 wxDebugContext(void);
240 ~wxDebugContext(void);
241
242 static int GetLevel(void) { return debugLevel; }
243 static void SetLevel(int level) { debugLevel = level; }
244
245 static bool GetDebugMode(void) { return debugOn; }
246 static void SetDebugMode(bool flag) { debugOn = flag; }
247
248 static void SetCheckpoint(bool all = false);
249 static wxMemStruct *GetCheckpoint(void) { return checkPoint; }
250
251 // Calculated from the request size and any padding needed
252 // before the final marker.
253 static size_t PaddedSize (const size_t reqSize);
254
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);
258
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);
264
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);
268
269 // Access to the list.
270 static wxMemStruct * GetHead () { return m_head; }
271 static wxMemStruct * GetTail () { return m_tail; }
272
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); }
276
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; }
281
282 // Checks all nodes, or all nodes if checkAll is true
283 static int Check(bool checkAll = false);
284
285 // Print out the list of wxMemStruct nodes.
286 static bool PrintList(void);
287
288 // Dump objects
289 static bool Dump(void);
290
291 // Print statistics
292 static bool PrintStatistics(bool detailed = true);
293
294 // Print out the classes in the application.
295 static bool PrintClasses(void);
296
297 // Count the number of non-wxDebugContext-related objects
298 // that are outstanding
299 static int CountObjectsLeft(bool sinceCheckpoint = false);
300
301 // This function is used to output the dump
302 static void OutputDumpLine(const wxChar *szFormat, ...);
303
304 static void SetShutdownNotifyFunction(wxShutdownNotifyFunction shutdownFn);
305
306 private:
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;
311
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;
315
316 // Holds a pointer to an optional application function to call at shutdown.
317 static wxShutdownNotifyFunction sm_shutdownFn;
318
319 // Have to access our shutdown hook
320 friend class wxDebugContextDumpDelayCounter;
321 };
322
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.
327
328 class WXDLLIMPEXP_BASE wxDebugContextDumpDelayCounter
329 {
330 public:
331 wxDebugContextDumpDelayCounter();
332 ~wxDebugContextDumpDelayCounter();
333
334 private:
335 void DoDump();
336 static int sm_count;
337 };
338
339 // make leak dump after all globals have been destructed
340 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_File;
341 #define WXDEBUG_DUMPDELAYCOUNTER \
342 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_Extra;
343
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;
347
348 #define WXTRACE wxTrace
349 #define WXTRACELEVEL wxTraceLevel
350
351 #else // wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
352
353 #define WXDEBUG_DUMPDELAYCOUNTER
354
355 // Borland C++ Builder 6 seems to have troubles with inline functions (see bug
356 // 819700)
357 #if 0
358 inline void wxTrace(const wxChar *WXUNUSED(fmt)) {}
359 inline void wxTraceLevel(int WXUNUSED(level), const wxChar *WXUNUSED(fmt)) {}
360 #else
361 #define wxTrace(fmt)
362 #define wxTraceLevel(l, fmt)
363 #endif
364
365 #define WXTRACE true ? (void)0 : wxTrace
366 #define WXTRACELEVEL true ? (void)0 : wxTraceLevel
367
368 #endif // wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
369
370 #endif // _WX_MEMORY_H_