2010-05-20 12:23:13 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PrecompiledHeader.h"
|
|
|
|
#include "Threading.h"
|
2010-08-05 01:39:31 +00:00
|
|
|
#include "TlsVariable.inl"
|
|
|
|
#include "SafeArray.inl"
|
2010-05-20 12:23:13 +00:00
|
|
|
|
|
|
|
using namespace Threading;
|
|
|
|
|
2010-08-05 12:48:49 +00:00
|
|
|
// Implement some very commonly used SafeArray types here
|
|
|
|
// (done here for lack of a better place)
|
|
|
|
|
|
|
|
template class SafeArray<char>;
|
|
|
|
template class SafeArray<wchar_t>;
|
|
|
|
template class SafeArray<u8>;
|
|
|
|
|
|
|
|
template class SafeAlignedArray<char,16>;
|
|
|
|
template class SafeAlignedArray<wchar_t,16>;
|
|
|
|
template class SafeAlignedArray<u8,16>;
|
|
|
|
|
2010-05-20 12:23:13 +00:00
|
|
|
// Sanity check: truncate strings if they exceed 512k in length. Anything like that
|
|
|
|
// is either a bug or really horrible code that needs to be stopped before it causes
|
|
|
|
// system deadlock.
|
|
|
|
static const int MaxFormattedStringLength = 0x80000;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
2010-08-03 22:18:19 +00:00
|
|
|
// FastFormatBuffers
|
2010-05-20 12:23:13 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
template< typename CharType >
|
2010-08-03 22:18:19 +00:00
|
|
|
class FastFormatBuffers
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
2010-08-03 22:18:19 +00:00
|
|
|
DeclareNoncopyableObject(FastFormatBuffers);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef SafeAlignedArray<CharType,16> BufferType;
|
|
|
|
|
|
|
|
static const uint BufferCount = 3;
|
|
|
|
|
|
|
|
BufferType m_buffers[BufferCount];
|
|
|
|
uint m_curslot;
|
|
|
|
|
2010-05-20 12:23:13 +00:00
|
|
|
public:
|
2010-08-03 22:18:19 +00:00
|
|
|
FastFormatBuffers()
|
|
|
|
{
|
|
|
|
// This protects against potential recursive calls to our formatter, by forcing those
|
|
|
|
// calls to use a dynamic buffer for formatting.
|
|
|
|
m_curslot = BufferCount;
|
|
|
|
|
|
|
|
for (uint i=0; i<BufferCount; ++i)
|
|
|
|
{
|
|
|
|
m_buffers[i].Name = wxsFormat(L"%s Formatting Buffer (slot%d)",
|
|
|
|
(sizeof(CharType)==1) ? L"Ascii" : L"Unicode", i);
|
|
|
|
m_buffers[i].MakeRoomFor(1024);
|
|
|
|
m_buffers[i].ChunkSize = 4096;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_curslot = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~FastFormatBuffers() throw()
|
|
|
|
{
|
|
|
|
pxAssumeDev(m_curslot==0, "Dangling Ascii formatting buffer detected!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasFreeBuffer() const
|
|
|
|
{
|
|
|
|
return m_curslot < BufferCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferType& GrabBuffer()
|
|
|
|
{
|
|
|
|
++m_curslot;
|
|
|
|
pxAssume(m_curslot<BufferCount);
|
|
|
|
return m_buffers[m_curslot];
|
|
|
|
}
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
void ReleaseBuffer()
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
2010-08-03 22:18:19 +00:00
|
|
|
--m_curslot;
|
|
|
|
pxAssume(m_curslot<BufferCount);
|
2010-05-20 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
BufferType& operator[](uint i)
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
2010-08-05 12:48:49 +00:00
|
|
|
IndexBoundsAssume( ((sizeof(CharType)==1) ? L"Ascii Formatting Buffer" : L"Unicode Formatting Buffer"), i, BufferCount );
|
2010-08-03 22:18:19 +00:00
|
|
|
return m_buffers[i];
|
2010-05-20 12:23:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// GlobalBufferManager
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// This local-scope class is needed in order to safely deal with C++ initializing and destroying
|
|
|
|
// global objects in arbitrary order. The initbit is updated by the object when constructed and
|
|
|
|
// destroyed; code using this class provides its own statically-initialized boolean (which MUST
|
|
|
|
// default to false!) and then sets the boolean to true to indicate the object is ready for use.
|
|
|
|
//
|
|
|
|
template< typename T >
|
|
|
|
class GlobalBufferManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
bool& initbit;
|
|
|
|
T instance;
|
|
|
|
|
|
|
|
GlobalBufferManager( bool& globalBoolean )
|
|
|
|
: initbit( globalBoolean )
|
|
|
|
{
|
|
|
|
initbit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
~GlobalBufferManager() throw()
|
|
|
|
{
|
|
|
|
initbit = false;
|
|
|
|
instance.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
T& Get()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
operator T&()
|
|
|
|
{
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
};
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
static bool buffer_is_avail = false;
|
|
|
|
static GlobalBufferManager< BaseTlsVariable< FastFormatBuffers< char > > > m_buffer_tls(buffer_is_avail);
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2010-08-09 04:10:38 +00:00
|
|
|
static __ri void format_that_ascii_mess( SafeArray<char>& buffer, uint writepos, const char* fmt, va_list argptr )
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
|
|
|
while( true )
|
|
|
|
{
|
|
|
|
int size = buffer.GetLength();
|
2010-08-03 22:18:19 +00:00
|
|
|
int len = vsnprintf(buffer.GetPtr(writepos), size-writepos, fmt, argptr);
|
2010-05-20 12:23:13 +00:00
|
|
|
|
|
|
|
// some implementations of vsnprintf() don't NUL terminate
|
|
|
|
// the string if there is not enough space for it so
|
|
|
|
// always do it manually
|
|
|
|
buffer[size-1] = '\0';
|
|
|
|
|
2010-08-06 05:46:09 +00:00
|
|
|
if (size >= MaxFormattedStringLength) break;
|
2010-05-20 12:23:13 +00:00
|
|
|
|
|
|
|
// vsnprintf() may return either -1 (traditional Unix behavior) or the
|
|
|
|
// total number of characters which would have been written if the
|
|
|
|
// buffer were large enough (newer standards such as Unix98)
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
if (len < 0)
|
2010-05-20 12:23:13 +00:00
|
|
|
len = size + (size/4);
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
len += writepos;
|
|
|
|
if (len < size) break;
|
|
|
|
buffer.ExactAlloc( len + 31 );
|
2010-05-20 12:23:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// performing an assertion or log of a truncated string is unsafe, so let's not; even
|
|
|
|
// though it'd be kinda nice if we did.
|
|
|
|
}
|
|
|
|
|
2010-08-09 04:10:38 +00:00
|
|
|
static __ri void format_that_unicode_mess( SafeArray<char>& buffer, uint writepos, const wxChar* fmt, va_list argptr)
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
|
|
|
while( true )
|
|
|
|
{
|
2010-08-17 12:09:03 +00:00
|
|
|
int size = buffer.GetLength() / sizeof(wxChar);
|
2010-08-03 22:18:19 +00:00
|
|
|
int len = wxVsnprintf((wxChar*)buffer.GetPtr(writepos), size-writepos, fmt, argptr);
|
2010-05-20 12:23:13 +00:00
|
|
|
|
|
|
|
// some implementations of vsnprintf() don't NUL terminate
|
|
|
|
// the string if there is not enough space for it so
|
|
|
|
// always do it manually
|
2010-08-03 22:18:19 +00:00
|
|
|
((wxChar*)buffer.GetPtr())[size-1] = L'\0';
|
2010-05-20 12:23:13 +00:00
|
|
|
|
|
|
|
if( size >= MaxFormattedStringLength ) break;
|
|
|
|
|
|
|
|
// vsnprintf() may return either -1 (traditional Unix behavior) or the
|
|
|
|
// total number of characters which would have been written if the
|
|
|
|
// buffer were large enough (newer standards such as Unix98)
|
|
|
|
|
2010-08-06 05:46:09 +00:00
|
|
|
if (len < 0)
|
2010-05-20 12:23:13 +00:00
|
|
|
len = size + (size/4);
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
len += writepos;
|
2010-08-06 05:46:09 +00:00
|
|
|
if (len < size) break;
|
2010-08-17 12:09:03 +00:00
|
|
|
buffer.ExactAlloc( (len + 31) * sizeof(wxChar) );
|
2010-05-20 12:23:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// performing an assertion or log of a truncated string is unsafe, so let's not; even
|
|
|
|
// though it'd be kinda nice if we did.
|
|
|
|
}
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
SafeArray<char>* GetFormatBuffer( bool& deleteDest )
|
2010-07-04 11:50:12 +00:00
|
|
|
{
|
2010-08-03 22:18:19 +00:00
|
|
|
deleteDest = false;
|
|
|
|
if (buffer_is_avail)
|
2010-07-04 11:50:12 +00:00
|
|
|
{
|
2010-08-03 22:18:19 +00:00
|
|
|
if (m_buffer_tls.Get()->HasFreeBuffer())
|
|
|
|
return &m_buffer_tls.Get()->GrabBuffer();
|
2010-07-04 11:50:12 +00:00
|
|
|
}
|
2010-08-03 22:18:19 +00:00
|
|
|
|
|
|
|
deleteDest = true;
|
|
|
|
return new SafeArray<char>(2048, L"Temporary string formatting buffer");
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// FastFormatUnicode (implementations)
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
FastFormatUnicode::FastFormatUnicode()
|
|
|
|
{
|
|
|
|
m_dest = GetFormatBuffer(m_deleteDest);
|
|
|
|
((wxChar*)m_dest->GetPtr())[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FastFormatUnicode::~FastFormatUnicode() throw()
|
|
|
|
{
|
|
|
|
if (m_deleteDest)
|
|
|
|
delete m_dest;
|
2010-07-04 11:50:12 +00:00
|
|
|
else
|
2010-08-03 22:18:19 +00:00
|
|
|
m_buffer_tls.Get()->ReleaseBuffer();
|
|
|
|
}
|
2010-07-04 11:50:12 +00:00
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::WriteV( const char* fmt, va_list argptr )
|
|
|
|
{
|
|
|
|
wxString converted( fromUTF8(FastFormatAscii().WriteV( fmt, argptr )) );
|
|
|
|
|
|
|
|
uint inspos = wxStrlen((wxChar*)m_dest->GetPtr());
|
2010-08-17 12:09:03 +00:00
|
|
|
m_dest->MakeRoomFor((inspos + converted.Length() + 31)*sizeof(wxChar));
|
2010-08-03 22:18:19 +00:00
|
|
|
wxStrcpy( &((wxChar*)m_dest->GetPtr())[inspos], converted );
|
2010-07-04 11:50:12 +00:00
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
return *this;
|
2010-07-04 11:50:12 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::WriteV( const wxChar* fmt, va_list argptr )
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
2010-08-03 22:18:19 +00:00
|
|
|
format_that_unicode_mess( *m_dest, wxStrlen((wxChar*)m_dest->GetPtr()), fmt, argptr );
|
|
|
|
return *this;
|
|
|
|
}
|
2010-05-20 12:23:13 +00:00
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::Write( const char* fmt, ... )
|
|
|
|
{
|
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
WriteV(fmt,list);
|
|
|
|
va_end(list);
|
|
|
|
return *this;
|
2010-05-20 12:23:13 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 22:18:19 +00:00
|
|
|
FastFormatUnicode& FastFormatUnicode::Write( const wxChar* fmt, ... )
|
2010-05-20 12:23:13 +00:00
|
|
|
{
|
2010-08-03 22:18:19 +00:00
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
WriteV(fmt,list);
|
|
|
|
va_end(list);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const wxChar* FastFormatUnicode::GetResult() const
|
|
|
|
{
|
|
|
|
return (wxChar*)m_dest->GetPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
// FastFormatAscii (implementations)
|
|
|
|
// --------------------------------------------------------------------------------------
|
|
|
|
FastFormatAscii::FastFormatAscii()
|
|
|
|
{
|
|
|
|
m_dest = GetFormatBuffer(m_deleteDest);
|
|
|
|
m_dest->GetPtr()[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FastFormatAscii::~FastFormatAscii() throw()
|
|
|
|
{
|
|
|
|
if (m_deleteDest)
|
|
|
|
delete m_dest;
|
2010-05-20 12:23:13 +00:00
|
|
|
else
|
2010-08-03 22:18:19 +00:00
|
|
|
m_buffer_tls.Get()->ReleaseBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
const wxString FastFormatAscii::GetString() const
|
|
|
|
{
|
|
|
|
return fromAscii(m_dest->GetPtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
FastFormatAscii::operator wxString() const
|
|
|
|
{
|
|
|
|
return fromAscii(m_dest->GetPtr());
|
|
|
|
}
|
|
|
|
|
|
|
|
FastFormatAscii& FastFormatAscii::WriteV( const char* fmt, va_list argptr )
|
|
|
|
{
|
|
|
|
format_that_ascii_mess( *m_dest, strlen(m_dest->GetPtr()), fmt, argptr );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
FastFormatAscii& FastFormatAscii::Write( const char* fmt, ... )
|
|
|
|
{
|
|
|
|
va_list list;
|
|
|
|
va_start(list, fmt);
|
|
|
|
WriteV(fmt,list);
|
|
|
|
va_end(list);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* FastFormatAscii::GetResult() const
|
|
|
|
{
|
|
|
|
return m_dest->GetPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
FastFormatAscii::operator const char*() const
|
|
|
|
{
|
|
|
|
return m_dest->GetPtr();
|
2010-05-20 12:23:13 +00:00
|
|
|
}
|
2010-08-05 12:48:49 +00:00
|
|
|
|