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
|
|
|
|
2009-07-16 09:58:17 +00:00
|
|
|
#include "MemcpyFast.h"
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
extern void* __fastcall pcsx2_aligned_malloc(size_t size, size_t align);
|
|
|
|
extern void* __fastcall pcsx2_aligned_realloc(void* handle, size_t size, size_t align);
|
|
|
|
extern void pcsx2_aligned_free(void* pmem);
|
|
|
|
|
|
|
|
// aligned_malloc: Implement/declare linux equivalents here!
|
|
|
|
#if !defined(_MSC_VER) && !defined(HAVE_ALIGNED_MALLOC)
|
|
|
|
# define _aligned_malloc pcsx2_aligned_malloc
|
|
|
|
# define _aligned_free pcsx2_aligned_free
|
|
|
|
# define _aligned_realloc pcsx2_aligned_realloc
|
|
|
|
#endif
|
|
|
|
|
2009-08-18 19:47:00 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Safe deallocation macros -- checks pointer validity (non-null) when needed, and sets
|
|
|
|
// pointer to null after deallocation.
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
#define safe_delete( ptr ) \
|
2009-09-29 19:16:00 +00:00
|
|
|
((void) (delete (ptr)), (ptr) = NULL)
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
#define safe_delete_array( ptr ) \
|
2009-09-29 19:16:00 +00:00
|
|
|
((void) (delete[] (ptr)), (ptr) = NULL)
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2010-05-28 16:50:49 +00:00
|
|
|
// No checks for NULL -- wxWidgets says it's safe to skip NULL checks and it runs on
|
|
|
|
// just about every compiler and libc implementation of any recentness.
|
2009-02-09 21:15:56 +00:00
|
|
|
#define safe_free( ptr ) \
|
2010-05-28 16:50:49 +00:00
|
|
|
( (void) (free( ptr ), !!0), (ptr) = NULL )
|
|
|
|
//((void) (( ( (ptr) != NULL ) && (free( ptr ), !!0) ), (ptr) = NULL))
|
2009-09-29 19:16:00 +00:00
|
|
|
|
|
|
|
#define safe_fclose( ptr ) \
|
|
|
|
((void) (( ( (ptr) != NULL ) && (fclose( ptr ), !!0) ), (ptr) = NULL))
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-08-18 19:47:00 +00:00
|
|
|
// Implementation note: all known implementations of _aligned_free check the pointer for
|
|
|
|
// NULL status (our implementation under GCC, and microsoft's under MSVC), so no need to
|
|
|
|
// do it here.
|
2009-02-09 21:15:56 +00:00
|
|
|
#define safe_aligned_free( ptr ) \
|
2009-09-29 19:16:00 +00:00
|
|
|
((void) ( _aligned_free( ptr ), (ptr) = NULL ))
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
#define SafeSysMunmap( ptr, size ) \
|
2009-09-29 19:16:00 +00:00
|
|
|
((void) ( HostSys::Munmap( (uptr)(ptr), size ), (ptr) = NULL ))
|
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
|
|
|
|
2009-03-06 19:01:30 +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.
|
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:
|
2009-10-29 13:32:40 +00:00
|
|
|
T* m_ptr;
|
|
|
|
int m_size; // size of the allocation of memory
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
protected:
|
2009-03-06 19:01:30 +00:00
|
|
|
// Internal constructor for use by derived classes. This allows a derived class to
|
2009-02-09 21:15:56 +00:00
|
|
|
// use its own memory allocation (with an aligned memory, for example).
|
|
|
|
// Throws:
|
2009-03-06 19:01:30 +00:00
|
|
|
// Exception::OutOfMemory if the allocated_mem pointer is NULL.
|
2010-05-28 16:50:49 +00:00
|
|
|
explicit SafeArray( const wxChar* name, T* allocated_mem, int initSize )
|
|
|
|
: Name( name )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-05-28 16:50:49 +00:00
|
|
|
ChunkSize = DefaultChunkSize;
|
|
|
|
m_ptr = allocated_mem;
|
|
|
|
m_size = initSize;
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
if( m_ptr == NULL )
|
|
|
|
throw Exception::OutOfMemory();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual T* _virtual_realloc( int newsize )
|
|
|
|
{
|
2010-04-25 00:31:27 +00:00
|
|
|
return (T*)((m_ptr == NULL) ?
|
2009-06-24 13:13:59 +00:00
|
|
|
malloc( newsize * sizeof(T) ) :
|
|
|
|
realloc( m_ptr, newsize * sizeof(T) )
|
|
|
|
);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2009-02-10 11:34:35 +00:00
|
|
|
virtual ~SafeArray()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
|
|
|
safe_free( m_ptr );
|
|
|
|
}
|
|
|
|
|
2010-05-28 16:50:49 +00:00
|
|
|
explicit SafeArray( const wxChar* name=L"Unnamed" )
|
|
|
|
: Name( name )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2010-05-28 16:50:49 +00:00
|
|
|
ChunkSize = DefaultChunkSize;
|
|
|
|
m_ptr = NULL;
|
|
|
|
m_size = 0;
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
2010-05-28 16:50:49 +00:00
|
|
|
explicit SafeArray( int initialSize, const wxChar* name=L"Unnamed" )
|
|
|
|
: Name( name )
|
2009-04-27 02:04:31 +00:00
|
|
|
{
|
2010-05-28 16:50:49 +00:00
|
|
|
ChunkSize = DefaultChunkSize;
|
|
|
|
m_ptr = (initialSize==0) ? NULL : (T*)malloc( initialSize * sizeof(T) );
|
|
|
|
m_size = initialSize;
|
|
|
|
|
2009-06-24 13:13:59 +00:00
|
|
|
if( (initialSize != 0) && (m_ptr == NULL) )
|
2009-04-27 02:04:31 +00:00
|
|
|
throw Exception::OutOfMemory();
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-06-24 13:13:59 +00:00
|
|
|
// Clears the contents of the array to zero, and frees all memory allocations.
|
|
|
|
void Dispose()
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-24 13:13:59 +00:00
|
|
|
m_size = 0;
|
|
|
|
safe_free( m_ptr );
|
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
|
|
|
// reallocates the array to the explicit size. Can be used to shrink or grow an
|
2010-04-25 00:31:27 +00:00
|
|
|
// array, and bypasses the internal threshold growth indicators.
|
2009-06-24 13:13:59 +00:00
|
|
|
void ExactAlloc( int newsize )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-06-24 13:13:59 +00:00
|
|
|
if( newsize == m_size ) return;
|
|
|
|
|
|
|
|
m_ptr = _virtual_realloc( newsize );
|
|
|
|
if( m_ptr == NULL )
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-10-09 17:28:17 +00:00
|
|
|
throw Exception::OutOfMemory(
|
|
|
|
wxsFormat( // english (for diagnostic)
|
|
|
|
L"Out-of-memory on SafeArray block re-allocation.\n"
|
|
|
|
L"Old size: %d bytes, New size: %d bytes.",
|
|
|
|
m_size, newsize
|
|
|
|
),
|
|
|
|
// internationalized!
|
|
|
|
wxsFormat( _("Out of memory, trying to allocate %d bytes."), newsize )
|
|
|
|
);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
2009-06-24 13:13:59 +00:00
|
|
|
m_size = newsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures that the allocation is large enough to fit data of the
|
|
|
|
// amount requested. The memory allocation is not resized smaller.
|
|
|
|
void MakeRoomFor( int newsize )
|
|
|
|
{
|
|
|
|
if( newsize > m_size )
|
|
|
|
ExactAlloc( newsize );
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
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.
|
2009-02-09 21:15:56 +00:00
|
|
|
T *GetPtr( uint idx=0 ) { return _getPtr( idx ); }
|
|
|
|
const T *GetPtr( uint idx=0 ) const { return _getPtr( idx ); }
|
|
|
|
|
|
|
|
// 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 ); }
|
|
|
|
|
2009-02-10 11:34:35 +00:00
|
|
|
virtual SafeArray<T>* Clone() const
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2009-02-10 11:34:35 +00:00
|
|
|
SafeArray<T>* retval = new SafeArray<T>( m_size );
|
2009-02-09 21:15:56 +00:00
|
|
|
memcpy_fast( retval->GetPtr(), m_ptr, sizeof(T) * m_size );
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// A safe array index fetcher. Throws an exception if the array index
|
|
|
|
// is outside the bounds of the array.
|
|
|
|
// Performance Considerations: This function adds quite a bit of overhead
|
|
|
|
// to array indexing and thus should be done infrequently if used in
|
2009-02-10 11:34:35 +00:00
|
|
|
// time-critical situations. Instead of using it from inside loops, cache
|
2009-03-06 19:01:30 +00:00
|
|
|
// the pointer into a local variable and use std (unsafe) C indexes.
|
2009-02-09 21:15:56 +00:00
|
|
|
T* _getPtr( uint i ) const
|
|
|
|
{
|
2009-11-04 09:30:22 +00:00
|
|
|
IndexBoundsCheckDev( Name.c_str(), i, m_size );
|
2009-02-09 21:15:56 +00:00
|
|
|
return &m_ptr[i];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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:
|
2009-03-07 00:48:57 +00:00
|
|
|
virtual T* _virtual_realloc( int newsize )
|
|
|
|
{
|
|
|
|
return (T*)realloc( m_ptr, newsize * sizeof(T) );
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-04-28 20:26:43 +00:00
|
|
|
public:
|
2009-09-16 17:23:02 +00:00
|
|
|
virtual ~SafeList() throw()
|
2009-03-06 19:01:30 +00:00
|
|
|
{
|
2009-06-24 13:13:59 +00:00
|
|
|
safe_free( m_ptr );
|
2009-03-06 19:01:30 +00:00
|
|
|
}
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
explicit SafeList( const wxChar* name=L"Unnamed" ) :
|
2009-03-06 19:01:30 +00:00
|
|
|
Name( name )
|
|
|
|
, ChunkSize( DefaultChunkSize )
|
|
|
|
, m_ptr( NULL )
|
|
|
|
, m_allocsize( 0 )
|
|
|
|
, m_length( 0 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-04-25 00:31:27 +00:00
|
|
|
explicit SafeList( int initialSize, const wxChar* name=L"Unnamed" ) :
|
2009-03-06 19:01:30 +00:00
|
|
|
Name( name )
|
|
|
|
, ChunkSize( DefaultChunkSize )
|
|
|
|
, m_ptr( (T*)malloc( initialSize * sizeof(T) ) )
|
|
|
|
, m_allocsize( initialSize )
|
|
|
|
, m_length( 0 )
|
|
|
|
{
|
|
|
|
if( m_ptr == NULL )
|
|
|
|
throw Exception::OutOfMemory();
|
|
|
|
|
2009-06-24 13:13:59 +00:00
|
|
|
for( int i=0; i<m_allocsize; ++i )
|
|
|
|
{
|
|
|
|
new (&m_ptr[i]) T();
|
|
|
|
}
|
|
|
|
|
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-03-06 19:01:30 +00:00
|
|
|
// Ensures that the allocation is large enough to fit data of the
|
|
|
|
// amount requested. The memory allocation is not resized smaller.
|
|
|
|
void MakeRoomFor( int blockSize )
|
|
|
|
{
|
|
|
|
if( blockSize > m_allocsize )
|
|
|
|
{
|
2009-06-24 13:13:59 +00:00
|
|
|
const int newalloc = blockSize + ChunkSize;
|
2009-03-06 19:01:30 +00:00
|
|
|
m_ptr = _virtual_realloc( newalloc );
|
|
|
|
if( m_ptr == NULL )
|
|
|
|
{
|
|
|
|
throw Exception::OutOfMemory(
|
User Interface:
* Fixed and added better Emulation/System menu updating. Suspend/Resume is more consistent, and Reset grays itself out after being used.
* Entering plugin configurations auto-suspends the emulator.
* Changing plugins in the Configuration PAnel takes effect now without a restart.
* Added preliminary support for an ExtensibleConfirmation Dialog box (contains a sizer you can add content to, and also has an optional "[x] Do not show this again" checkbox).
Bugfixes:
* Added some mutex protection to cdvdNewDiskCB; "just in case."
* Resolved several recursion and deadlock scenarios when (very!) rapidly suspending, resuming, and resetting the emu.
Developments / Code Cleanups:
* Renamed SysCoreThread ExecutionModes: Suspend/Resume are now Opened/Closed (which more accurately reflects the fact they opena nd close the plugins, and helps avoid ambiguity with the "Paused" state).
* Added Exception::ThreadTimedOut, which is now thrown from Semaphore::Wait when recursive wxApp::Yield() calls are detected, and a deadlock occurs (basically cancels the current action which, most of the time, allows for full recovery).
* Major Threading namespace cleanups, documentations, etc.
* Removed wxScopedArray (scopedarray.h) and replaced it with a better implemeneted ScopedArray class.
* Removed toUTF8 class, which I only added a couple weeks ago because I didn't realize wxCharBuffer had an implicit typecast to (char*).
* Implemented more Source/Listener events for Pcsx2App. CoreThread events are sourced properly now, and added SettingsApplied and SettingsLoadSave Sources.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@2010 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-16 03:58:29 +00:00
|
|
|
// English Diagnostic message:
|
2009-04-27 02:04:31 +00:00
|
|
|
wxsFormat(
|
2009-05-01 02:15:18 +00:00
|
|
|
L"Out-of-memory on SafeList block re-allocation.\n"
|
User Interface:
* Fixed and added better Emulation/System menu updating. Suspend/Resume is more consistent, and Reset grays itself out after being used.
* Entering plugin configurations auto-suspends the emulator.
* Changing plugins in the Configuration PAnel takes effect now without a restart.
* Added preliminary support for an ExtensibleConfirmation Dialog box (contains a sizer you can add content to, and also has an optional "[x] Do not show this again" checkbox).
Bugfixes:
* Added some mutex protection to cdvdNewDiskCB; "just in case."
* Resolved several recursion and deadlock scenarios when (very!) rapidly suspending, resuming, and resetting the emu.
Developments / Code Cleanups:
* Renamed SysCoreThread ExecutionModes: Suspend/Resume are now Opened/Closed (which more accurately reflects the fact they opena nd close the plugins, and helps avoid ambiguity with the "Paused" state).
* Added Exception::ThreadTimedOut, which is now thrown from Semaphore::Wait when recursive wxApp::Yield() calls are detected, and a deadlock occurs (basically cancels the current action which, most of the time, allows for full recovery).
* Major Threading namespace cleanups, documentations, etc.
* Removed wxScopedArray (scopedarray.h) and replaced it with a better implemeneted ScopedArray class.
* Removed toUTF8 class, which I only added a couple weeks ago because I didn't realize wxCharBuffer had an implicit typecast to (char*).
* Implemented more Source/Listener events for Pcsx2App. CoreThread events are sourced properly now, and added SettingsApplied and SettingsLoadSave Sources.
git-svn-id: http://pcsx2.googlecode.com/svn/trunk@2010 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-16 03:58:29 +00:00
|
|
|
L"Name: %s, Old size: %d bytes, New size: %d bytes",
|
2009-10-29 13:32:40 +00:00
|
|
|
Name.c_str(), m_allocsize, newalloc
|
2009-09-01 00:43:28 +00:00
|
|
|
),
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-09-01 00:43:28 +00:00
|
|
|
wxsFormat( _("Out of memory, trying to allocate %d bytes."), newalloc )
|
2009-03-06 19:01:30 +00:00
|
|
|
);
|
|
|
|
}
|
2009-06-24 13:13:59 +00:00
|
|
|
|
|
|
|
for( ; m_allocsize<newalloc; ++m_allocsize )
|
|
|
|
{
|
|
|
|
new (&m_ptr[m_allocsize]) T();
|
|
|
|
}
|
2009-03-06 19:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-24 13:13:59 +00:00
|
|
|
// Appends an item to the end of the list and returns a handle to it.
|
|
|
|
T& New()
|
|
|
|
{
|
|
|
|
_MakeRoomFor_threshold( m_length + 1 );
|
|
|
|
return m_ptr[m_length++];
|
|
|
|
}
|
|
|
|
|
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-04-28 20:26:43 +00:00
|
|
|
|
2009-03-06 19:01:30 +00:00
|
|
|
int Add( const T& src )
|
|
|
|
{
|
2009-06-24 13:13:59 +00:00
|
|
|
_MakeRoomFor_threshold( m_length + 1 );
|
2009-03-06 19:01:30 +00:00
|
|
|
m_ptr[m_length] = src;
|
|
|
|
return m_length++;
|
|
|
|
}
|
2009-04-28 20:26:43 +00:00
|
|
|
|
2009-06-24 13:13:59 +00:00
|
|
|
// Same as Add, but returns the handle of the new object instead of it's array index.
|
|
|
|
T& AddNew( const T& src )
|
|
|
|
{
|
|
|
|
_MakeRoomFor_threshold( m_length + 1 );
|
|
|
|
m_ptr[m_length] = src;
|
|
|
|
return m_ptr[m_length];
|
|
|
|
}
|
2010-04-25 00:31:27 +00:00
|
|
|
|
2009-03-06 19:01:30 +00:00
|
|
|
// Performs a standard array-copy removal of the given item. All items past the
|
2009-11-04 09:30:22 +00:00
|
|
|
// given item are copied over.
|
|
|
|
// DevBuilds : Generates assertion if the index is invalid.
|
2009-03-06 19:01:30 +00:00
|
|
|
void Remove( int index )
|
|
|
|
{
|
2009-11-04 09:30:22 +00:00
|
|
|
IndexBoundsCheckDev( Name.c_str(), index, m_length );
|
|
|
|
|
2009-03-06 19:01:30 +00:00
|
|
|
int copylen = m_length - index;
|
|
|
|
if( copylen > 0 )
|
|
|
|
memcpy_fast( &m_ptr[index], &m_ptr[index+1], copylen );
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SafeList<T>* Clone() const
|
|
|
|
{
|
|
|
|
SafeList<T>* retval = new SafeList<T>( m_length );
|
2009-04-02 11:30:23 +00:00
|
|
|
memcpy_fast( retval->m_ptr, m_ptr, sizeof(T) * m_length );
|
2009-03-06 19:01:30 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2009-06-24 13:13:59 +00:00
|
|
|
|
|
|
|
void _MakeRoomFor_threshold( int newsize )
|
|
|
|
{
|
|
|
|
MakeRoomFor( newsize + ChunkSize );
|
|
|
|
}
|
|
|
|
|
2009-03-06 19:01:30 +00:00
|
|
|
// A safe array index fetcher. Throws an exception if the array index
|
|
|
|
// is outside the bounds of the array.
|
|
|
|
// Performance Considerations: This function adds quite a bit of overhead
|
|
|
|
// to array indexing and thus should be done infrequently if used in
|
|
|
|
// time-critical situations. Instead of using it from inside loops, cache
|
|
|
|
// the pointer into a local variable and use std (unsafe) C indexes.
|
|
|
|
T* _getPtr( uint i ) const
|
|
|
|
{
|
2009-11-04 09:30:22 +00:00
|
|
|
IndexBoundsCheckDev( Name.c_str(), i, m_length );
|
2009-03-06 19:01:30 +00:00
|
|
|
return &m_ptr[i];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-09-16 17:23:02 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// SafeAlignedArray class
|
|
|
|
// --------------------------------------------------------------------------------------
|
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
|
|
|
{
|
|
|
|
protected:
|
|
|
|
T* _virtual_realloc( int newsize )
|
|
|
|
{
|
2009-06-26 00:30:19 +00:00
|
|
|
return (T*)( ( this->m_ptr == NULL ) ?
|
2009-06-24 13:13:59 +00:00
|
|
|
_aligned_malloc( newsize * sizeof(T), Alignment ) :
|
2009-06-26 00:30:19 +00:00
|
|
|
_aligned_realloc( this->m_ptr, newsize * sizeof(T), Alignment )
|
2009-06-24 13:13:59 +00:00
|
|
|
);
|
2009-02-09 21:15:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Appends "(align: xx)" to the name of the allocation in devel builds.
|
2009-03-06 19:01:30 +00:00
|
|
|
// Maybe useful,maybe not... no harm in attaching it. :D
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~SafeAlignedArray()
|
|
|
|
{
|
|
|
|
safe_aligned_free( this->m_ptr );
|
|
|
|
// mptr is set to null, so the parent class's destructor won't re-free it.
|
|
|
|
}
|
|
|
|
|
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-04-25 00:31:27 +00:00
|
|
|
explicit SafeAlignedArray( int initialSize, const wxChar* name=L"Unnamed" ) :
|
2009-04-27 02:04:31 +00:00
|
|
|
SafeArray<T>::SafeArray(
|
2009-06-24 13:13:59 +00:00
|
|
|
name,
|
2009-04-27 02:04:31 +00:00
|
|
|
(T*)_aligned_malloc( initialSize * sizeof(T), Alignment ),
|
2009-04-28 20:26:43 +00:00
|
|
|
initialSize
|
2009-04-27 02:04:31 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
virtual SafeAlignedArray<T,Alignment>* Clone() const
|
|
|
|
{
|
|
|
|
SafeAlignedArray<T,Alignment>* retval = new SafeAlignedArray<T,Alignment>( this->m_size );
|
|
|
|
memcpy_fast( retval->GetPtr(), this->m_ptr, sizeof(T) * this->m_size );
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-27 02:04:31 +00:00
|
|
|
|
|
|
|
// For lack of a better place for now (they depend on SafeList so they can't go in StringUtil)
|
|
|
|
extern void SplitString( SafeList<wxString>& dest, const wxString& src, const wxString& delims );
|
|
|
|
extern void JoinString( wxString& dest, const SafeList<wxString>& src, const wxString& separator );
|