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