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