wxArt2D
a2dmemmgr.cpp
Go to the documentation of this file.
1 /*! \file general/src/a2dmemmgr.cpp
2  \author Klaas Holwerda / Leo Kadisoff
3 
4  Copyright: 2001-2005 (c) Klaas Holwerda
5 
6  Licence: wxWidgets Licence
7 
8  RCS-ID: $Id: a2dmemmgr.cpp,v 1.9 2007/07/30 19:34:11 titato Exp $
9 */
10 
11 #include "wxartbaseprec.h"
12 
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16 
17 #ifndef WX_PRECOMP
18 #include "wx/wx.h"
19 #endif
20 
21 #include "wx/general/a2dmemmgr.h"
22 
23 #ifdef new
24 #undef new
25 #endif
26 
28 {
29  for( a2dFreeList* fl = m_allLists; fl != NULL; )
30  {
31  wxASSERT( m_count > 0 );
32  for( void* space = fl->m_topOfList; space != NULL; )
33  {
34  wxASSERT( fl->m_count > 0 );
35  void* itemForDestroy = space;
36  space = *( ( void** )space );
37  delete itemForDestroy;
38  fl->m_count --;
39  }
40  a2dFreeList* forDestroy = fl;
41  fl = fl->m_next;
42  delete forDestroy;
43  m_count--;
44  }
45 }
46 
47 void* a2dMemManager::Allocate( size_t bytes )
48 {
49 // wxCriticalSectionLocker aGuard(m_guard);
50  for( a2dFreeList* fl = m_allLists;
51  fl != NULL;
52  fl = fl->m_next )
53  {
54  if( fl->m_blockSize == bytes )
55  {
56  if( fl->m_topOfList != NULL )
57  {
58  void* space = fl->m_topOfList;
59  fl->m_topOfList = *( ( void** )( fl->m_topOfList ) );
60  fl->m_count--;
61  wxASSERT( ( fl->m_topOfList == NULL && fl->m_count == 0 ) || ( fl->m_topOfList != NULL && fl->m_count != 0 ) );
62  return space;
63  }
64  break;
65  }
66  }
67  void* newpointer = ::operator new( bytes );
68  return newpointer;
69 }
70 
71 void a2dMemManager::Deallocate( void* space, size_t bytes )
72 {
73 // wxCriticalSectionLocker aGuard(m_guard);
74 
75  a2dFreeList* fl;
76  for( fl = m_allLists; fl != NULL; fl = fl->m_next )
77  {
78  if( fl->m_blockSize == bytes )
79  break;
80  }
81  if( fl == NULL )
82  {
83  fl = new a2dFreeList( m_allLists, bytes );
84  m_allLists = fl;
85  m_count++;
86  }
87 
88  *( ( void** )space ) = fl->m_topOfList;
89  fl->m_topOfList = space;
90  fl->m_count++;
91 }
92 
93 #ifdef __WXDEBUG__
94 #if wxUSE_STACKWALKER
95 #include "wx/stackwalk.h"
96 
97 wxString a2dGetStackTrace( size_t skip, bool withArgs, int maxCalls, int maxLines )
98 {
99  wxString stackTrace;
100 
101  class StackDump : public wxStackWalker
102  {
103  public:
104  StackDump( int maxCalls = 20, bool withArgs = false ) { m_curCall = 0; m_maxCalls = maxCalls; m_withArgs = withArgs; }
105 
106  const wxString& GetStackTrace() const { return m_stackTrace; }
107 
108  protected:
109  virtual void OnStackFrame( const wxStackFrame& frame )
110  {
111  if( m_curCall >= m_maxCalls )
112  return;
113  m_curCall++;
114  m_stackTrace << wxString::Format( _T( "\t[%02d] " ), frame.GetLevel() );
115 
116  wxString name = frame.GetName();
117  if ( !name.empty() )
118  {
119  m_stackTrace << wxString::Format( _T( "%-40s" ), name.c_str() );
120  }
121  else
122  {
123  m_stackTrace << wxString::Format
124  (
125  _T( "0x%08lx" ),
126  ( unsigned long )frame.GetAddress()
127  );
128  }
129 
130  if( m_withArgs )
131  {
132  wxString type, val;
133  size_t n;
134  for ( n = 0; frame.GetParam( n, &type, &name, &val ); n++ )
135  {
136  if( !n )
137  m_stackTrace << wxT( "\t(" );
138  else
139  m_stackTrace << wxT( ", " );
140  m_stackTrace << wxString::Format( wxT( "%s %s=%s" ), type.c_str(), name.c_str(), val.Find( wxT( '\n' ) ) == wxNOT_FOUND ? val.c_str() : wxT( "..." ) );
141  }
142  if( n )
143  m_stackTrace << wxT( " )" );
144  }
145 
146  if ( frame.HasSourceLocation() )
147  {
148  m_stackTrace << _T( '\t' )
149  << frame.GetFileName()
150  << _T( ':' )
151  << frame.GetLine();
152  }
153 
154  m_stackTrace << _T( '\n' );
155  }
156 
157  private:
158  wxString m_stackTrace;
159  bool m_withArgs;
160  int m_maxCalls;
161  int m_curCall;
162  };
163 
164  StackDump dump( maxCalls, withArgs );
165  dump.Walk( skip ); // don't show call itself
166  stackTrace = dump.GetStackTrace();
167 
168  // don't show more than maxLines or we could get a dialog too tall to be
169  // shown on screen: 20 should be ok everywhere as even with 15 pixel high
170  // characters it is still only 300 pixels...
171  const int count = stackTrace.Freq( wxT( '\n' ) );
172  for ( int i = 0; i < count - maxLines; i++ )
173  stackTrace = stackTrace.BeforeLast( wxT( '\n' ) );
174 
175  return stackTrace;
176 }
177 #endif // wxUSE_STACKWALKER
178 #endif // __WXDEBUG__
~a2dMemManager()
destructor of memory manager
Definition: a2dmemmgr.cpp:27
void Deallocate(void *space, size_t bytes)
function for adding memory block by size bytes to list of freed memory blocks
Definition: a2dmemmgr.cpp:71
void * Allocate(size_t bytes)
function for allocating memory block by size bytes
Definition: a2dmemmgr.cpp:47
memory management which can optionally be used for certain intensively used classes.
a2dmemmgr.cpp Source File -- Sun Oct 12 2014 17:04:12 -- Sun Oct 12 2014 -- 1.8.5 -- wxArt2D -- . -- Main Page Reference Documentation