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