]> git.saurik.com Git - wxWidgets.git/blob - include/wx/memory.h
One that got away
[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
21 /*
22 The macro which will be expanded to include the file and line number
23 info, or to be a straight call to the new operator.
24 */
25
26 #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
27
28 #include <stddef.h>
29
30 #if wxUSE_IOSTREAMH
31 # include <ostream.h>
32 #else
33 # include <ostream>
34 # ifdef _MSC_VER
35 using namespace std;
36 # endif
37 #endif
38
39 #include "wx/string.h"
40
41 #if WXDEBUG
42
43 // #ifndef WXDEBUG_NEW
44 // #define WXDEBUG_NEW new(__FILE__,__LINE__)
45 // #endif
46
47 void * wxDebugAlloc(size_t size, char * fileName, int lineNum, bool isObject, bool isVect = FALSE);
48 void wxDebugFree(void * buf, bool isVect = FALSE);
49
50 // Global versions of the new and delete operators.
51 // Currently, these merely call malloc and free; only the wxObject
52 // operators do something interesting. But this allows WXDEBUG_NEW to
53 // work for all 'new's in a file.
54 #if wxUSE_GLOBAL_MEMORY_OPERATORS
55
56 #ifdef new
57 #undef new
58 #endif
59
60 void * operator new (size_t size, char * fileName, int lineNum);
61 void operator delete (void * buf);
62
63 // VC++ 6.0
64 #if _MSC_VER >= 1200
65 void operator delete(void *buf, char*, int);
66 #endif
67
68 #if !( defined (_MSC_VER) && (_MSC_VER <= 1020) )
69 void * operator new[] (size_t size, char * fileName, int lineNum);
70 void operator delete[] (void * buf);
71 #endif
72
73 #endif
74 #endif
75
76 typedef unsigned int wxMarkerType;
77
78 /*
79 Define the struct which will be placed at the start of all dynamically
80 allocated memory.
81 */
82
83 class WXDLLEXPORT wxMemStruct {
84
85 friend class WXDLLEXPORT wxDebugContext; // access to the m_next pointer for list traversal.
86
87 public:
88 public:
89 int AssertList ();
90
91 size_t RequestSize () { return m_reqSize; }
92 wxMarkerType Marker () { return m_firstMarker; }
93
94 // When an object is deleted we set the id slot to a specific value.
95 inline void SetDeleted ();
96 inline int IsDeleted ();
97
98 int Append ();
99 int Unlink ();
100
101 // Used to determine if the object is really a wxMemStruct.
102 // Not a foolproof test by any means, but better than none I hope!
103 int AssertIt ();
104
105 // Do all validation on a node.
106 int ValidateNode ();
107
108 // Check the integrity of a node and of the list, node by node.
109 int CheckBlock ();
110 int CheckAllPrevious ();
111
112 // Print a single node.
113 void PrintNode ();
114
115 // Called when the memory linking functions get an error.
116 void ErrorMsg (const char *);
117 void ErrorMsg ();
118
119 inline void *GetActualData(void) const { return m_actualData; }
120
121 void Dump(void);
122
123 public:
124 // Check for underwriting. There are 2 of these checks. This one
125 // inside the struct and another right after the struct.
126 wxMarkerType m_firstMarker;
127
128 // File name and line number are from cpp.
129 char* m_fileName;
130 int m_lineNum;
131
132 // The amount of memory requested by the caller.
133 size_t m_reqSize;
134
135 // Used to try to verify that we really are dealing with an object
136 // of the required class. Can be 1 of 2 values these indicating a valid
137 // wxMemStruct object, or a deleted wxMemStruct object.
138 wxMarkerType m_id;
139
140 wxMemStruct * m_prev;
141 wxMemStruct * m_next;
142
143 void * m_actualData;
144 bool m_isObject;
145 };
146
147
148 typedef void (wxMemStruct::*PmSFV) ();
149
150
151 /*
152 Debugging class. This will only have a single instance, but it\'s
153 a reasonable way to keep everything together and to make this
154 available for change if needed by someone else.
155 A lot of this stuff would be better off within the wxMemStruct class, but
156 it\'s stuff which we need to access at times when there is no wxMemStruct
157 object so we use this class instead. Think of it as a collection of
158 globals which have to do with the wxMemStruct class.
159 */
160
161 class WXDLLEXPORT wxDebugContext {
162
163 protected:
164 // Used to set alignment for markers.
165 static size_t CalcAlignment ();
166
167 // Returns the amount of padding needed after something of the given
168 // size. This is so that when we cast pointers backwards and forwards
169 // the pointer value will be valid for a wxMarkerType.
170 static size_t GetPadding (const size_t size) ;
171
172 // Traverse the list.
173 static void TraverseList (PmSFV, wxMemStruct *from = NULL);
174
175 static streambuf *m_streamBuf;
176 static ostream *m_debugStream;
177
178 static int debugLevel;
179 static bool debugOn;
180
181 public:
182 // Set a checkpoint to dump only the memory from
183 // a given point
184 static wxMemStruct *checkPoint;
185
186 wxDebugContext(void);
187 ~wxDebugContext(void);
188
189 static bool HasStream(void) { return (m_debugStream != NULL); };
190 static ostream& GetStream(void) { return *m_debugStream; }
191 static streambuf *GetStreamBuf(void) { return m_streamBuf; }
192 static void SetStream(ostream *stream, streambuf *buf = NULL);
193 static bool SetFile(const wxString& file);
194 static bool SetStandardError(void);
195
196 static int GetLevel(void) { return debugLevel; }
197 static void SetLevel(int level) { debugLevel = level; }
198
199 static bool GetDebugMode(void) { return debugOn; }
200 static void SetDebugMode(bool flag) { debugOn = flag; }
201
202 static void SetCheckpoint(bool all = FALSE);
203 static wxMemStruct *GetCheckpoint(void) { return checkPoint; }
204
205 // Calculated from the request size and any padding needed
206 // before the final marker.
207 static size_t PaddedSize (const size_t reqSize);
208
209 // Calc the total amount of space we need from the system
210 // to satisfy a caller request. This includes all padding.
211 static size_t TotSize (const size_t reqSize);
212
213 // Return valid pointers to offsets within the allocated memory.
214 static char * StructPos (const char * buf);
215 static char * MidMarkerPos (const char * buf);
216 static char * CallerMemPos (const char * buf);
217 static char * EndMarkerPos (const char * buf, const size_t size);
218
219 // Given a pointer to the start of the caller requested area
220 // return a pointer to the start of the entire alloc\'d buffer.
221 static char * StartPos (const char * caller);
222
223 // Access to the list.
224 static wxMemStruct * GetHead () { return m_head; }
225 static wxMemStruct * GetTail () { return m_tail; }
226
227 // Set the list sentinals.
228 static wxMemStruct * SetHead (wxMemStruct * st) { return (m_head = st); }
229 static wxMemStruct * SetTail (wxMemStruct * st) { return (m_tail = st); }
230
231 // If this is set then every new operation checks the validity
232 // of the all previous nodes in the list.
233 static bool GetCheckPrevious () { return m_checkPrevious; }
234 static void SetCheckPrevious (bool value) { m_checkPrevious = value; }
235
236 // Checks all nodes, or all nodes if checkAll is TRUE
237 static int Check(bool checkAll = FALSE);
238
239 // Print out the list of wxMemStruct nodes.
240 static bool PrintList(void);
241
242 // Dump objects
243 static bool Dump(void);
244
245 // Print statistics
246 static bool PrintStatistics(bool detailed = TRUE);
247
248 // Print out the classes in the application.
249 static bool PrintClasses(void);
250
251 // Count the number of non-wxDebugContext-related objects
252 // that are outstanding
253 static int CountObjectsLeft(void);
254
255 private:
256 // Store these here to allow access to the list without
257 // needing to have a wxMemStruct object.
258 static wxMemStruct* m_head;
259 static wxMemStruct* m_tail;
260
261 // Set to FALSE if we're not checking all previous nodes when
262 // we do a new. Set to TRUE when we are.
263 static bool m_checkPrevious;
264 };
265
266 // Output a debug mess., in a system dependent fashion.
267 void WXDLLEXPORT wxTrace(const char *fmt ...);
268 void WXDLLEXPORT wxTraceLevel(int level, const char *fmt ...);
269
270 #define WXTRACE wxTrace
271 #define WXTRACELEVEL wxTraceLevel
272
273 #else // else part for the #if WXDEBUG
274
275 inline void wxTrace(const char *WXUNUSED(fmt)) {}
276 inline void wxTraceLevel(int WXUNUSED(level), const char *WXUNUSED(fmt)) {}
277
278 #define WXTRACE TRUE ? (void)0 : wxTrace
279 #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
280 // #define WXDEBUG_NEW new
281
282 #endif // WXDEBUG
283
284 #endif
285 // _WX_MEMORYH__
286