2010-08-05 01:39:31 +00:00
|
|
|
/* 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
|
|
|
|
|
2022-05-18 13:27:23 +00:00
|
|
|
#include "common/Assertions.h"
|
2021-09-03 10:43:33 +00:00
|
|
|
#include "common/SafeArray.h"
|
2022-05-18 13:27:23 +00:00
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
// 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.
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2022-05-18 13:27:23 +00:00
|
|
|
SafeArray<T>::SafeArray(std::string name, T* allocated_mem, int initSize)
|
|
|
|
: Name(std::move(name))
|
2010-08-05 01:39:31 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
ChunkSize = DefaultChunkSize;
|
|
|
|
m_ptr = allocated_mem;
|
|
|
|
m_size = initSize;
|
2010-08-05 01:39:31 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
if (m_ptr == NULL)
|
2022-10-12 15:42:36 +00:00
|
|
|
pxFailRel("SafeArray memory assignment failed");
|
2010-08-05 01:39:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2021-09-06 18:28:26 +00:00
|
|
|
T* SafeArray<T>::_virtual_realloc(int newsize)
|
2010-08-05 01:39:31 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
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;
|
2010-08-05 01:39:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2017-05-06 12:22:00 +00:00
|
|
|
SafeArray<T>::~SafeArray()
|
2010-08-05 01:39:31 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
safe_free(m_ptr);
|
2010-08-05 01:39:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2022-05-18 13:27:23 +00:00
|
|
|
SafeArray<T>::SafeArray(std::string name)
|
|
|
|
: Name(std::move(name))
|
2010-08-05 01:39:31 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
ChunkSize = DefaultChunkSize;
|
|
|
|
m_ptr = NULL;
|
|
|
|
m_size = 0;
|
2010-08-05 01:39:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2022-05-18 13:27:23 +00:00
|
|
|
SafeArray<T>::SafeArray(int initialSize, std::string name)
|
|
|
|
: Name(std::move(name))
|
2010-08-05 01:39:31 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
ChunkSize = DefaultChunkSize;
|
|
|
|
m_ptr = (initialSize == 0) ? NULL : (T*)malloc(initialSize * sizeof(T));
|
|
|
|
m_size = initialSize;
|
2010-08-05 01:39:31 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
if ((initialSize != 0) && (m_ptr == NULL))
|
2022-10-12 15:42:36 +00:00
|
|
|
pxFailRel("SafeArray memory allocation failed");
|
2010-08-05 01:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clears the contents of the array to zero, and frees all memory allocations.
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2010-08-05 01:39:31 +00:00
|
|
|
void SafeArray<T>::Dispose()
|
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
m_size = 0;
|
|
|
|
safe_free(m_ptr);
|
2010-08-05 01:39:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2021-09-06 18:28:26 +00:00
|
|
|
T* SafeArray<T>::_getPtr(uint i) const
|
2010-10-18 01:40:49 +00:00
|
|
|
{
|
2022-05-18 13:27:23 +00:00
|
|
|
pxAssumeDev(i < static_cast<uint>(m_size), "Array index in bounds");
|
2021-09-06 18:28:26 +00:00
|
|
|
return &m_ptr[i];
|
2010-10-18 01:40:49 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 01:39:31 +00:00
|
|
|
// reallocates the array to the explicit size. Can be used to shrink or grow an
|
|
|
|
// array, and bypasses the internal threshold growth indicators.
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
|
|
|
void SafeArray<T>::ExactAlloc(int newsize)
|
2010-08-05 01:39:31 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
if (newsize == m_size)
|
|
|
|
return;
|
2010-08-05 01:39:31 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
m_ptr = _virtual_realloc(newsize);
|
|
|
|
if (m_ptr == NULL)
|
2022-10-12 15:42:36 +00:00
|
|
|
pxFailRel("SafeArray exact alloc failed");
|
2010-08-05 01:39:31 +00:00
|
|
|
|
2021-09-06 18:28:26 +00:00
|
|
|
m_size = newsize;
|
2010-08-05 01:39:31 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
template <typename T>
|
2021-09-06 18:28:26 +00:00
|
|
|
SafeArray<T>* SafeArray<T>::Clone() const
|
2010-08-05 01:39:31 +00:00
|
|
|
{
|
2021-09-06 18:28:26 +00:00
|
|
|
SafeArray<T>* retval = new SafeArray<T>(m_size);
|
|
|
|
memcpy(retval->GetPtr(), m_ptr, sizeof(T) * m_size);
|
|
|
|
return retval;
|
2010-08-05 01:39:31 +00:00
|
|
|
}
|