]> git.saurik.com Git - wxWidgets.git/blob - include/wx/memory.h
Build fix after last commit.
[wxWidgets.git] / include / wx / memory.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: memory.h
3 // Purpose: MDI classes
4 // Author: Arthur Seaton, Julian Smart
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_MEMORYH__
13 #define _WX_MEMORYH__
14
15 #include "wx/defs.h"
16 #include "wx/string.h"
17 #include "wx/msgout.h"
18
19 /*
20 The macro which will be expanded to include the file and line number
21 info, or to be a straight call to the new operator.
22 */
23
24 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
25
26 #include <stddef.h>
27
28 #ifdef __WXDEBUG__
29
30 WXDLLIMPEXP_BASE void * wxDebugAlloc(size_t size, wxChar * fileName, int lineNum, bool isObject, bool isVect = false);
31 WXDLLIMPEXP_BASE void wxDebugFree(void * buf, bool isVect = false);
32
33 //**********************************************************************************
34 /*
35 The global operator new used for everything apart from getting
36 dynamic storage within this function itself.
37 */
38
39 // We'll only do malloc and free for the moment: leave the interesting
40 // stuff for the wxObject versions.
41
42
43 #if wxUSE_GLOBAL_MEMORY_OPERATORS
44
45 // Undefine temporarily (new is #defined in object.h) because we want to
46 // declare some new operators.
47 #ifdef new
48 #undef new
49 #endif
50
51 #if defined(__SUNCC__)
52 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
53 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
54 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
55 #elif defined (__SGI_CC_)
56 // only supported by -n32 compilers
57 #ifndef __EDG_ABI_COMPATIBILITY_VERSION
58 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
59 #endif
60 #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
61 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
62 #else
63 // ::operator new[] is a recent C++ feature, so assume it's not supported
64 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
65 #endif
66
67 // devik 2000-8-29: All new/delete ops are now inline because they can't
68 // be marked as dllexport/dllimport. It then leads to weird bugs when
69 // used on MSW as DLL
70 #if defined(__WXMSW__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
71 inline void * operator new (size_t size, wxChar * fileName, int lineNum)
72 {
73 return wxDebugAlloc(size, fileName, lineNum, FALSE, FALSE);
74 }
75
76 inline void * operator new (size_t size)
77 {
78 return wxDebugAlloc(size, NULL, 0, FALSE);
79 }
80
81 inline void operator delete (void * buf)
82 {
83 wxDebugFree(buf, FALSE);
84 }
85
86 #if wxUSE_ARRAY_MEMORY_OPERATORS
87 inline void * operator new[] (size_t size)
88 {
89 return wxDebugAlloc(size, NULL, 0, FALSE, TRUE);
90 }
91
92 inline void * operator new[] (size_t size, wxChar * fileName, int lineNum)
93 {
94 return wxDebugAlloc(size, fileName, lineNum, FALSE, TRUE);
95 }
96
97 inline void operator delete[] (void * buf)
98 {
99 wxDebugFree(buf, TRUE);
100 }
101 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
102
103 #else
104
105 void * operator new (size_t size, wxChar * fileName, int lineNum);
106
107 void * operator new (size_t size);
108
109 void operator delete (void * buf);
110
111 #if wxUSE_ARRAY_MEMORY_OPERATORS
112 void * operator new[] (size_t size);
113
114 void * operator new[] (size_t size, wxChar * fileName, int lineNum);
115
116 void operator delete[] (void * buf);
117 #endif // wxUSE_ARRAY_MEMORY_OPERATORS
118 #endif // defined(__WXMSW__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
119
120 // VC++ 6.0 and MWERKS
121 #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || defined(__MWERKS__)
122 inline void operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */)
123 {
124 wxDebugFree(pData, false);
125 }
126 inline void operator delete[](void* pData, wxChar* /* fileName */, int /* lineNum */)
127 {
128 wxDebugFree(pData, true);
129 }
130 #endif // __VISUALC__>=1200
131 #endif // wxUSE_GLOBAL_MEMORY_OPERATORS
132 #endif // __WXDEBUG__
133
134 //**********************************************************************************
135
136 typedef unsigned int wxMarkerType;
137
138 /*
139 Define the struct which will be placed at the start of all dynamically
140 allocated memory.
141 */
142
143 class WXDLLIMPEXP_BASE wxMemStruct {
144
145 friend class WXDLLIMPEXP_BASE wxDebugContext; // access to the m_next pointer for list traversal.
146
147 public:
148 public:
149 int AssertList ();
150
151 size_t RequestSize () { return m_reqSize; }
152 wxMarkerType Marker () { return m_firstMarker; }
153
154 // When an object is deleted we set the id slot to a specific value.
155 inline void SetDeleted ();
156 inline int IsDeleted ();
157
158 int Append ();
159 int Unlink ();
160
161 // Used to determine if the object is really a wxMemStruct.
162 // Not a foolproof test by any means, but better than none I hope!
163 int AssertIt ();
164
165 // Do all validation on a node.
166 int ValidateNode ();
167
168 // Check the integrity of a node and of the list, node by node.
169 int CheckBlock ();
170 int CheckAllPrevious ();
171
172 // Print a single node.
173 void PrintNode ();
174
175 // Called when the memory linking functions get an error.
176 void ErrorMsg (const char *);
177 void ErrorMsg ();
178
179 inline void *GetActualData(void) const { return m_actualData; }
180
181 void Dump(void);
182
183 public:
184 // Check for underwriting. There are 2 of these checks. This one
185 // inside the struct and another right after the struct.
186 wxMarkerType m_firstMarker;
187
188 // File name and line number are from cpp.
189 wxChar* m_fileName;
190 int m_lineNum;
191
192 // The amount of memory requested by the caller.
193 size_t m_reqSize;
194
195 // Used to try to verify that we really are dealing with an object
196 // of the required class. Can be 1 of 2 values these indicating a valid
197 // wxMemStruct object, or a deleted wxMemStruct object.
198 wxMarkerType m_id;
199
200 wxMemStruct * m_prev;
201 wxMemStruct * m_next;
202
203 void * m_actualData;
204 bool m_isObject;
205 };
206
207
208 typedef void (wxMemStruct::*PmSFV) ();
209
210
211 /*
212 Debugging class. This will only have a single instance, but it's
213 a reasonable way to keep everything together and to make this
214 available for change if needed by someone else.
215 A lot of this stuff would be better off within the wxMemStruct class, but
216 it's stuff which we need to access at times when there is no wxMemStruct
217 object so we use this class instead. Think of it as a collection of
218 globals which have to do with the wxMemStruct class.
219 */
220
221 class WXDLLIMPEXP_BASE wxDebugContext {
222
223 protected:
224 // Used to set alignment for markers.
225 static size_t CalcAlignment ();
226
227 // Returns the amount of padding needed after something of the given
228 // size. This is so that when we cast pointers backwards and forwards
229 // the pointer value will be valid for a wxMarkerType.
230 static size_t GetPadding (const size_t size) ;
231
232 // Traverse the list.
233 static void TraverseList (PmSFV, wxMemStruct *from = NULL);
234
235 static int debugLevel;
236 static bool debugOn;
237
238 static int m_balign; // byte alignment
239 static int m_balignmask; // mask for performing byte alignment
240 public:
241 // Set a checkpoint to dump only the memory from
242 // a given point
243 static wxMemStruct *checkPoint;
244
245 wxDebugContext(void);
246 ~wxDebugContext(void);
247
248 static int GetLevel(void) { return debugLevel; }
249 static void SetLevel(int level) { debugLevel = level; }
250
251 static bool GetDebugMode(void) { return debugOn; }
252 static void SetDebugMode(bool flag) { debugOn = flag; }
253
254 static void SetCheckpoint(bool all = false);
255 static wxMemStruct *GetCheckpoint(void) { return checkPoint; }
256
257 // Calculated from the request size and any padding needed
258 // before the final marker.
259 static size_t PaddedSize (const size_t reqSize);
260
261 // Calc the total amount of space we need from the system
262 // to satisfy a caller request. This includes all padding.
263 static size_t TotSize (const size_t reqSize);
264
265 // Return valid pointers to offsets within the allocated memory.
266 static char * StructPos (const char * buf);
267 static char * MidMarkerPos (const char * buf);
268 static char * CallerMemPos (const char * buf);
269 static char * EndMarkerPos (const char * buf, const size_t size);
270
271 // Given a pointer to the start of the caller requested area
272 // return a pointer to the start of the entire alloc\'d buffer.
273 static char * StartPos (const char * caller);
274
275 // Access to the list.
276 static wxMemStruct * GetHead () { return m_head; }
277 static wxMemStruct * GetTail () { return m_tail; }
278
279 // Set the list sentinals.
280 static wxMemStruct * SetHead (wxMemStruct * st) { return (m_head = st); }
281 static wxMemStruct * SetTail (wxMemStruct * st) { return (m_tail = st); }
282
283 // If this is set then every new operation checks the validity
284 // of the all previous nodes in the list.
285 static bool GetCheckPrevious () { return m_checkPrevious; }
286 static void SetCheckPrevious (bool value) { m_checkPrevious = value; }
287
288 // Checks all nodes, or all nodes if checkAll is true
289 static int Check(bool checkAll = false);
290
291 // Print out the list of wxMemStruct nodes.
292 static bool PrintList(void);
293
294 // Dump objects
295 static bool Dump(void);
296
297 // Print statistics
298 static bool PrintStatistics(bool detailed = true);
299
300 // Print out the classes in the application.
301 static bool PrintClasses(void);
302
303 // Count the number of non-wxDebugContext-related objects
304 // that are outstanding
305 static int CountObjectsLeft(bool sinceCheckpoint = false);
306
307 // This function is used to output the dump
308 static void OutputDumpLine(const wxChar *szFormat, ...);
309
310 private:
311 // Store these here to allow access to the list without
312 // needing to have a wxMemStruct object.
313 static wxMemStruct* m_head;
314 static wxMemStruct* m_tail;
315
316 // Set to false if we're not checking all previous nodes when
317 // we do a new. Set to true when we are.
318 static bool m_checkPrevious;
319 };
320
321 // Final cleanup (e.g. deleting the log object and doing memory leak checking)
322 // will be delayed until all wxDebugContextDumpDelayCounter objects have been
323 // destructed. Adding one wxDebugContextDumpDelayCounter per file will delay
324 // memory leak checking until after destructing all global objects.
325 class WXDLLIMPEXP_BASE wxDebugContextDumpDelayCounter
326 {
327 public:
328 wxDebugContextDumpDelayCounter() {
329 sm_count++;
330 }
331
332 ~wxDebugContextDumpDelayCounter() {
333 sm_count--;
334 if(!sm_count) DoDump();
335 }
336 private:
337 void DoDump();
338 static int sm_count;
339 };
340
341 // make leak dump after all globals have been destructed
342 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_File;
343 #define WXDEBUG_DUMPDELAYCOUNTER \
344 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_Extra;
345
346 // Output a debug message, in a system dependent fashion.
347 void WXDLLIMPEXP_BASE wxTrace(const wxChar *fmt ...) ATTRIBUTE_PRINTF_1;
348 void WXDLLIMPEXP_BASE wxTraceLevel(int level, const wxChar *fmt ...) ATTRIBUTE_PRINTF_2;
349
350 #define WXTRACE wxTrace
351 #define WXTRACELEVEL wxTraceLevel
352
353 #else // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
354
355 #define WXDEBUG_DUMPDELAYCOUNTER
356
357 // Borland C++ Builder 6 seems to have troubles with inline functions (see bug
358 // 819700)
359 #if 0
360 inline void wxTrace(const wxChar *WXUNUSED(fmt)) {}
361 inline void wxTraceLevel(int WXUNUSED(level), const wxChar *WXUNUSED(fmt)) {}
362 #else
363 #define wxTrace(fmt)
364 #define wxTraceLevel(l, fmt)
365 #endif
366
367 #define WXTRACE true ? (void)0 : wxTrace
368 #define WXTRACELEVEL true ? (void)0 : wxTraceLevel
369
370 #endif // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
371
372 #endif
373 // _WX_MEMORYH__
374