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