From 7ceb4005cdba0e9cfce46e77f6d055389fc52315 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sun, 24 May 2020 16:17:54 +0200 Subject: [PATCH] Don't use alloca in inlined functions inside a loop. --- server/TracyWorker.cpp | 23 +++++++++++++++++++---- server/TracyWorker.hpp | 3 +++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/server/TracyWorker.cpp b/server/TracyWorker.cpp index ba995837..a4227828 100644 --- a/server/TracyWorker.cpp +++ b/server/TracyWorker.cpp @@ -2122,6 +2122,7 @@ Worker::~Worker() LZ4_freeStreamDecode( (LZ4_streamDecode_t*)m_stream ); delete[] m_frameImageBuffer; + delete[] m_tmpBuf; for( auto& v : m_data.threads ) { @@ -4733,11 +4734,18 @@ void Worker::ProcessZoneText( const QueueZoneText& ev ) const auto str1 = it->second.ptr; const auto len0 = strlen( str0 ); const auto len1 = strlen( str1 ); - char* buf = (char*)alloca( len0+len1+1 ); + const auto bsz = len0+len1+1; + if( m_tmpBufSize < bsz ) + { + delete[] m_tmpBuf; + m_tmpBuf = new char[bsz]; + m_tmpBufSize = bsz; + } + char* buf = m_tmpBuf; memcpy( buf, str0, len0 ); buf[len0] = '\n'; memcpy( buf+len0+1, str1, len1 ); - extra.text = StringIdx( StoreString( buf, len0+len1+1 ).idx ); + extra.text = StringIdx( StoreString( buf, bsz ).idx ); } m_pendingCustomStrings.erase( it ); } @@ -4785,11 +4793,18 @@ void Worker::ProcessZoneValue( const QueueZoneValue& ev ) { const auto str0 = GetString( extra.text ); const auto len0 = strlen( str0 ); - char* buf = (char*)alloca( len0+tsz+1 ); + const auto bsz = len0+tsz+1; + if( m_tmpBufSize < bsz ) + { + delete[] m_tmpBuf; + m_tmpBuf = new char[bsz]; + m_tmpBufSize = bsz; + } + char* buf = m_tmpBuf; memcpy( buf, str0, len0 ); buf[len0] = '\n'; memcpy( buf+len0+1, tmp, tsz ); - extra.text = StringIdx( StoreString( buf, len0+tsz+1 ).idx ); + extra.text = StringIdx( StoreString( buf, bsz ).idx ); } } diff --git a/server/TracyWorker.hpp b/server/TracyWorker.hpp index 6f02cc3b..a49a20a8 100644 --- a/server/TracyWorker.hpp +++ b/server/TracyWorker.hpp @@ -884,6 +884,9 @@ private: #endif Vector m_params; + + char* m_tmpBuf = nullptr; + size_t m_tmpBufSize = 0; }; }