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