2009-09-08 12:08:10 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2010-04-25 00:31:27 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* 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.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* 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.
|
2009-02-09 21:15:56 +00:00
|
|
|
*
|
2009-09-08 12:08:10 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
2009-02-09 21:15:56 +00:00
|
|
|
*/
|
|
|
|
|
2009-04-28 20:26:43 +00:00
|
|
|
#pragma once
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-10-18 01:40:49 +00:00
|
|
|
// pxUSE_SECURE_MALLOC - enables bounds checking on scoped malloc allocations.
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-10-18 01:40:49 +00:00
|
|
|
#ifndef pxUSE_SECURE_MALLOC
|
|
|
|
#define pxUSE_SECURE_MALLOC 0
|
|
|
|
#endif
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
|
2009-09-29 19:16:00 +00:00
|
|
|
// Microsoft Windows only macro, useful for freeing out COM objects:
|
|
|
|
#define safe_release( ptr ) \
|
|
|
|
((void) (( ( (ptr) != NULL ) && ((ptr)->Release(), !!0) ), (ptr) = NULL))
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// SafeArray
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// Handy little class for allocating a resizable memory block, complete with exception
|
|
|
|
// error handling and automatic cleanup. A lightweight alternative to std::vector.
|
2009-03-06 19:01:30 +00:00
|
|
|
//
|
2009-02-09 21:15:56 +00:00
|
|
|
template< typename T >
|
2009-08-20 14:34:48 +00:00
|
|
|
class SafeArray
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-09-21 03:33:35 +00:00
|
|
|
DeclareNoncopyableObject(SafeArray);
|
2009-08-20 14:34:48 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
public:
|
|
|
|
static const int DefaultChunkSize = 0x1000 * sizeof(T);
|
|
|
|
|
2009-10-29 13:32:40 +00:00
|
|
|
public:
|
|
|
|
wxString Name; // user-assigned block name
|
|
|
|
int ChunkSize;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
protected:
|
2010-06-28 10:21:04 +00:00
|
|
|
T* m_ptr;
|
|
|
|
int m_size; // size of the allocation of memory
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
protected:
|
2010-08-05 01:39:31 +00:00
|
|
|
SafeArray( const wxChar* name, T* allocated_mem, int initSize );
|
|
|
|
virtual T* _virtual_realloc( int newsize );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
// A safe array index fetcher. Asserts if the index is out of bounds (dev and debug
|
|
|
|
// builds only -- no bounds checking is done in release builds).
|
2010-10-18 01:40:49 +00:00
|
|
|
T* _getPtr( uint i ) const;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
public:
|
2010-08-05 01:39:31 +00:00
|
|
|
virtual ~SafeArray() throw();
|
2010-05-28 16:50:49 +00:00
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
explicit SafeArray( const wxChar* name=L"Unnamed" );
|
|
|
|
explicit SafeArray( int initialSize, const wxChar* name=L"Unnamed" );
|
2010-08-03 22:18:19 +00:00
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
void Dispose();
|
|
|
|
void ExactAlloc( int newsize );
|
|
|
|
void MakeRoomFor( int newsize )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-08-05 01:39:31 +00:00
|
|
|
if( newsize > m_size )
|
|
|
|
ExactAlloc( newsize );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 13:13:59 +00:00
|
|
|
bool IsDisposed() const { return (m_ptr==NULL); }
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
// Returns the size of the memory allocation, as according to the array type.
|
|
|
|
int GetLength() const { return m_size; }
|
|
|
|
// Returns the size of the memory allocation in bytes.
|
|
|
|
int GetSizeInBytes() const { return m_size * sizeof(T); }
|
|
|
|
|
2009-06-24 13:13:59 +00:00
|
|
|
// Extends the containment area of the array. Extensions are performed
|
|
|
|
// in chunks.
|
|
|
|
void GrowBy( int items )
|
|
|
|
{
|
|
|
|
MakeRoomFor( m_size + ChunkSize + items + 1 );
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Gets a pointer to the requested allocation index.
|
2009-11-04 09:30:22 +00:00
|
|
|
// DevBuilds : Generates assertion if the index is invalid.
|
2010-11-22 16:24:54 +00:00
|
|
|
T* GetPtr( uint idx=0 ) { return _getPtr( idx ); }
|
|
|
|
const T* GetPtr( uint idx=0 ) const { return _getPtr( idx ); }
|
|
|
|
|
|
|
|
// Gets a pointer to the element directly after the last element in the array.
|
|
|
|
// This is equivalent to doing GetPtr(GetLength()), except that this call *avoids*
|
|
|
|
// the out-of-bounds assertion check that typically occurs when you do that. :)
|
2014-07-04 22:06:26 +00:00
|
|
|
T* GetPtrEnd() { return &m_ptr[m_size]; }
|
|
|
|
const T* GetPtrEnd() const { return &m_ptr[m_size]; }
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
// Gets an element of this memory allocation much as if it were an array.
|
2009-11-04 09:30:22 +00:00
|
|
|
// DevBuilds : Generates assertion if the index is invalid.
|
2009-02-09 21:15:56 +00:00
|
|
|
T& operator[]( int idx ) { return *_getPtr( (uint)idx ); }
|
|
|
|
const T& operator[]( int idx ) const { return *_getPtr( (uint)idx ); }
|
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
virtual SafeArray<T>* Clone() const;
|
2009-02-09 21:15:56 +00:00
|
|
|
};
|
|
|
|
|
2009-03-06 19:01:30 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
2009-06-24 13:13:59 +00:00
|
|
|
// SafeList - Simple growable container without all the mess or hassle of std containers.
|
2009-03-06 19:01:30 +00:00
|
|
|
//
|
2009-06-24 13:13:59 +00:00
|
|
|
// This container is intended for reasonably simple class types only. Things which this
|
|
|
|
// container does not handle with desired robustness:
|
2009-03-06 19:01:30 +00:00
|
|
|
//
|
2009-06-24 13:13:59 +00:00
|
|
|
// * Classes with non-trivial constructors (such that construction creates much overhead)
|
|
|
|
// * Classes with copy constructors (copying is done using performance memcpy)
|
|
|
|
// * Classes with destructors (they're not called, sorry!)
|
2009-03-06 19:01:30 +00:00
|
|
|
//
|
|
|
|
template< typename T >
|
2009-08-20 14:34:48 +00:00
|
|
|
class SafeList
|
2009-03-06 19:01:30 +00:00
|
|
|
{
|
2009-09-21 03:33:35 +00:00
|
|
|
DeclareNoncopyableObject(SafeList);
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-03-06 19:01:30 +00:00
|
|
|
public:
|
|
|
|
static const int DefaultChunkSize = 0x80 * sizeof(T);
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
public:
|
2009-10-29 13:32:40 +00:00
|
|
|
wxString Name; // user-assigned block name
|
|
|
|
int ChunkSize; // assigned DefaultChunkSize on init, reconfigurable at any time.
|
2009-03-06 19:01:30 +00:00
|
|
|
|
|
|
|
protected:
|
2009-10-29 13:32:40 +00:00
|
|
|
T* m_ptr;
|
|
|
|
int m_allocsize; // size of the allocation of memory
|
|
|
|
uint m_length; // length of the array (active items, not buffer allocation)
|
2009-03-06 19:01:30 +00:00
|
|
|
|
|
|
|
protected:
|
2010-08-05 01:39:31 +00:00
|
|
|
virtual T* _virtual_realloc( int newsize );
|
|
|
|
void _MakeRoomFor_threshold( int newsize );
|
2009-03-06 19:01:30 +00:00
|
|
|
|
2010-10-18 01:40:49 +00:00
|
|
|
T* _getPtr( uint i ) const;
|
2009-03-06 19:01:30 +00:00
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
public:
|
|
|
|
virtual ~SafeList() throw();
|
|
|
|
explicit SafeList( const wxChar* name=L"Unnamed" );
|
|
|
|
explicit SafeList( int initialSize, const wxChar* name=L"Unnamed" );
|
|
|
|
virtual SafeList<T>* Clone() const;
|
2009-03-06 19:01:30 +00:00
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
void Remove( int index );
|
|
|
|
void MakeRoomFor( int blockSize );
|
2009-06-24 13:13:59 +00:00
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
T& New();
|
|
|
|
int Add( const T& src );
|
|
|
|
T& AddNew( const T& src );
|
2009-04-27 02:04:31 +00:00
|
|
|
|
2009-03-06 19:01:30 +00:00
|
|
|
// Returns the size of the list, as according to the array type. This includes
|
|
|
|
// mapped items only. The actual size of the allocation may differ.
|
|
|
|
int GetLength() const { return m_length; }
|
|
|
|
|
|
|
|
// Returns the size of the list, in bytes. This includes mapped items only.
|
|
|
|
// The actual size of the allocation may differ.
|
|
|
|
int GetSizeInBytes() const { return m_length * sizeof(T); }
|
|
|
|
|
2009-09-16 17:23:02 +00:00
|
|
|
void MatchLengthToAllocatedSize()
|
|
|
|
{
|
|
|
|
m_length = m_allocsize;
|
|
|
|
}
|
|
|
|
|
2009-06-24 13:13:59 +00:00
|
|
|
void GrowBy( int items )
|
|
|
|
{
|
|
|
|
MakeRoomFor( m_length + ChunkSize + items + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the item length to zero. Does not free memory allocations.
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
m_length = 0;
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-03-06 19:01:30 +00:00
|
|
|
// Gets an element of this memory allocation much as if it were an array.
|
2009-11-04 09:30:22 +00:00
|
|
|
// DevBuilds : Generates assertion if the index is invalid.
|
2009-06-24 13:13:59 +00:00
|
|
|
T& operator[]( int idx ) { return *_getPtr( (uint)idx ); }
|
|
|
|
const T& operator[]( int idx ) const { return *_getPtr( (uint)idx ); }
|
|
|
|
|
|
|
|
T* GetPtr() { return m_ptr; }
|
|
|
|
const T* GetPtr() const { return m_ptr; }
|
|
|
|
|
|
|
|
T& GetLast() { return m_ptr[m_length-1]; }
|
|
|
|
const T& GetLast() const{ return m_ptr[m_length-1]; }
|
2009-03-06 19:01:30 +00:00
|
|
|
};
|
|
|
|
|
2009-09-16 17:23:02 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
2010-08-05 01:39:31 +00:00
|
|
|
// SafeAlignedArray<T>
|
2009-09-16 17:23:02 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
2009-02-09 21:15:56 +00:00
|
|
|
// Handy little class for allocating a resizable memory block, complete with
|
|
|
|
// exception-based error handling and automatic cleanup.
|
|
|
|
// This one supports aligned data allocations too!
|
|
|
|
|
|
|
|
template< typename T, uint Alignment >
|
2009-02-10 11:34:35 +00:00
|
|
|
class SafeAlignedArray : public SafeArray<T>
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-08-03 22:18:19 +00:00
|
|
|
typedef SafeArray<T> _parent;
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
protected:
|
2010-08-05 01:39:31 +00:00
|
|
|
T* _virtual_realloc( int newsize );
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
public:
|
2010-08-03 22:18:19 +00:00
|
|
|
using _parent::operator[];
|
|
|
|
|
2010-08-05 09:54:59 +00:00
|
|
|
virtual ~SafeAlignedArray() throw();
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
explicit SafeAlignedArray( const wxChar* name=L"Unnamed" ) :
|
2009-04-27 02:04:31 +00:00
|
|
|
SafeArray<T>::SafeArray( name )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
explicit SafeAlignedArray( int initialSize, const wxChar* name=L"Unnamed" );
|
|
|
|
virtual SafeAlignedArray<T,Alignment>* Clone() const;
|
2009-02-09 21:15:56 +00:00
|
|
|
};
|
|
|
|
|