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
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/Pcsx2Defs.h"
|
2020-11-24 03:25:47 +00:00
|
|
|
|
2009-09-29 19:16:00 +00:00
|
|
|
// Microsoft Windows only macro, useful for freeing out COM objects:
|
2016-11-12 15:28:37 +00:00
|
|
|
#define safe_release(ptr) \
|
2021-09-06 18:28:26 +00:00
|
|
|
((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
|
|
|
//
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2009-08-20 14:34:48 +00:00
|
|
|
class SafeArray
|
2009-02-09 21:15:56 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
DeclareNoncopyableObject(SafeArray);
|
2009-08-20 14:34:48 +00:00
|
|
|
|
2009-02-09 21:15:56 +00:00
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
static const int DefaultChunkSize = 0x1000 * sizeof(T);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2009-10-29 13:32:40 +00:00
|
|
|
public:
|
2022-05-18 13:27:23 +00:00
|
|
|
std::string Name; // user-assigned block name
|
2021-09-06 18:28:26 +00:00
|
|
|
int ChunkSize;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
protected:
|
2021-09-06 18:28:26 +00:00
|
|
|
T* m_ptr;
|
|
|
|
int m_size; // size of the allocation of memory
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
protected:
|
2022-05-18 13:27:23 +00:00
|
|
|
SafeArray(std::string name, T* allocated_mem, int initSize);
|
2021-09-06 18:28:26 +00:00
|
|
|
virtual T* _virtual_realloc(int newsize);
|
2009-02-09 21:15:56 +00:00
|
|
|
|
2021-09-06 18:28:26 +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).
|
|
|
|
T* _getPtr(uint i) const;
|
2009-02-09 21:15:56 +00:00
|
|
|
|
|
|
|
public:
|
2021-09-06 18:28:26 +00:00
|
|
|
virtual ~SafeArray();
|
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
explicit SafeArray(std::string name = "Unnamed");
|
|
|
|
explicit SafeArray(int initialSize, std::string name = "Unnamed");
|
2021-09-06 18:28:26 +00:00
|
|
|
|
|
|
|
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;
|
2009-02-09 21:15:56 +00:00
|
|
|
};
|