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