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