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