]> git.saurik.com Git - wxWidgets.git/blame - include/wx/memory.h
Small modifications to Vadims changes
[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
47d67540 31#if wxUSE_IOSTREAMH
3f4a0c5b
VZ
32 // N.B. BC++ doesn't have istream.h, ostream.h
33# include <iostream.h>
c801d85f 34#else
3f4a0c5b
VZ
35# include <ostream>
36# if defined(__VISUALC__) || defined(__MWERKS__)
37 using namespace std;
38# endif
c801d85f
KB
39#endif
40
ea57084d 41#ifdef __WXDEBUG__
e55ad60e 42
4de6207a 43void * wxDebugAlloc(size_t size, wxChar * fileName, int lineNum, bool isObject, bool isVect = FALSE);
c801d85f
KB
44void wxDebugFree(void * buf, bool isVect = FALSE);
45
46// Global versions of the new and delete operators.
47d67540 47#if wxUSE_GLOBAL_MEMORY_OPERATORS
c801d85f 48
6b037754
JS
49// Undefine temporarily (new is #defined in object.h) because we want to
50// declare some new operators.
c801d85f 51#ifdef new
3f4a0c5b 52 #undef new
c801d85f
KB
53#endif
54
5dcf05ae 55#if defined(__SUNCC__)
3f4a0c5b
VZ
56 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
57#elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
58 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
ba6f401d
VZ
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
5dcf05ae 64#else
ba6f401d 65 // ::operator new[] is a recent C++ feature, so assume it's not supported
3f4a0c5b 66 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
5dcf05ae
JS
67#endif
68
7fe7d506
JS
69// Added JACS 25/11/98: needed for some compilers
70void * operator new (size_t size);
30c710fd 71
4de6207a 72void * operator new (size_t size, wxChar * fileName, int lineNum);
c801d85f
KB
73void operator delete (void * buf);
74
5dcf05ae 75#if wxUSE_ARRAY_MEMORY_OPERATORS
ba6f401d 76 void * operator new[] (size_t size);
4de6207a 77 void * operator new[] (size_t size, wxChar * fileName, int lineNum);
ba6f401d 78 void operator delete[] (void * buf);
201812a8 79#endif
c801d85f 80
184b5d99 81// VC++ 6.0
3f4a0c5b 82#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
4de6207a
OK
83 void operator delete(void *buf, wxChar*, int);
84 void operator delete[](void *buf, wxChar*, int);
184b5d99
JS
85#endif
86
c801d85f 87#endif
2432b92d 88 // wxUSE_GLOBAL_MEMORY_OPERATORS
c801d85f 89#endif
2432b92d 90 // __WXDEBUG__
c801d85f
KB
91
92typedef unsigned int wxMarkerType;
93
94/*
95 Define the struct which will be placed at the start of all dynamically
96 allocated memory.
97*/
98
99class WXDLLEXPORT wxMemStruct {
100
101friend class WXDLLEXPORT wxDebugContext; // access to the m_next pointer for list traversal.
102
103public:
104public:
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
139public:
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.
4de6207a 145 wxChar* m_fileName;
c801d85f
KB
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
164typedef 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
177class WXDLLEXPORT wxDebugContext {
178
179protected:
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
197public:
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
4fabb575 269 static int CountObjectsLeft(bool sinceCheckpoint = FALSE);
c801d85f
KB
270
271private:
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
6b037754 282// Output a debug message, in a system dependent fashion.
4de6207a
OK
283void WXDLLEXPORT wxTrace(const wxChar *fmt ...);
284void WXDLLEXPORT wxTraceLevel(int level, const wxChar *fmt ...);
c801d85f
KB
285
286#define WXTRACE wxTrace
287#define WXTRACELEVEL wxTraceLevel
288
6b037754 289#else // else part for the #if __WXDEBUG__
c801d85f 290
4de6207a
OK
291inline void wxTrace(const wxChar *WXUNUSED(fmt)) {}
292inline void wxTraceLevel(int WXUNUSED(level), const wxChar *WXUNUSED(fmt)) {}
c801d85f
KB
293
294#define WXTRACE TRUE ? (void)0 : wxTrace
295#define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel
e55ad60e 296// #define WXDEBUG_NEW new
c801d85f 297
ea57084d 298#endif // __WXDEBUG__
c801d85f
KB
299
300#endif
34138703 301 // _WX_MEMORYH__
c801d85f 302