]> git.saurik.com Git - wxWidgets.git/blame - include/wx/memory.h
Committing in .
[wxWidgets.git] / include / wx / memory.h
CommitLineData
c801d85f
KB
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
3f4a0c5b 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_MEMORYH__
13#define _WX_MEMORYH__
c801d85f
KB
14
15#ifdef __GNUG__
16#pragma interface "memory.h"
17#endif
18
19#include "wx/defs.h"
2432b92d 20#include "wx/string.h"
c801d85f
KB
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
ea57084d 27#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
c801d85f
KB
28
29#include <stddef.h>
30
fd85b064
JS
31// Obsolete
32#if 0
47d67540 33#if wxUSE_IOSTREAMH
3f4a0c5b
VZ
34 // N.B. BC++ doesn't have istream.h, ostream.h
35# include <iostream.h>
c801d85f 36#else
e7fa076d 37# include <iostream>
3f4a0c5b 38# if defined(__VISUALC__) || defined(__MWERKS__)
dd107c50 39// using namespace std;
3f4a0c5b 40# endif
c801d85f 41#endif
fd85b064 42#endif
c801d85f 43
ea57084d 44#ifdef __WXDEBUG__
e55ad60e 45
2db0bbde
JS
46// devik 2000-8-27: export these because new/delete operators are now inline
47WXDLLEXPORT void * wxDebugAlloc(size_t size, wxChar * fileName, int lineNum, bool isObject, bool isVect = FALSE);
48WXDLLEXPORT 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
c801d85f 61
47d67540 62#if wxUSE_GLOBAL_MEMORY_OPERATORS
c801d85f 63
6b037754
JS
64// Undefine temporarily (new is #defined in object.h) because we want to
65// declare some new operators.
c801d85f 66#ifdef new
3f4a0c5b 67 #undef new
c801d85f
KB
68#endif
69
5dcf05ae 70#if defined(__SUNCC__)
3f4a0c5b
VZ
71 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
72#elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
73 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
ba6f401d
VZ
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
2db0bbde
JS
79#elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
80 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
5dcf05ae 81#else
ba6f401d 82 // ::operator new[] is a recent C++ feature, so assume it's not supported
3f4a0c5b 83 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
5dcf05ae
JS
84#endif
85
2db0bbde
JS
86inline void * operator new (size_t size, wxChar * fileName, int lineNum)
87{
88 return wxDebugAlloc(size, fileName, lineNum, FALSE, FALSE);
89}
b40b0f5b 90
2db0bbde
JS
91inline void * operator new (size_t size)
92{
93 return wxDebugAlloc(size, NULL, 0, FALSE);
94}
95
96inline void operator delete (void * buf)
97{
98 wxDebugFree(buf, FALSE);
99}
c801d85f 100
5dcf05ae 101#if wxUSE_ARRAY_MEMORY_OPERATORS
2db0bbde
JS
102inline void * operator new[] (size_t size)
103{
104 return wxDebugAlloc(size, NULL, 0, FALSE, TRUE);
105}
106
107inline void * operator new[] (size_t size, wxChar * fileName, int lineNum)
108{
109 return wxDebugAlloc(size, fileName, lineNum, FALSE, TRUE);
110}
111
112inline void operator delete[] (void * buf)
113{
114 wxDebugFree(buf, TRUE);
115}
201812a8 116#endif
c801d85f 117
184b5d99 118// VC++ 6.0
3f4a0c5b 119#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
2db0bbde
JS
120inline void operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */)
121{
122 wxDebugFree(pData, FALSE);
123}
124inline 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//**********************************************************************************
c801d85f
KB
133
134typedef unsigned int wxMarkerType;
135
136/*
137 Define the struct which will be placed at the start of all dynamically
138 allocated memory.
139*/
140
141class WXDLLEXPORT wxMemStruct {
142
143friend class WXDLLEXPORT wxDebugContext; // access to the m_next pointer for list traversal.
144
145public:
146public:
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
181public:
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.
4de6207a 187 wxChar* m_fileName;
c801d85f
KB
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
206typedef 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
219class WXDLLEXPORT wxDebugContext {
220
221protected:
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
fd85b064
JS
233 // Obsolete
234#if 0
dd107c50
VZ
235 static wxSTD streambuf *m_streamBuf;
236 static wxSTD ostream *m_debugStream;
fd85b064 237#endif
c801d85f
KB
238
239 static int debugLevel;
240 static bool debugOn;
241
478e6b71
VZ
242 static int m_balign; // byte alignment
243 static int m_balignmask; // mask for performing byte alignment
c801d85f
KB
244public:
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
fd85b064
JS
252 // Obsolete
253#if 0
c801d85f 254 static bool HasStream(void) { return (m_debugStream != NULL); };
dd107c50
VZ
255 static wxSTD ostream& GetStream(void) { return *m_debugStream; }
256 static wxSTD streambuf *GetStreamBuf(void) { return m_streamBuf; }
478e6b71 257 static void SetStream(wxSTD ostream *stream, wxSTD streambuf *buf = NULL);
c801d85f
KB
258 static bool SetFile(const wxString& file);
259 static bool SetStandardError(void);
fd85b064 260#endif
c801d85f
KB
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; }
2bd5bbc9 270
c801d85f
KB
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
4fabb575 319 static int CountObjectsLeft(bool sinceCheckpoint = FALSE);
c801d85f
KB
320
321private:
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
6b037754 332// Output a debug message, in a system dependent fashion.
4de6207a
OK
333void WXDLLEXPORT wxTrace(const wxChar *fmt ...);
334void WXDLLEXPORT wxTraceLevel(int level, const wxChar *fmt ...);
c801d85f
KB
335
336#define WXTRACE wxTrace
337#define WXTRACELEVEL wxTraceLevel
338
6b037754 339#else // else part for the #if __WXDEBUG__
c801d85f 340
4de6207a
OK
341inline void wxTrace(const wxChar *WXUNUSED(fmt)) {}
342inline void wxTraceLevel(int WXUNUSED(level), const wxChar *WXUNUSED(fmt)) {}
c801d85f
KB
343
344#define WXTRACE TRUE ? (void)0 : wxTrace
345#define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
e55ad60e 346// #define WXDEBUG_NEW new
c801d85f 347
ea57084d 348#endif // __WXDEBUG__
c801d85f
KB
349
350#endif
34138703 351 // _WX_MEMORYH__
c801d85f 352