diff --git a/sakura_core/mem/CMemory.cpp b/sakura_core/mem/CMemory.cpp index 0916447a16..e4f82c55a4 100644 --- a/sakura_core/mem/CMemory.cpp +++ b/sakura_core/mem/CMemory.cpp @@ -312,10 +312,6 @@ void CMemory::AllocBuffer( int nNewDataLen ) }else{ /* ���݂̃o�b�t�@�T�C�Y���傫���Ȃ����ꍇ�̂ݍĊm�ۂ��� */ if( m_nDataBufSize < nWorkLen ){ - // �p�ɂȍĊm�ۂ��s��Ȃ��悤�ɂ���ׁA�K�v�ʂ̔{�̃T�C�Y���m�ۂ��� - if (nWorkLen < std::numeric_limits::max() / 2) { - nWorkLen *= 2; - } // 2014.06.25 �L���f�[�^����0�̏ꍇ��free & malloc if( m_nRawLen == 0 ){ free( m_pRawData ); @@ -399,7 +395,7 @@ void CMemory::SetRawDataHoldBuffer( const CMemory& pcmemData ) void CMemory::AppendRawData( const void* pData, int nDataLenBytes ) { if(nDataLenBytes<=0)return; - AllocBuffer( m_nRawLen + nDataLenBytes ); + _ReallocIfNeeded( nDataLenBytes ); _AddData( pData, nDataLenBytes ); } @@ -412,7 +408,7 @@ void CMemory::AppendRawData( const CMemory* pcmemData ) } int nDataLen; const void* pData = pcmemData->GetRawPtr( &nDataLen ); - AllocBuffer( m_nRawLen + nDataLen ); + _ReallocIfNeeded( nDataLen ); _AddData( pData, nDataLen ); } @@ -425,12 +421,10 @@ void CMemory::_Empty( void ) return; } - - void CMemory::_AppendSz(const char* str) { int len=strlen(str); - AllocBuffer( m_nRawLen + len ); + _ReallocIfNeeded(len); _AddData(str,len); } @@ -443,3 +437,15 @@ void CMemory::_SetRawLength(int nLength) m_pRawData[m_nRawLen ]=0; m_pRawData[m_nRawLen+1]=0; //�I�['\0'��2�•t������('\0''\0'==L'\0')�B } + +void CMemory::_ReallocIfNeeded(int appendLength) +{ + assert(appendLength >= 0); + if(m_nDataBufSize - 2 - m_nRawLen < appendLength) { + int64_t newSize1 = (int64_t)m_nRawLen + 2 + appendLength; + int64_t newSize2 = (int64_t)m_nDataBufSize * 2; + int64_t newSize = std::min(std::max(newSize1, newSize2), (int64_t)std::numeric_limits::max()); + AllocBuffer( (int)newSize ); + } +} + diff --git a/sakura_core/mem/CMemory.h b/sakura_core/mem/CMemory.h index 6e39ec0101..65e0024441 100644 --- a/sakura_core/mem/CMemory.h +++ b/sakura_core/mem/CMemory.h @@ -87,6 +87,7 @@ class CMemory */ void _Empty( void ); //!< �������Bm_pRawData��NULL�ɂȂ�B void _AddData( const void*, int ); + void _ReallocIfNeeded(int appendLength); public: void _AppendSz(const char* str); void _SetRawLength(int nLength);