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