]>
Commit | Line | Data |
---|---|---|
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 licence | |
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 | #ifdef __WXDEBUG__ | |
32 | ||
33 | WXDLLIMPEXP_BASE void * wxDebugAlloc(size_t size, wxChar * fileName, int lineNum, bool isObject, bool isVect = FALSE); | |
34 | WXDLLIMPEXP_BASE void wxDebugFree(void * buf, bool isVect = FALSE); | |
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 | |
47 | ||
48 | #if wxUSE_GLOBAL_MEMORY_OPERATORS | |
49 | ||
50 | // Undefine temporarily (new is #defined in object.h) because we want to | |
51 | // declare some new operators. | |
52 | #ifdef new | |
53 | #undef new | |
54 | #endif | |
55 | ||
56 | #if defined(__SUNCC__) | |
57 | #define wxUSE_ARRAY_MEMORY_OPERATORS 0 | |
58 | #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__) | |
59 | #define wxUSE_ARRAY_MEMORY_OPERATORS 1 | |
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 | |
65 | #elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__) | |
66 | #define wxUSE_ARRAY_MEMORY_OPERATORS 1 | |
67 | #else | |
68 | // ::operator new[] is a recent C++ feature, so assume it's not supported | |
69 | #define wxUSE_ARRAY_MEMORY_OPERATORS 0 | |
70 | #endif | |
71 | ||
72 | inline void * operator new (size_t size, wxChar * fileName, int lineNum) | |
73 | { | |
74 | return wxDebugAlloc(size, fileName, lineNum, FALSE, FALSE); | |
75 | } | |
76 | ||
77 | inline void * operator new (size_t size) | |
78 | { | |
79 | return wxDebugAlloc(size, NULL, 0, FALSE); | |
80 | } | |
81 | ||
82 | inline void operator delete (void * buf) | |
83 | { | |
84 | wxDebugFree(buf, FALSE); | |
85 | } | |
86 | ||
87 | #if wxUSE_ARRAY_MEMORY_OPERATORS | |
88 | inline void * operator new[] (size_t size) | |
89 | { | |
90 | return wxDebugAlloc(size, NULL, 0, FALSE, TRUE); | |
91 | } | |
92 | ||
93 | inline void * operator new[] (size_t size, wxChar * fileName, int lineNum) | |
94 | { | |
95 | return wxDebugAlloc(size, fileName, lineNum, FALSE, TRUE); | |
96 | } | |
97 | ||
98 | inline void operator delete[] (void * buf) | |
99 | { | |
100 | wxDebugFree(buf, TRUE); | |
101 | } | |
102 | #endif | |
103 | ||
104 | // VC++ 6.0 and MWERKS | |
105 | #if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || defined(__MWERKS__) | |
106 | inline void operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */) | |
107 | { | |
108 | wxDebugFree(pData, FALSE); | |
109 | } | |
110 | inline void operator delete[](void* pData, wxChar* /* fileName */, int /* lineNum */) | |
111 | { | |
112 | wxDebugFree(pData, TRUE); | |
113 | } | |
114 | #endif // __VISUALC__>=1200 | |
115 | #endif // wxUSE_GLOBAL_MEMORY_OPERATORS | |
116 | #endif // __WXDEBUG__ | |
117 | ||
118 | //********************************************************************************** | |
119 | ||
120 | typedef unsigned int wxMarkerType; | |
121 | ||
122 | /* | |
123 | Define the struct which will be placed at the start of all dynamically | |
124 | allocated memory. | |
125 | */ | |
126 | ||
127 | class WXDLLIMPEXP_BASE wxMemStruct { | |
128 | ||
129 | friend class WXDLLIMPEXP_BASE wxDebugContext; // access to the m_next pointer for list traversal. | |
130 | ||
131 | public: | |
132 | public: | |
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 | ||
167 | public: | |
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. | |
173 | wxChar* m_fileName; | |
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 | ||
192 | typedef void (wxMemStruct::*PmSFV) (); | |
193 | ||
194 | ||
195 | /* | |
196 | Debugging class. This will only have a single instance, but it's | |
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 | |
200 | it's stuff which we need to access at times when there is no wxMemStruct | |
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 | ||
205 | class WXDLLIMPEXP_BASE wxDebugContext { | |
206 | ||
207 | protected: | |
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 | ||
219 | static int debugLevel; | |
220 | static bool debugOn; | |
221 | ||
222 | static int m_balign; // byte alignment | |
223 | static int m_balignmask; // mask for performing byte alignment | |
224 | public: | |
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 | ||
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; } | |
240 | ||
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 | |
289 | static int CountObjectsLeft(bool sinceCheckpoint = FALSE); | |
290 | ||
291 | private: | |
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 | ||
302 | // Output a debug message, in a system dependent fashion. | |
303 | void WXDLLIMPEXP_BASE wxTrace(const wxChar *fmt ...) ATTRIBUTE_PRINTF_1; | |
304 | void WXDLLIMPEXP_BASE wxTraceLevel(int level, const wxChar *fmt ...) ATTRIBUTE_PRINTF_2; | |
305 | ||
306 | #define WXTRACE wxTrace | |
307 | #define WXTRACELEVEL wxTraceLevel | |
308 | ||
309 | #else // else part for the #if __WXDEBUG__ | |
310 | ||
311 | inline void wxTrace(const wxChar *WXUNUSED(fmt)) {} | |
312 | inline void wxTraceLevel(int WXUNUSED(level), const wxChar *WXUNUSED(fmt)) {} | |
313 | ||
314 | #define WXTRACE TRUE ? (void)0 : wxTrace | |
315 | #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel | |
316 | ||
317 | #endif // __WXDEBUG__ | |
318 | ||
319 | #endif | |
320 | // _WX_MEMORYH__ | |
321 |