]> git.saurik.com Git - wxWidgets.git/blame - include/wx/memory.h
rename wxSocketSelectManager to wxSocketFDIOManager, the old name was confusing as...
[wxWidgets.git] / include / wx / memory.h
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
5b56bffb
WS
2// Name: wx/memory.h
3// Purpose: Memory operations
c801d85f
KB
4// Author: Arthur Seaton, Julian Smart
5// Modified by:
6// Created: 29/01/98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Julian Smart
65571936 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
34138703
JS
12#ifndef _WX_MEMORYH__
13#define _WX_MEMORYH__
c801d85f 14
c801d85f 15#include "wx/defs.h"
2432b92d 16#include "wx/string.h"
ced55544 17#include "wx/msgout.h"
c801d85f
KB
18
19/*
20 The macro which will be expanded to include the file and line number
21 info, or to be a straight call to the new operator.
22*/
23
ea57084d 24#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
c801d85f
KB
25
26#include <stddef.h>
27
ea57084d 28#ifdef __WXDEBUG__
e55ad60e 29
4e32eea1
WS
30WXDLLIMPEXP_BASE void * wxDebugAlloc(size_t size, wxChar * fileName, int lineNum, bool isObject, bool isVect = false);
31WXDLLIMPEXP_BASE void wxDebugFree(void * buf, bool isVect = false);
2db0bbde
JS
32
33//**********************************************************************************
34/*
35 The global operator new used for everything apart from getting
36 dynamic storage within this function itself.
37*/
38
39// We'll only do malloc and free for the moment: leave the interesting
40// stuff for the wxObject versions.
241421a6 41
c801d85f 42
47d67540 43#if wxUSE_GLOBAL_MEMORY_OPERATORS
c801d85f 44
6b037754
JS
45// Undefine temporarily (new is #defined in object.h) because we want to
46// declare some new operators.
c801d85f 47#ifdef new
3f4a0c5b 48 #undef new
c801d85f
KB
49#endif
50
5dcf05ae 51#if defined(__SUNCC__)
3f4a0c5b
VZ
52 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
53#elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
54 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
ba6f401d
VZ
55#elif defined (__SGI_CC_)
56 // only supported by -n32 compilers
57 #ifndef __EDG_ABI_COMPATIBILITY_VERSION
58 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
59 #endif
2db0bbde
JS
60#elif !( defined (__VISUALC__) && (__VISUALC__ <= 1020) ) || defined( __MWERKS__)
61 #define wxUSE_ARRAY_MEMORY_OPERATORS 1
5dcf05ae 62#else
ba6f401d 63 // ::operator new[] is a recent C++ feature, so assume it's not supported
3f4a0c5b 64 #define wxUSE_ARRAY_MEMORY_OPERATORS 0
5dcf05ae
JS
65#endif
66
241421a6
JS
67// devik 2000-8-29: All new/delete ops are now inline because they can't
68// be marked as dllexport/dllimport. It then leads to weird bugs when
69// used on MSW as DLL
70#if defined(__WXMSW__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
71inline void * operator new (size_t size, wxChar * fileName, int lineNum)
72{
5b56bffb 73 return wxDebugAlloc(size, fileName, lineNum, false, false);
241421a6
JS
74}
75
76inline void * operator new (size_t size)
77{
5b56bffb 78 return wxDebugAlloc(size, NULL, 0, false);
241421a6
JS
79}
80
81inline void operator delete (void * buf)
82{
5b56bffb 83 wxDebugFree(buf, false);
241421a6
JS
84}
85
86#if wxUSE_ARRAY_MEMORY_OPERATORS
87inline void * operator new[] (size_t size)
88{
5b56bffb 89 return wxDebugAlloc(size, NULL, 0, false, true);
241421a6
JS
90}
91
92inline void * operator new[] (size_t size, wxChar * fileName, int lineNum)
93{
5b56bffb 94 return wxDebugAlloc(size, fileName, lineNum, false, true);
241421a6
JS
95}
96
97inline void operator delete[] (void * buf)
98{
5b56bffb 99 wxDebugFree(buf, true);
241421a6
JS
100}
101#endif // wxUSE_ARRAY_MEMORY_OPERATORS
102
103#else
104
aaf1bbfd 105void * operator new (size_t size, wxChar * fileName, int lineNum);
b40b0f5b 106
aaf1bbfd 107void * operator new (size_t size);
2db0bbde 108
aaf1bbfd 109void operator delete (void * buf);
c801d85f 110
5dcf05ae 111#if wxUSE_ARRAY_MEMORY_OPERATORS
aaf1bbfd
JS
112void * operator new[] (size_t size);
113
114void * operator new[] (size_t size, wxChar * fileName, int lineNum);
115
116void operator delete[] (void * buf);
241421a6
JS
117#endif // wxUSE_ARRAY_MEMORY_OPERATORS
118#endif // defined(__WXMSW__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL_BASE))
c801d85f 119
ba14d986
VZ
120// VC++ 6.0 and MWERKS
121#if ( defined(__VISUALC__) && (__VISUALC__ >= 1200) ) || defined(__MWERKS__)
2db0bbde
JS
122inline void operator delete(void* pData, wxChar* /* fileName */, int /* lineNum */)
123{
4e32eea1 124 wxDebugFree(pData, false);
2db0bbde
JS
125}
126inline void operator delete[](void* pData, wxChar* /* fileName */, int /* lineNum */)
127{
4e32eea1 128 wxDebugFree(pData, true);
2db0bbde
JS
129}
130#endif // __VISUALC__>=1200
131#endif // wxUSE_GLOBAL_MEMORY_OPERATORS
132#endif // __WXDEBUG__
133
134//**********************************************************************************
c801d85f
KB
135
136typedef unsigned int wxMarkerType;
137
138/*
139 Define the struct which will be placed at the start of all dynamically
140 allocated memory.
141*/
142
bddd7a8d 143class WXDLLIMPEXP_BASE wxMemStruct {
c801d85f 144
b5dbe15d 145friend class WXDLLIMPEXP_FWD_BASE wxDebugContext; // access to the m_next pointer for list traversal.
c801d85f
KB
146
147public:
148public:
149 int AssertList ();
150
151 size_t RequestSize () { return m_reqSize; }
152 wxMarkerType Marker () { return m_firstMarker; }
153
154 // When an object is deleted we set the id slot to a specific value.
155 inline void SetDeleted ();
156 inline int IsDeleted ();
157
158 int Append ();
159 int Unlink ();
160
161 // Used to determine if the object is really a wxMemStruct.
162 // Not a foolproof test by any means, but better than none I hope!
163 int AssertIt ();
164
165 // Do all validation on a node.
166 int ValidateNode ();
167
168 // Check the integrity of a node and of the list, node by node.
169 int CheckBlock ();
170 int CheckAllPrevious ();
171
172 // Print a single node.
173 void PrintNode ();
174
175 // Called when the memory linking functions get an error.
176 void ErrorMsg (const char *);
177 void ErrorMsg ();
178
179 inline void *GetActualData(void) const { return m_actualData; }
180
181 void Dump(void);
182
183public:
184 // Check for underwriting. There are 2 of these checks. This one
185 // inside the struct and another right after the struct.
186 wxMarkerType m_firstMarker;
187
188 // File name and line number are from cpp.
4de6207a 189 wxChar* m_fileName;
c801d85f
KB
190 int m_lineNum;
191
192 // The amount of memory requested by the caller.
193 size_t m_reqSize;
194
195 // Used to try to verify that we really are dealing with an object
196 // of the required class. Can be 1 of 2 values these indicating a valid
197 // wxMemStruct object, or a deleted wxMemStruct object.
198 wxMarkerType m_id;
199
200 wxMemStruct * m_prev;
201 wxMemStruct * m_next;
202
203 void * m_actualData;
204 bool m_isObject;
205};
206
207
208typedef void (wxMemStruct::*PmSFV) ();
209
6dfbea27
VZ
210// Type of the app function that can be installed and called at wxWidgets shutdown
211// (after all other registered files with global destructors have been closed down).
212typedef void (*wxShutdownNotifyFunction)();
c801d85f
KB
213
214/*
b073db94 215 Debugging class. This will only have a single instance, but it's
c801d85f
KB
216 a reasonable way to keep everything together and to make this
217 available for change if needed by someone else.
218 A lot of this stuff would be better off within the wxMemStruct class, but
b073db94 219 it's stuff which we need to access at times when there is no wxMemStruct
c801d85f
KB
220 object so we use this class instead. Think of it as a collection of
221 globals which have to do with the wxMemStruct class.
222*/
223
bddd7a8d 224class WXDLLIMPEXP_BASE wxDebugContext {
c801d85f
KB
225
226protected:
227 // Used to set alignment for markers.
228 static size_t CalcAlignment ();
229
230 // Returns the amount of padding needed after something of the given
231 // size. This is so that when we cast pointers backwards and forwards
232 // the pointer value will be valid for a wxMarkerType.
233 static size_t GetPadding (const size_t size) ;
234
235 // Traverse the list.
236 static void TraverseList (PmSFV, wxMemStruct *from = NULL);
237
c801d85f
KB
238 static int debugLevel;
239 static bool debugOn;
240
478e6b71
VZ
241 static int m_balign; // byte alignment
242 static int m_balignmask; // mask for performing byte alignment
c801d85f
KB
243public:
244 // Set a checkpoint to dump only the memory from
245 // a given point
246 static wxMemStruct *checkPoint;
247
248 wxDebugContext(void);
249 ~wxDebugContext(void);
250
c801d85f
KB
251 static int GetLevel(void) { return debugLevel; }
252 static void SetLevel(int level) { debugLevel = level; }
253
254 static bool GetDebugMode(void) { return debugOn; }
255 static void SetDebugMode(bool flag) { debugOn = flag; }
256
4e32eea1 257 static void SetCheckpoint(bool all = false);
c801d85f 258 static wxMemStruct *GetCheckpoint(void) { return checkPoint; }
2bd5bbc9 259
c801d85f
KB
260 // Calculated from the request size and any padding needed
261 // before the final marker.
262 static size_t PaddedSize (const size_t reqSize);
263
264 // Calc the total amount of space we need from the system
265 // to satisfy a caller request. This includes all padding.
266 static size_t TotSize (const size_t reqSize);
267
268 // Return valid pointers to offsets within the allocated memory.
269 static char * StructPos (const char * buf);
270 static char * MidMarkerPos (const char * buf);
271 static char * CallerMemPos (const char * buf);
272 static char * EndMarkerPos (const char * buf, const size_t size);
273
274 // Given a pointer to the start of the caller requested area
275 // return a pointer to the start of the entire alloc\'d buffer.
276 static char * StartPos (const char * caller);
277
278 // Access to the list.
279 static wxMemStruct * GetHead () { return m_head; }
280 static wxMemStruct * GetTail () { return m_tail; }
281
282 // Set the list sentinals.
283 static wxMemStruct * SetHead (wxMemStruct * st) { return (m_head = st); }
284 static wxMemStruct * SetTail (wxMemStruct * st) { return (m_tail = st); }
285
286 // If this is set then every new operation checks the validity
287 // of the all previous nodes in the list.
288 static bool GetCheckPrevious () { return m_checkPrevious; }
289 static void SetCheckPrevious (bool value) { m_checkPrevious = value; }
290
4e32eea1
WS
291 // Checks all nodes, or all nodes if checkAll is true
292 static int Check(bool checkAll = false);
c801d85f
KB
293
294 // Print out the list of wxMemStruct nodes.
295 static bool PrintList(void);
296
297 // Dump objects
298 static bool Dump(void);
299
300 // Print statistics
4e32eea1 301 static bool PrintStatistics(bool detailed = true);
c801d85f
KB
302
303 // Print out the classes in the application.
304 static bool PrintClasses(void);
305
306 // Count the number of non-wxDebugContext-related objects
307 // that are outstanding
4e32eea1 308 static int CountObjectsLeft(bool sinceCheckpoint = false);
c801d85f 309
ced55544
VS
310 // This function is used to output the dump
311 static void OutputDumpLine(const wxChar *szFormat, ...);
312
6dfbea27
VZ
313 static void SetShutdownNotifyFunction(wxShutdownNotifyFunction shutdownFn);
314
c801d85f
KB
315private:
316 // Store these here to allow access to the list without
317 // needing to have a wxMemStruct object.
318 static wxMemStruct* m_head;
319 static wxMemStruct* m_tail;
320
4e32eea1
WS
321 // Set to false if we're not checking all previous nodes when
322 // we do a new. Set to true when we are.
c801d85f 323 static bool m_checkPrevious;
6dfbea27
VZ
324
325 // Holds a pointer to an optional application function to call at shutdown.
326 static wxShutdownNotifyFunction sm_shutdownFn;
327
328 // Have to access our shutdown hook
329 friend class wxDebugContextDumpDelayCounter;
c801d85f
KB
330};
331
ced55544
VS
332// Final cleanup (e.g. deleting the log object and doing memory leak checking)
333// will be delayed until all wxDebugContextDumpDelayCounter objects have been
334// destructed. Adding one wxDebugContextDumpDelayCounter per file will delay
335// memory leak checking until after destructing all global objects.
6dfbea27 336
ced55544
VS
337class WXDLLIMPEXP_BASE wxDebugContextDumpDelayCounter
338{
339public:
6dfbea27
VZ
340 wxDebugContextDumpDelayCounter();
341 ~wxDebugContextDumpDelayCounter();
342
ced55544
VS
343private:
344 void DoDump();
345 static int sm_count;
346};
347
348// make leak dump after all globals have been destructed
349static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_File;
350#define WXDEBUG_DUMPDELAYCOUNTER \
351 static wxDebugContextDumpDelayCounter wxDebugContextDumpDelayCounter_Extra;
352
6b037754 353// Output a debug message, in a system dependent fashion.
bddd7a8d
VZ
354void WXDLLIMPEXP_BASE wxTrace(const wxChar *fmt ...) ATTRIBUTE_PRINTF_1;
355void WXDLLIMPEXP_BASE wxTraceLevel(int level, const wxChar *fmt ...) ATTRIBUTE_PRINTF_2;
c801d85f
KB
356
357#define WXTRACE wxTrace
358#define WXTRACELEVEL wxTraceLevel
359
ced55544
VS
360#else // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
361
362#define WXDEBUG_DUMPDELAYCOUNTER
c801d85f 363
f2e5c082
VZ
364// Borland C++ Builder 6 seems to have troubles with inline functions (see bug
365// 819700)
366#if 0
367 inline void wxTrace(const wxChar *WXUNUSED(fmt)) {}
368 inline void wxTraceLevel(int WXUNUSED(level), const wxChar *WXUNUSED(fmt)) {}
369#else
370 #define wxTrace(fmt)
371 #define wxTraceLevel(l, fmt)
372#endif
c801d85f 373
4e32eea1
WS
374#define WXTRACE true ? (void)0 : wxTrace
375#define WXTRACELEVEL true ? (void)0 : wxTraceLevel
c801d85f 376
ced55544 377#endif // (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
c801d85f
KB
378
379#endif
34138703 380 // _WX_MEMORYH__