]>
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 | |
9 | // Licence: wxWindows license | |
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" | |
20 | ||
21 | /* | |
22 | The macro which will be expanded to include the file and line number | |
23 | info, or to be a straight call to the new operator. | |
24 | */ | |
25 | ||
47d67540 | 26 | #if (WXDEBUG && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT |
c801d85f KB |
27 | |
28 | #include <stddef.h> | |
29 | ||
47d67540 | 30 | #if wxUSE_IOSTREAMH |
c801d85f KB |
31 | #include <iostream.h> |
32 | #else | |
33 | #include <iostream> | |
34 | #endif | |
35 | ||
36 | #include "wx/string.h" | |
37 | ||
b2aef89b | 38 | #if WXDEBUG |
e55ad60e RR |
39 | |
40 | // #ifndef WXDEBUG_NEW | |
41 | // #define WXDEBUG_NEW new(__FILE__,__LINE__) | |
42 | // #endif | |
43 | ||
c801d85f KB |
44 | void * wxDebugAlloc(size_t size, char * fileName, int lineNum, bool isObject, bool isVect = FALSE); |
45 | void wxDebugFree(void * buf, bool isVect = FALSE); | |
46 | ||
47 | // Global versions of the new and delete operators. | |
48 | // Currently, these merely call malloc and free; only the wxObject | |
49 | // operators do something interesting. But this allows WXDEBUG_NEW to | |
50 | // work for all 'new's in a file. | |
47d67540 | 51 | #if wxUSE_GLOBAL_MEMORY_OPERATORS |
c801d85f KB |
52 | |
53 | #ifdef new | |
54 | #undef new | |
55 | #endif | |
56 | ||
57 | void * operator new (size_t size, char * fileName, int lineNum); | |
58 | void operator delete (void * buf); | |
59 | ||
76626af2 JS |
60 | // VC++ 6.0 |
61 | #if _MSC_VER >= 1200 | |
62 | void operator delete(void *buf, char*, int); | |
63 | #endif | |
64 | ||
16c1f7f3 | 65 | #if !( defined (_MSC_VER) && (_MSC_VER <= 1020) ) |
c801d85f KB |
66 | void * operator new[] (size_t size, char * fileName, int lineNum); |
67 | void operator delete[] (void * buf); | |
201812a8 | 68 | #endif |
c801d85f | 69 | |
c801d85f KB |
70 | #endif |
71 | #endif | |
72 | ||
73 | typedef unsigned int wxMarkerType; | |
74 | ||
75 | /* | |
76 | Define the struct which will be placed at the start of all dynamically | |
77 | allocated memory. | |
78 | */ | |
79 | ||
80 | class WXDLLEXPORT wxMemStruct { | |
81 | ||
82 | friend class WXDLLEXPORT wxDebugContext; // access to the m_next pointer for list traversal. | |
83 | ||
84 | public: | |
85 | public: | |
86 | int AssertList (); | |
87 | ||
88 | size_t RequestSize () { return m_reqSize; } | |
89 | wxMarkerType Marker () { return m_firstMarker; } | |
90 | ||
91 | // When an object is deleted we set the id slot to a specific value. | |
92 | inline void SetDeleted (); | |
93 | inline int IsDeleted (); | |
94 | ||
95 | int Append (); | |
96 | int Unlink (); | |
97 | ||
98 | // Used to determine if the object is really a wxMemStruct. | |
99 | // Not a foolproof test by any means, but better than none I hope! | |
100 | int AssertIt (); | |
101 | ||
102 | // Do all validation on a node. | |
103 | int ValidateNode (); | |
104 | ||
105 | // Check the integrity of a node and of the list, node by node. | |
106 | int CheckBlock (); | |
107 | int CheckAllPrevious (); | |
108 | ||
109 | // Print a single node. | |
110 | void PrintNode (); | |
111 | ||
112 | // Called when the memory linking functions get an error. | |
113 | void ErrorMsg (const char *); | |
114 | void ErrorMsg (); | |
115 | ||
116 | inline void *GetActualData(void) const { return m_actualData; } | |
117 | ||
118 | void Dump(void); | |
119 | ||
120 | public: | |
121 | // Check for underwriting. There are 2 of these checks. This one | |
122 | // inside the struct and another right after the struct. | |
123 | wxMarkerType m_firstMarker; | |
124 | ||
125 | // File name and line number are from cpp. | |
126 | char* m_fileName; | |
127 | int m_lineNum; | |
128 | ||
129 | // The amount of memory requested by the caller. | |
130 | size_t m_reqSize; | |
131 | ||
132 | // Used to try to verify that we really are dealing with an object | |
133 | // of the required class. Can be 1 of 2 values these indicating a valid | |
134 | // wxMemStruct object, or a deleted wxMemStruct object. | |
135 | wxMarkerType m_id; | |
136 | ||
137 | wxMemStruct * m_prev; | |
138 | wxMemStruct * m_next; | |
139 | ||
140 | void * m_actualData; | |
141 | bool m_isObject; | |
142 | }; | |
143 | ||
144 | ||
145 | typedef void (wxMemStruct::*PmSFV) (); | |
146 | ||
147 | ||
148 | /* | |
149 | Debugging class. This will only have a single instance, but it\'s | |
150 | a reasonable way to keep everything together and to make this | |
151 | available for change if needed by someone else. | |
152 | A lot of this stuff would be better off within the wxMemStruct class, but | |
153 | it\'s stuff which we need to access at times when there is no wxMemStruct | |
154 | object so we use this class instead. Think of it as a collection of | |
155 | globals which have to do with the wxMemStruct class. | |
156 | */ | |
157 | ||
158 | class WXDLLEXPORT wxDebugContext { | |
159 | ||
160 | protected: | |
161 | // Used to set alignment for markers. | |
162 | static size_t CalcAlignment (); | |
163 | ||
164 | // Returns the amount of padding needed after something of the given | |
165 | // size. This is so that when we cast pointers backwards and forwards | |
166 | // the pointer value will be valid for a wxMarkerType. | |
167 | static size_t GetPadding (const size_t size) ; | |
168 | ||
169 | // Traverse the list. | |
170 | static void TraverseList (PmSFV, wxMemStruct *from = NULL); | |
171 | ||
172 | static streambuf *m_streamBuf; | |
173 | static ostream *m_debugStream; | |
174 | ||
175 | static int debugLevel; | |
176 | static bool debugOn; | |
177 | ||
178 | public: | |
179 | // Set a checkpoint to dump only the memory from | |
180 | // a given point | |
181 | static wxMemStruct *checkPoint; | |
182 | ||
183 | wxDebugContext(void); | |
184 | ~wxDebugContext(void); | |
185 | ||
186 | static bool HasStream(void) { return (m_debugStream != NULL); }; | |
187 | static ostream& GetStream(void) { return *m_debugStream; } | |
188 | static streambuf *GetStreamBuf(void) { return m_streamBuf; } | |
189 | static void SetStream(ostream *stream, streambuf *buf = NULL); | |
190 | static bool SetFile(const wxString& file); | |
191 | static bool SetStandardError(void); | |
192 | ||
193 | static int GetLevel(void) { return debugLevel; } | |
194 | static void SetLevel(int level) { debugLevel = level; } | |
195 | ||
196 | static bool GetDebugMode(void) { return debugOn; } | |
197 | static void SetDebugMode(bool flag) { debugOn = flag; } | |
198 | ||
199 | static void SetCheckpoint(bool all = FALSE); | |
200 | static wxMemStruct *GetCheckpoint(void) { return checkPoint; } | |
201 | ||
202 | // Calculated from the request size and any padding needed | |
203 | // before the final marker. | |
204 | static size_t PaddedSize (const size_t reqSize); | |
205 | ||
206 | // Calc the total amount of space we need from the system | |
207 | // to satisfy a caller request. This includes all padding. | |
208 | static size_t TotSize (const size_t reqSize); | |
209 | ||
210 | // Return valid pointers to offsets within the allocated memory. | |
211 | static char * StructPos (const char * buf); | |
212 | static char * MidMarkerPos (const char * buf); | |
213 | static char * CallerMemPos (const char * buf); | |
214 | static char * EndMarkerPos (const char * buf, const size_t size); | |
215 | ||
216 | // Given a pointer to the start of the caller requested area | |
217 | // return a pointer to the start of the entire alloc\'d buffer. | |
218 | static char * StartPos (const char * caller); | |
219 | ||
220 | // Access to the list. | |
221 | static wxMemStruct * GetHead () { return m_head; } | |
222 | static wxMemStruct * GetTail () { return m_tail; } | |
223 | ||
224 | // Set the list sentinals. | |
225 | static wxMemStruct * SetHead (wxMemStruct * st) { return (m_head = st); } | |
226 | static wxMemStruct * SetTail (wxMemStruct * st) { return (m_tail = st); } | |
227 | ||
228 | // If this is set then every new operation checks the validity | |
229 | // of the all previous nodes in the list. | |
230 | static bool GetCheckPrevious () { return m_checkPrevious; } | |
231 | static void SetCheckPrevious (bool value) { m_checkPrevious = value; } | |
232 | ||
233 | // Checks all nodes, or all nodes if checkAll is TRUE | |
234 | static int Check(bool checkAll = FALSE); | |
235 | ||
236 | // Print out the list of wxMemStruct nodes. | |
237 | static bool PrintList(void); | |
238 | ||
239 | // Dump objects | |
240 | static bool Dump(void); | |
241 | ||
242 | // Print statistics | |
243 | static bool PrintStatistics(bool detailed = TRUE); | |
244 | ||
245 | // Print out the classes in the application. | |
246 | static bool PrintClasses(void); | |
247 | ||
248 | // Count the number of non-wxDebugContext-related objects | |
249 | // that are outstanding | |
250 | static int CountObjectsLeft(void); | |
251 | ||
252 | private: | |
253 | // Store these here to allow access to the list without | |
254 | // needing to have a wxMemStruct object. | |
255 | static wxMemStruct* m_head; | |
256 | static wxMemStruct* m_tail; | |
257 | ||
258 | // Set to FALSE if we're not checking all previous nodes when | |
259 | // we do a new. Set to TRUE when we are. | |
260 | static bool m_checkPrevious; | |
261 | }; | |
262 | ||
263 | // Output a debug mess., in a system dependent fashion. | |
264 | void WXDLLEXPORT wxTrace(const char *fmt ...); | |
265 | void WXDLLEXPORT wxTraceLevel(int level, const char *fmt ...); | |
266 | ||
267 | #define WXTRACE wxTrace | |
268 | #define WXTRACELEVEL wxTraceLevel | |
269 | ||
b2aef89b | 270 | #else // else part for the #if WXDEBUG |
c801d85f KB |
271 | |
272 | inline void wxTrace(const char *WXUNUSED(fmt)) {} | |
273 | inline void wxTraceLevel(int WXUNUSED(level), const char *WXUNUSED(fmt)) {} | |
274 | ||
275 | #define WXTRACE TRUE ? (void)0 : wxTrace | |
276 | #define WXTRACELEVEL TRUE ? (void)0 : wxTraceLevel | |
e55ad60e | 277 | // #define WXDEBUG_NEW new |
c801d85f | 278 | |
b2aef89b | 279 | #endif // WXDEBUG |
c801d85f KB |
280 | |
281 | #endif | |
34138703 | 282 | // _WX_MEMORYH__ |
c801d85f | 283 |