]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
5b56bffb WS |
2 | // Name: wx/memory.h |
3 | // Purpose: Memory operations | |
c801d85f KB |
4 | // Author: Arthur Seaton, Julian Smart |
5 | // Modified by: | |
6 | // Created: 29/01/98 | |
c801d85f | 7 | // Copyright: (c) 1998 Julian Smart |
65571936 | 8 | // Licence: wxWindows licence |
c801d85f KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
657a8a35 VZ |
11 | #ifndef _WX_MEMORY_H_ |
12 | #define _WX_MEMORY_H_ | |
c801d85f | 13 | |
c801d85f | 14 | #include "wx/defs.h" |
2432b92d | 15 | #include "wx/string.h" |
ced55544 | 16 | #include "wx/msgout.h" |
c801d85f | 17 | |
657a8a35 | 18 | #if wxUSE_MEMORY_TRACING || wxUSE_DEBUG_CONTEXT |
c801d85f KB |
19 | |
20 | #include <stddef.h> | |
21 | ||
4e32eea1 WS |
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); | |
2db0bbde JS |
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. | |
241421a6 | 33 | |
c801d85f | 34 | |
47d67540 | 35 | #if wxUSE_GLOBAL_MEMORY_OPERATORS |
c801d85f | 36 | |
6b037754 JS |
37 | // Undefine temporarily (new is #defined in object.h) because we want to |
38 | // declare some new operators. | |
c801d85f | 39 | #ifdef new |
3f4a0c5b | 40 | #undef new |
c801d85f KB |
41 | #endif |
42 | ||
5dcf05ae | 43 | #if defined(__SUNCC__) |
3f4a0c5b | 44 | #define wxUSE_ARRAY_MEMORY_OPERATORS 0 |
2415cf67 | 45 | #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) |
3f4a0c5b | 46 | #define wxUSE_ARRAY_MEMORY_OPERATORS 1 |
ba6f401d VZ |
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 | |
2415cf67 | 52 | #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) |
2db0bbde | 53 | #define wxUSE_ARRAY_MEMORY_OPERATORS 1 |
5dcf05ae | 54 | #else |
ba6f401d | 55 | // ::operator new[] is a recent C++ feature, so assume it's not supported |
3f4a0c5b | 56 | #define wxUSE_ARRAY_MEMORY_OPERATORS 0 |
5dcf05ae JS |
57 | #endif |
58 | ||
241421a6 JS |
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 | |
d98a58c5 | 62 | #if defined(__WINDOWS__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE)) |
241421a6 JS |
63 | inline void * operator new (size_t size, wxChar * fileName, int lineNum) |
64 | { | |
5b56bffb | 65 | return wxDebugAlloc(size, fileName, lineNum, false, false); |
241421a6 JS |
66 | } |
67 | ||
68 | inline void * operator new (size_t size) | |
69 | { | |
5b56bffb | 70 | return wxDebugAlloc(size, NULL, 0, false); |
241421a6 JS |
71 | } |
72 | ||
73 | inline void operator delete (void * buf) | |
74 | { | |
5b56bffb | 75 | wxDebugFree(buf, false); |
241421a6 JS |
76 | } |
77 | ||
78 | #if wxUSE_ARRAY_MEMORY_OPERATORS | |
79 | inline void * operator new[] (size_t size) | |
80 | { | |
5b56bffb | 81 | return wxDebugAlloc(size, NULL, 0, false, true); |
241421a6 JS |
82 | } |
83 | ||
84 | inline void * operator new[] (size_t size, wxChar * fileName, int lineNum) | |
85 | { | |
5b56bffb | 86 | return wxDebugAlloc(size, fileName, lineNum, false, true); |
241421a6 JS |
87 | } |
88 | ||
89 | inline void operator delete[] (void * buf) | |
90 | { | |
5b56bffb | 91 | wxDebugFree(buf, true); |
241421a6 JS |
92 | } |
93 | #endif // wxUSE_ARRAY_MEMORY_OPERATORS | |
94 | ||
95 | #else | |
96 | ||
aaf1bbfd | 97 | void * operator new (size_t size, wxChar * fileName, int lineNum); |
b40b0f5b | 98 | |
aaf1bbfd | 99 | void * operator new (size_t size); |
2db0bbde | 100 | |
aaf1bbfd | 101 | void operator delete (void * buf); |
c801d85f | 102 | |
5dcf05ae | 103 | #if wxUSE_ARRAY_MEMORY_OPERATORS |
aaf1bbfd JS |
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); | |
241421a6 | 109 | #endif // wxUSE_ARRAY_MEMORY_OPERATORS |
d98a58c5 | 110 | #endif // defined(__WINDOWS__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE)) |
c801d85f | 111 | |
2415cf67 VZ |
112 | // VC++ 6.0 |
113 | #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) | |
2db0bbde JS |
114 | inline void operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */) |
115 | { | |
4e32eea1 | 116 | wxDebugFree(pData, false); |
2db0bbde JS |
117 | } |
118 | inline void operator delete[](void* pData, wxChar* /* fileName */, int /* lineNum */) | |
119 | { | |
4e32eea1 | 120 | wxDebugFree(pData, true); |
2db0bbde JS |
121 | } |
122 | #endif // __VISUALC__>=1200 | |
123 | #endif // wxUSE_GLOBAL_MEMORY_OPERATORS | |
2db0bbde JS |
124 | |
125 | //********************************************************************************** | |
c801d85f KB |
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 | ||
bddd7a8d | 134 | class WXDLLIMPEXP_BASE wxMemStruct { |
c801d85f | 135 | |
b5dbe15d | 136 | friend class WXDLLIMPEXP_FWD_BASE wxDebugContext; // access to the m_next pointer for list traversal. |
c801d85f KB |
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. | |
4de6207a | 180 | wxChar* m_fileName; |
c801d85f KB |
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 | ||
6dfbea27 VZ |
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)(); | |
c801d85f KB |
204 | |
205 | /* | |
b073db94 | 206 | Debugging class. This will only have a single instance, but it's |
c801d85f KB |
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 | |
b073db94 | 210 | it's stuff which we need to access at times when there is no wxMemStruct |
c801d85f KB |
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 | ||
bddd7a8d | 215 | class WXDLLIMPEXP_BASE wxDebugContext { |
c801d85f KB |
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 | ||
c801d85f KB |
229 | static int debugLevel; |
230 | static bool debugOn; | |
231 | ||
478e6b71 VZ |
232 | static int m_balign; // byte alignment |
233 | static int m_balignmask; // mask for performing byte alignment | |
c801d85f KB |
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 | ||
c801d85f KB |
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 | ||
4e32eea1 | 248 | static void SetCheckpoint(bool all = false); |
c801d85f | 249 | static wxMemStruct *GetCheckpoint(void) { return checkPoint; } |
2bd5bbc9 | 250 | |
c801d85f KB |
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 | ||
4e32eea1 WS |
282 | // Checks all nodes, or all nodes if checkAll is true |
283 | static int Check(bool checkAll = false); | |
c801d85f KB |
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 | |
4e32eea1 | 292 | static bool PrintStatistics(bool detailed = true); |
c801d85f KB |
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 | |
4e32eea1 | 299 | static int CountObjectsLeft(bool sinceCheckpoint = false); |
c801d85f | 300 | |
ced55544 VS |
301 | // This function is used to output the dump |
302 | static void OutputDumpLine(const wxChar *szFormat, ...); | |
303 | ||
6dfbea27 VZ |
304 | static void SetShutdownNotifyFunction(wxShutdownNotifyFunction shutdownFn); |
305 | ||
c801d85f KB |
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 | ||
4e32eea1 WS |
312 | // Set to false if we're not checking all previous nodes when |
313 | // we do a new. Set to true when we are. | |
c801d85f | 314 | static bool m_checkPrevious; |
6dfbea27 VZ |
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; | |
c801d85f KB |
321 | }; |
322 | ||
ced55544 VS |
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. | |
6dfbea27 | 327 | |
ced55544 VS |
328 | class WXDLLIMPEXP_BASE wxDebugContextDumpDelayCounter |
329 | { | |
330 | public: | |
6dfbea27 VZ |
331 | wxDebugContextDumpDelayCounter(); |
332 | ~wxDebugContextDumpDelayCounter(); | |
333 | ||
ced55544 VS |
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 | ||
6b037754 | 344 | // Output a debug message, in a system dependent fashion. |
862bb5c7 VS |
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; | |
c801d85f KB |
347 | |
348 | #define WXTRACE wxTrace | |
349 | #define WXTRACELEVEL wxTraceLevel | |
350 | ||
657a8a35 | 351 | #else // wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
ced55544 VS |
352 | |
353 | #define WXDEBUG_DUMPDELAYCOUNTER | |
c801d85f | 354 | |
f2e5c082 VZ |
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 | |
c801d85f | 364 | |
4e32eea1 WS |
365 | #define WXTRACE true ? (void)0 : wxTrace |
366 | #define WXTRACELEVEL true ? (void)0 : wxTraceLevel | |
c801d85f | 367 | |
657a8a35 | 368 | #endif // wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
c801d85f | 369 | |
657a8a35 | 370 | #endif // _WX_MEMORY_H_ |