/[pcsx2_0.9.7]/trunk/common/include/Utilities/SafeArray.inl
ViewVC logotype

Annotation of /trunk/common/include/Utilities/SafeArray.inl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (hide annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (9 years, 11 months ago) by william
File size: 7551 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug in ./trunk
1 william 62 /* PCSX2 - PS2 Emulator for PCs
2     * Copyright (C) 2002-2010 PCSX2 Dev Team
3     *
4     * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5     * of the GNU Lesser General Public License as published by the Free Software Found-
6     * ation, either version 3 of the License, or (at your option) any later version.
7     *
8     * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10     * PURPOSE. See the GNU General Public License for more details.
11     *
12     * You should have received a copy of the GNU General Public License along with PCSX2.
13     * If not, see <http://www.gnu.org/licenses/>.
14     */
15    
16     #pragma once
17    
18     #include "MemcpyFast.h"
19     #include "SafeArray.h"
20    
21     // Internal constructor for use by derived classes. This allows a derived class to
22     // use its own memory allocation (with an aligned memory, for example).
23     // Throws:
24     // Exception::OutOfMemory if the allocated_mem pointer is NULL.
25     template< typename T >
26     SafeArray<T>::SafeArray( const wxChar* name, T* allocated_mem, int initSize )
27     : Name( name )
28     {
29     ChunkSize = DefaultChunkSize;
30     m_ptr = allocated_mem;
31     m_size = initSize;
32    
33     if( m_ptr == NULL )
34     throw Exception::OutOfMemory(name + wxsFormat(L" (SafeArray::constructor) [size=%d]", initSize));
35     }
36    
37     template< typename T >
38     T* SafeArray<T>::_virtual_realloc( int newsize )
39     {
40     T* retval = (T*)((m_ptr == NULL) ?
41     malloc( newsize * sizeof(T) ) :
42     realloc( m_ptr, newsize * sizeof(T) )
43     );
44    
45     if( IsDebugBuild )
46     {
47     // Zero everything out to 0xbaadf00d, so that its obviously uncleared
48     // to a debuggee
49    
50     u32* fill = (u32*)&retval[m_size];
51     const u32* end = (u32*)((((uptr)&retval[newsize-1])-3) & ~0x3);
52     for( ; fill<end; ++fill ) *fill = 0xbaadf00d;
53     }
54    
55     return retval;
56     }
57    
58     template< typename T >
59     SafeArray<T>::~SafeArray() throw()
60     {
61     safe_free( m_ptr );
62     }
63    
64     template< typename T >
65     SafeArray<T>::SafeArray( const wxChar* name )
66     : Name( name )
67     {
68     ChunkSize = DefaultChunkSize;
69     m_ptr = NULL;
70     m_size = 0;
71     }
72    
73     template< typename T >
74     SafeArray<T>::SafeArray( int initialSize, const wxChar* name )
75     : Name( name )
76     {
77     ChunkSize = DefaultChunkSize;
78     m_ptr = (initialSize==0) ? NULL : (T*)malloc( initialSize * sizeof(T) );
79     m_size = initialSize;
80    
81     if( (initialSize != 0) && (m_ptr == NULL) )
82     throw Exception::OutOfMemory(name + wxsFormat(L" (SafeArray::constructor) [size=%d]", initialSize));
83     }
84    
85     // Clears the contents of the array to zero, and frees all memory allocations.
86     template< typename T >
87     void SafeArray<T>::Dispose()
88     {
89     m_size = 0;
90     safe_free( m_ptr );
91     }
92    
93     // reallocates the array to the explicit size. Can be used to shrink or grow an
94     // array, and bypasses the internal threshold growth indicators.
95     template< typename T >
96     void SafeArray<T>::ExactAlloc( int newsize )
97     {
98     if( newsize == m_size ) return;
99    
100     m_ptr = _virtual_realloc( newsize );
101     if( m_ptr == NULL )
102     throw Exception::OutOfMemory(Name +
103     wxsFormat(L" (SafeArray::ExactAlloc) [oldsize=%d] [newsize=%d]", m_size, newsize)
104     );
105    
106     m_size = newsize;
107     }
108    
109     template< typename T >
110     SafeArray<T>* SafeArray<T>::Clone() const
111     {
112     SafeArray<T>* retval = new SafeArray<T>( m_size );
113     memcpy_fast( retval->GetPtr(), m_ptr, sizeof(T) * m_size );
114     return retval;
115     }
116    
117    
118     // --------------------------------------------------------------------------------------
119     // SafeAlignedArray<T> (implementations)
120     // --------------------------------------------------------------------------------------
121    
122     template< typename T, uint Alignment >
123     T* SafeAlignedArray<T,Alignment>::_virtual_realloc( int newsize )
124     {
125     return (T*)( ( this->m_ptr == NULL ) ?
126     _aligned_malloc( newsize * sizeof(T), Alignment ) :
127     _aligned_realloc( this->m_ptr, newsize * sizeof(T), Alignment )
128     );
129     }
130    
131     // Appends "(align: xx)" to the name of the allocation in devel builds.
132     // Maybe useful,maybe not... no harm in attaching it. :D
133    
134     template< typename T, uint Alignment >
135     SafeAlignedArray<T,Alignment>::~SafeAlignedArray() throw()
136     {
137     safe_aligned_free( this->m_ptr );
138     // mptr is set to null, so the parent class's destructor won't re-free it.
139     }
140    
141     template< typename T, uint Alignment >
142     SafeAlignedArray<T,Alignment>::SafeAlignedArray( int initialSize, const wxChar* name ) :
143     SafeArray<T>::SafeArray(
144     name,
145     (T*)_aligned_malloc( initialSize * sizeof(T), Alignment ),
146     initialSize
147     )
148     {
149     }
150    
151     template< typename T, uint Alignment >
152     SafeAlignedArray<T,Alignment>* SafeAlignedArray<T,Alignment>::Clone() const
153     {
154     SafeAlignedArray<T,Alignment>* retval = new SafeAlignedArray<T,Alignment>( this->m_size );
155     memcpy_fast( retval->GetPtr(), this->m_ptr, sizeof(T) * this->m_size );
156     return retval;
157     }
158    
159     // --------------------------------------------------------------------------------------
160     // SafeList<T> (implementations)
161     // --------------------------------------------------------------------------------------
162    
163     template< typename T >
164     T* SafeList<T>::_virtual_realloc( int newsize )
165     {
166     return (T*)realloc( m_ptr, newsize * sizeof(T) );
167     }
168    
169     template< typename T >
170     SafeList<T>::~SafeList() throw()
171     {
172     safe_free( m_ptr );
173     }
174    
175     template< typename T >
176     SafeList<T>::SafeList( const wxChar* name )
177     : Name( name )
178     {
179     ChunkSize = DefaultChunkSize;
180     m_ptr = NULL;
181     m_allocsize = 0;
182     m_length = 0;
183     }
184    
185     template< typename T >
186     SafeList<T>::SafeList( int initialSize, const wxChar* name )
187     : Name( name )
188     {
189     ChunkSize = DefaultChunkSize;
190     m_allocsize = initialSize;
191     m_length = 0;
192     m_ptr = (T*)malloc( initialSize * sizeof(T) );
193    
194     if( m_ptr == NULL )
195     throw Exception::OutOfMemory(Name +
196     wxsFormat(L" (SafeList::Constructor) [length=%d]", m_length)
197     );
198    
199     for( int i=0; i<m_allocsize; ++i )
200     {
201     new (&m_ptr[i]) T();
202     }
203    
204     }
205    
206     // Ensures that the allocation is large enough to fit data of the
207     // amount requested. The memory allocation is not resized smaller.
208     template< typename T >
209     void SafeList<T>::MakeRoomFor( int blockSize )
210     {
211     if( blockSize > m_allocsize )
212     {
213     const int newalloc = blockSize + ChunkSize;
214     m_ptr = _virtual_realloc( newalloc );
215     if( m_ptr == NULL )
216     throw Exception::OutOfMemory(Name +
217     wxsFormat(L" (SafeList::MakeRoomFor) [oldlen=%d] [newlen=%d]", m_length, blockSize)
218     );
219    
220     for( ; m_allocsize<newalloc; ++m_allocsize )
221     {
222     new (&m_ptr[m_allocsize]) T();
223     }
224     }
225     }
226    
227     // Appends an item to the end of the list and returns a handle to it.
228     template< typename T >
229     T& SafeList<T>::New()
230     {
231     _MakeRoomFor_threshold( m_length + 1 );
232     return m_ptr[m_length++];
233     }
234    
235     template< typename T >
236     int SafeList<T>::Add( const T& src )
237     {
238     _MakeRoomFor_threshold( m_length + 1 );
239     m_ptr[m_length] = src;
240     return m_length++;
241     }
242    
243     // Same as Add, but returns the handle of the new object instead of it's array index.
244     template< typename T >
245     T& SafeList<T>::AddNew( const T& src )
246     {
247     _MakeRoomFor_threshold( m_length + 1 );
248     m_ptr[m_length] = src;
249     return m_ptr[m_length];
250     }
251    
252     // Performs a standard array-copy removal of the given item. All items past the
253     // given item are copied over.
254     // DevBuilds : Generates assertion if the index is invalid.
255     template< typename T >
256     void SafeList<T>::Remove( int index )
257     {
258     IndexBoundsAssumeDev( Name.c_str(), index, m_length );
259    
260     int copylen = m_length - index;
261     if( copylen > 0 )
262     memcpy_fast( &m_ptr[index], &m_ptr[index+1], copylen );
263     }
264    
265     template< typename T >
266     SafeList<T>* SafeList<T>::Clone() const
267     {
268     SafeList<T>* retval = new SafeList<T>( m_length );
269     memcpy_fast( retval->m_ptr, m_ptr, sizeof(T) * m_length );
270     return retval;
271     }
272    
273     template< typename T >
274     void SafeList<T>::_MakeRoomFor_threshold( int newsize )
275     {
276     MakeRoomFor( newsize + ChunkSize );
277     }

  ViewVC Help
Powered by ViewVC 1.1.22