mirror of
https://github.com/PCSX2/pcsx2.git
synced 2024-10-07 15:33:29 +00:00
13dfceff48
* common: format AlignedMalloc.cpp * common: format AppTrait.h * common: format Assertions.h * common: format CheckedStaticBox * common: format Console * common: format Dependencies.h * common: format EmbeddedImage * common: format EventSource * common: format Exceptions * common: format FastFormatString.cpp * common: format General.h * common: format InitInterface * common: format MathUtils.h * common: format MemsetFast/MemcpyFast * common: format Mutex.cpp * common: format PageFaultSource.h * common: format Path.h * common: format PathUtils.cpp * common: format Pcsx2Types.h * common: format Perf * common: format PersistentThread.h * common: format RwMutex * common: format SafeArray * common: format ScopedAlloc.h * common: format ScopedPtrMT.h * common: format Semaphore.cpp * common: format StringHelpers * common: format ThreadTools.cpp * common: format Threading.h * common: format ThreadingDialogs * common: format ThreadingInternal.h * common: format TraceLog.h * common: format VirtualMemory.cpp * common: format pxCheckBox * common: format pxEvents.h * common: format pxForwardDefs.h * common: format pxRadioPanel * common: format pxStaticText * common: format pxStreams * common: format pxTranslate.cpp * common: format pxWindowTextWriter.cpp * common: format wxAppWithHelpers * common: format wxBaseTools.h * common: format wxGuiTools * common: format wxHelpers.cpp * common: format Darwin directory * common: format Linux directory * common: format Windows directory * common: format LnxCpuDetect.cpp * common: format WinCpuDetect.cpp * common: format bmi.cpp * common: format cpudetect.cpp * common: format cpu_detect_internal.h * common: format fpu.cpp * common: format groups.cpp * common: format instructions.h * common: format internal.h * common: format jmp.cpp * common: format legacy.cpp * common: format legacy_instructions.h * common: format legacy_internal.h * common: format movs.cpp * common: format simd.cpp * common: format tools.h * common: format x86emitter.cpp * common: format x86types.h * common: format bmi.h * common: format dwshift.h * common: format group1.h group2.h group3.h * common: format incdec.h * common: format jmpcall.h * common: format movs.h * common: format simd_arithmetic.h * common: format simd_comparisons.h * common: format simd_helpers.h * common: format simd_moremovs.h * common: format simd_shufflepack.h * common: format simd_templated_helpers.h * common: format test.h
218 lines
6.9 KiB
C++
218 lines
6.9 KiB
C++
/* 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 "common/Dependencies.h"
|
|
|
|
// pxUSE_SECURE_MALLOC - enables bounds checking on scoped malloc allocations.
|
|
|
|
#ifndef pxUSE_SECURE_MALLOC
|
|
#define pxUSE_SECURE_MALLOC 0
|
|
#endif
|
|
|
|
|
|
// Microsoft Windows only macro, useful for freeing out COM objects:
|
|
#define safe_release(ptr) \
|
|
((void)((((ptr) != NULL) && ((ptr)->Release(), !!0)), (ptr) = NULL))
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
// SafeArray
|
|
// --------------------------------------------------------------------------------------
|
|
// Handy little class for allocating a resizable memory block, complete with exception
|
|
// error handling and automatic cleanup. A lightweight alternative to std::vector.
|
|
//
|
|
template <typename T>
|
|
class SafeArray
|
|
{
|
|
DeclareNoncopyableObject(SafeArray);
|
|
|
|
public:
|
|
static const int DefaultChunkSize = 0x1000 * sizeof(T);
|
|
|
|
public:
|
|
wxString Name; // user-assigned block name
|
|
int ChunkSize;
|
|
|
|
protected:
|
|
T* m_ptr;
|
|
int m_size; // size of the allocation of memory
|
|
|
|
protected:
|
|
SafeArray(const wxChar* name, T* allocated_mem, int initSize);
|
|
virtual T* _virtual_realloc(int newsize);
|
|
|
|
// 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).
|
|
T* _getPtr(uint i) const;
|
|
|
|
public:
|
|
virtual ~SafeArray();
|
|
|
|
explicit SafeArray(const wxChar* name = L"Unnamed");
|
|
explicit SafeArray(int initialSize, const wxChar* name = L"Unnamed");
|
|
|
|
void Dispose();
|
|
void ExactAlloc(int newsize);
|
|
void MakeRoomFor(int newsize)
|
|
{
|
|
if (newsize > m_size)
|
|
ExactAlloc(newsize);
|
|
}
|
|
|
|
bool IsDisposed() const { return (m_ptr == NULL); }
|
|
|
|
// 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); }
|
|
|
|
// Extends the containment area of the array. Extensions are performed
|
|
// in chunks.
|
|
void GrowBy(int items)
|
|
{
|
|
MakeRoomFor(m_size + ChunkSize + items + 1);
|
|
}
|
|
|
|
// Gets a pointer to the requested allocation index.
|
|
// DevBuilds : Generates assertion if the index is invalid.
|
|
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. :)
|
|
T* GetPtrEnd() { return &m_ptr[m_size]; }
|
|
const T* GetPtrEnd() const { return &m_ptr[m_size]; }
|
|
|
|
// Gets an element of this memory allocation much as if it were an array.
|
|
// DevBuilds : Generates assertion if the index is invalid.
|
|
T& operator[](int idx) { return *_getPtr((uint)idx); }
|
|
const T& operator[](int idx) const { return *_getPtr((uint)idx); }
|
|
|
|
virtual SafeArray<T>* Clone() const;
|
|
};
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
// SafeList - Simple growable container without all the mess or hassle of std containers.
|
|
//
|
|
// This container is intended for reasonably simple class types only. Things which this
|
|
// container does not handle with desired robustness:
|
|
//
|
|
// * 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!)
|
|
//
|
|
template <typename T>
|
|
class SafeList
|
|
{
|
|
DeclareNoncopyableObject(SafeList);
|
|
|
|
public:
|
|
static const int DefaultChunkSize = 0x80 * sizeof(T);
|
|
|
|
public:
|
|
wxString Name; // user-assigned block name
|
|
int ChunkSize; // assigned DefaultChunkSize on init, reconfigurable at any time.
|
|
|
|
protected:
|
|
T* m_ptr;
|
|
int m_allocsize; // size of the allocation of memory
|
|
uint m_length; // length of the array (active items, not buffer allocation)
|
|
|
|
protected:
|
|
virtual T* _virtual_realloc(int newsize);
|
|
void _MakeRoomFor_threshold(int newsize);
|
|
|
|
T* _getPtr(uint i) const;
|
|
|
|
public:
|
|
virtual ~SafeList();
|
|
explicit SafeList(const wxChar* name = L"Unnamed");
|
|
explicit SafeList(int initialSize, const wxChar* name = L"Unnamed");
|
|
virtual SafeList<T>* Clone() const;
|
|
|
|
void Remove(int index);
|
|
void MakeRoomFor(int blockSize);
|
|
|
|
T& New();
|
|
int Add(const T& src);
|
|
T& AddNew(const T& src);
|
|
|
|
// 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); }
|
|
|
|
void MatchLengthToAllocatedSize()
|
|
{
|
|
m_length = m_allocsize;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// Gets an element of this memory allocation much as if it were an array.
|
|
// DevBuilds : Generates assertion if the index is invalid.
|
|
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]; }
|
|
};
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
// SafeAlignedArray<T>
|
|
// --------------------------------------------------------------------------------------
|
|
// 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>
|
|
class SafeAlignedArray : public SafeArray<T>
|
|
{
|
|
typedef SafeArray<T> _parent;
|
|
|
|
protected:
|
|
T* _virtual_realloc(int newsize);
|
|
|
|
public:
|
|
using _parent::operator[];
|
|
|
|
virtual ~SafeAlignedArray();
|
|
|
|
explicit SafeAlignedArray(const wxChar* name = L"Unnamed")
|
|
: SafeArray<T>::SafeArray(name)
|
|
{
|
|
}
|
|
|
|
explicit SafeAlignedArray(int initialSize, const wxChar* name = L"Unnamed");
|
|
virtual SafeAlignedArray<T, Alignment>* Clone() const;
|
|
};
|