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