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