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