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