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