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