1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-05-05 14:18:53 +02:00

Remove naked new[] in: blockfile and commands

This commit is contained in:
Paul Licameli 2016-04-14 12:05:14 -04:00
parent 88cac8cd7e
commit e6db1a57a3
7 changed files with 56 additions and 77 deletions

View File

@ -193,13 +193,11 @@ void *BlockFile::CalcSummary(samplePtr buffer, size_t len,
float *summary64K = (float *)(fullSummary.get() + mSummaryInfo.offset64K); float *summary64K = (float *)(fullSummary.get() + mSummaryInfo.offset64K);
float *summary256 = (float *)(fullSummary.get() + mSummaryInfo.offset256); float *summary256 = (float *)(fullSummary.get() + mSummaryInfo.offset256);
float *fbuffer = new float[len]; Floats fbuffer{ len };
CopySamples(buffer, format, CopySamples(buffer, format,
(samplePtr)fbuffer, floatSample, len); (samplePtr)fbuffer.get(), floatSample, len);
CalcSummaryFromBuffer(fbuffer, len, summary256, summary64K); CalcSummaryFromBuffer(fbuffer.get(), len, summary256, summary64K);
delete[] fbuffer;
return fullSummary.get(); return fullSummary.get();
} }
@ -436,14 +434,14 @@ bool BlockFile::Read256(float *buffer,
{ {
wxASSERT(start >= 0); wxASSERT(start >= 0);
char *summary = new char[mSummaryInfo.totalSummaryBytes]; ArrayOf<char> summary{ mSummaryInfo.totalSummaryBytes };
// FIXME: TRAP_ERR ReadSummary() could return fail. // FIXME: TRAP_ERR ReadSummary() could return fail.
this->ReadSummary(summary); this->ReadSummary(summary.get());
start = std::min( start, mSummaryInfo.frames256 ); start = std::min( start, mSummaryInfo.frames256 );
len = std::min( len, mSummaryInfo.frames256 - start ); len = std::min( len, mSummaryInfo.frames256 - start );
CopySamples(summary + mSummaryInfo.offset256 + (start * mSummaryInfo.bytesPerFrame), CopySamples(summary.get() + mSummaryInfo.offset256 + (start * mSummaryInfo.bytesPerFrame),
mSummaryInfo.format, mSummaryInfo.format,
(samplePtr)buffer, floatSample, len * mSummaryInfo.fields); (samplePtr)buffer, floatSample, len * mSummaryInfo.fields);
@ -456,8 +454,6 @@ bool BlockFile::Read256(float *buffer,
} }
} }
delete[] summary;
return true; return true;
} }
@ -475,14 +471,14 @@ bool BlockFile::Read64K(float *buffer,
{ {
wxASSERT(start >= 0); wxASSERT(start >= 0);
char *summary = new char[mSummaryInfo.totalSummaryBytes]; ArrayOf<char> summary{ mSummaryInfo.totalSummaryBytes };
// FIXME: TRAP_ERR ReadSummary() could return fail. // FIXME: TRAP_ERR ReadSummary() could return fail.
this->ReadSummary(summary); this->ReadSummary(summary.get());
start = std::min( start, mSummaryInfo.frames64K ); start = std::min( start, mSummaryInfo.frames64K );
len = std::min( len, mSummaryInfo.frames64K - start ); len = std::min( len, mSummaryInfo.frames64K - start );
CopySamples(summary + mSummaryInfo.offset64K + CopySamples(summary.get() + mSummaryInfo.offset64K +
(start * mSummaryInfo.bytesPerFrame), (start * mSummaryInfo.bytesPerFrame),
mSummaryInfo.format, mSummaryInfo.format,
(samplePtr)buffer, floatSample, len*mSummaryInfo.fields); (samplePtr)buffer, floatSample, len*mSummaryInfo.fields);
@ -496,8 +492,6 @@ bool BlockFile::Read64K(float *buffer,
} }
} }
delete[] summary;
return true; return true;
} }

View File

@ -68,7 +68,7 @@ void ComputeLegacySummaryInfo(const wxFileName &fileName,
// 64K summary data // 64K summary data
// //
float *summary = new float[info->frames64K * fields]; Floats summary{ info->frames64K * fields };
SampleBuffer data(info->frames64K * fields, SampleBuffer data(info->frames64K * fields,
info->format); info->format);
@ -100,7 +100,7 @@ void ComputeLegacySummaryInfo(const wxFileName &fileName,
int count = read / info->bytesPerFrame; int count = read / info->bytesPerFrame;
CopySamples(data.ptr(), info->format, CopySamples(data.ptr(), info->format,
(samplePtr)summary, floatSample, count); (samplePtr)summary.get(), floatSample, count);
(*min) = FLT_MAX; (*min) = FLT_MAX;
(*max) = FLT_MIN; (*max) = FLT_MIN;
@ -118,8 +118,6 @@ void ComputeLegacySummaryInfo(const wxFileName &fileName,
(*rms) = sqrt(sumsq / count); (*rms) = sqrt(sumsq / count);
else else
(*rms) = 0; (*rms) = 0;
delete[] summary;
} }
/// Construct a LegacyBlockFile memory structure that will point to an /// Construct a LegacyBlockFile memory structure that will point to an

View File

@ -369,9 +369,8 @@ void ODDecodeBlockFile::SetFileName(wxFileNameWrapper &&name)
mFileName=std::move(name); mFileName=std::move(name);
/* mchinen oct 9 2009 don't think we need the char* but leaving it in for now just as a reminder that we might /* mchinen oct 9 2009 don't think we need the char* but leaving it in for now just as a reminder that we might
if wxFileName isn't threadsafe. if wxFileName isn't threadsafe.
delete [] mFileNameChar; mFileNameChar.reinit(strlen(mFileName.GetFullPath().mb_str(wxConvUTF8))+1);
mFileNameChar = new char[strlen(mFileName.GetFullPath().mb_str(wxConvUTF8))+1]; strcpy(mFileNameChar.get(), mFileName.GetFullPath().mb_str(wxConvUTF8)); */
strcpy(mFileNameChar,mFileName.GetFullPath().mb_str(wxConvUTF8)); */
mFileNameMutex.Unlock(); mFileNameMutex.Unlock();
} }
@ -409,6 +408,7 @@ void *ODDecodeBlockFile::CalcSummary(samplePtr buffer, size_t len,
float *summary64K = (float *)(localFullSummary + mSummaryInfo.offset64K); float *summary64K = (float *)(localFullSummary + mSummaryInfo.offset64K);
float *summary256 = (float *)(localFullSummary + mSummaryInfo.offset256); float *summary256 = (float *)(localFullSummary + mSummaryInfo.offset256);
Floats floats;
float *fbuffer; float *fbuffer;
//mchinen: think we can hack this - don't allocate and copy if we don't need to., //mchinen: think we can hack this - don't allocate and copy if we don't need to.,
@ -418,18 +418,14 @@ void *ODDecodeBlockFile::CalcSummary(samplePtr buffer, size_t len,
} }
else else
{ {
fbuffer = new float[len]; floats.reinit(len);
fbuffer = floats.get();
CopySamples(buffer, format, CopySamples(buffer, format,
(samplePtr)fbuffer, floatSample, len); (samplePtr)fbuffer, floatSample, len);
} }
BlockFile::CalcSummaryFromBuffer(fbuffer, len, summary256, summary64K); BlockFile::CalcSummaryFromBuffer(fbuffer, len, summary256, summary64K);
//if we've used the float sample..
if(format!=floatSample)
{
delete[] fbuffer;
}
return localFullSummary; return localFullSummary;
} }

View File

@ -379,24 +379,25 @@ void ODPCMAliasBlockFile::WriteSummary()
// wxFFile summaryFile(mFileName.GetFullPath(), wxT("wb")); // wxFFile summaryFile(mFileName.GetFullPath(), wxT("wb"));
// ...and we use fopen instead. // ...and we use fopen instead.
FILE* summaryFile{};
wxString sFullPath = mFileName.GetFullPath(); wxString sFullPath = mFileName.GetFullPath();
char* fileNameChar = new char[strlen(sFullPath.mb_str(wxConvFile)) + 1]; {
strcpy(fileNameChar, sFullPath.mb_str(wxConvFile)); ArrayOf < char > fileNameChar{ strlen(sFullPath.mb_str(wxConvFile)) + 1 };
FILE* summaryFile = fopen(fileNameChar, "wb"); strcpy(fileNameChar.get(), sFullPath.mb_str(wxConvFile));
summaryFile = fopen(fileNameChar.get(), "wb");
mFileNameMutex.Unlock(); mFileNameMutex.Unlock();
// JKC ANSWER-ME: Whay is IsOpened() commented out? // JKC ANSWER-ME: Whay is IsOpened() commented out?
if( !summaryFile){//.IsOpened() ){ if (!summaryFile){//.IsOpened() ){
// Never silence the Log w.r.t write errors; they always count // Never silence the Log w.r.t write errors; they always count
//however, this is going to be called from a non-main thread, //however, this is going to be called from a non-main thread,
//and wxLog calls are not thread safe. //and wxLog calls are not thread safe.
printf("Unable to write summary data to file: %s", fileNameChar); printf("Unable to write summary data to file: %s", fileNameChar.get());
delete [] fileNameChar; return;
return; }
} }
delete [] fileNameChar;
// To build the summary data, call ReadData (implemented by the // To build the summary data, call ReadData (implemented by the
// derived classes) to get the sample data // derived classes) to get the sample data
@ -449,6 +450,7 @@ void *ODPCMAliasBlockFile::CalcSummary(samplePtr buffer, size_t len,
float *summary64K = (float *)(localFullSummary + mSummaryInfo.offset64K); float *summary64K = (float *)(localFullSummary + mSummaryInfo.offset64K);
float *summary256 = (float *)(localFullSummary + mSummaryInfo.offset256); float *summary256 = (float *)(localFullSummary + mSummaryInfo.offset256);
Floats floats;
float *fbuffer; float *fbuffer;
//mchinen: think we can hack this - don't allocate and copy if we don't need to., //mchinen: think we can hack this - don't allocate and copy if we don't need to.,
@ -458,18 +460,14 @@ void *ODPCMAliasBlockFile::CalcSummary(samplePtr buffer, size_t len,
} }
else else
{ {
fbuffer = new float[len]; floats.reinit(len);
fbuffer = floats.get();
CopySamples(buffer, format, CopySamples(buffer, format,
(samplePtr)fbuffer, floatSample, len); (samplePtr)fbuffer, floatSample, len);
} }
BlockFile::CalcSummaryFromBuffer(fbuffer, len, summary256, summary64K); BlockFile::CalcSummaryFromBuffer(fbuffer, len, summary256, summary64K);
//if we've used the float sample..
if(format!=floatSample)
{
delete[] fbuffer;
}
return localFullSummary; return localFullSummary;
} }

View File

@ -124,13 +124,13 @@ SimpleBlockFile::SimpleBlockFile(wxFileNameWrapper &&baseFileName,
mCache.needWrite = true; mCache.needWrite = true;
mCache.format = format; mCache.format = format;
const auto sampleDataSize = sampleLen * SAMPLE_SIZE(format); const auto sampleDataSize = sampleLen * SAMPLE_SIZE(format);
mCache.sampleData = new char[sampleDataSize]; mCache.sampleData.reinit(sampleDataSize);
memcpy(mCache.sampleData, sampleData, sampleDataSize); memcpy(mCache.sampleData.get(), sampleData, sampleDataSize);
ArrayOf<char> cleanup; ArrayOf<char> cleanup;
void* summaryData = BlockFile::CalcSummary(sampleData, sampleLen, void* summaryData = BlockFile::CalcSummary(sampleData, sampleLen,
format, cleanup); format, cleanup);
mCache.summaryData = new char[mSummaryInfo.totalSummaryBytes]; mCache.summaryData.reinit(mSummaryInfo.totalSummaryBytes);
memcpy(mCache.summaryData, summaryData, memcpy(mCache.summaryData.get(), summaryData,
mSummaryInfo.totalSummaryBytes); mSummaryInfo.totalSummaryBytes);
} }
} }
@ -155,11 +155,6 @@ SimpleBlockFile::SimpleBlockFile(wxFileNameWrapper &&existingFile, size_t len,
SimpleBlockFile::~SimpleBlockFile() SimpleBlockFile::~SimpleBlockFile()
{ {
if (mCache.active)
{
delete[] mCache.sampleData;
delete[] (char *)mCache.summaryData;
}
} }
bool SimpleBlockFile::WriteSimpleBlockFile( bool SimpleBlockFile::WriteSimpleBlockFile(
@ -317,18 +312,18 @@ void SimpleBlockFile::FillCache()
file.Close(); file.Close();
// Read samples into cache // Read samples into cache
mCache.sampleData = new char[mLen * SAMPLE_SIZE(mCache.format)]; mCache.sampleData.reinit(mLen * SAMPLE_SIZE(mCache.format));
if (ReadData(mCache.sampleData, mCache.format, 0, mLen) != mLen) if (ReadData(mCache.sampleData.get(), mCache.format, 0, mLen) != mLen)
{ {
// Could not read all samples // Could not read all samples
delete mCache.sampleData; mCache.sampleData.reset();
return; return;
} }
// Read summary data into cache // Read summary data into cache
mCache.summaryData = new char[mSummaryInfo.totalSummaryBytes]; mCache.summaryData.reinit(mSummaryInfo.totalSummaryBytes);
if (!ReadSummary(mCache.summaryData)) if (!ReadSummary(mCache.summaryData.get()))
memset(mCache.summaryData, 0, mSummaryInfo.totalSummaryBytes); memset(mCache.summaryData.get(), 0, mSummaryInfo.totalSummaryBytes);
// Cache is active but already on disk // Cache is active but already on disk
mCache.active = true; mCache.active = true;
@ -346,7 +341,7 @@ bool SimpleBlockFile::ReadSummary(void *data)
if (mCache.active) if (mCache.active)
{ {
//wxLogDebug("SimpleBlockFile::ReadSummary(): Summary is already in cache."); //wxLogDebug("SimpleBlockFile::ReadSummary(): Summary is already in cache.");
memcpy(data, mCache.summaryData, mSummaryInfo.totalSummaryBytes); memcpy(data, mCache.summaryData.get(), mSummaryInfo.totalSummaryBytes);
return true; return true;
} }
else else
@ -398,7 +393,7 @@ size_t SimpleBlockFile::ReadData(samplePtr data, sampleFormat format,
len = std::min(len, std::max(start, mLen) - start); len = std::min(len, std::max(start, mLen) - start);
CopySamples( CopySamples(
(samplePtr)(((char*)mCache.sampleData) + (samplePtr)(mCache.sampleData.get() +
start * SAMPLE_SIZE(mCache.format)), start * SAMPLE_SIZE(mCache.format)),
mCache.format, data, format, len); mCache.format, data, format, len);
return len; return len;
@ -540,7 +535,6 @@ auto SimpleBlockFile::GetSpaceUsage() const -> DiskByteCount
void SimpleBlockFile::Recover(){ void SimpleBlockFile::Recover(){
wxFFile file(mFileName.GetFullPath(), wxT("wb")); wxFFile file(mFileName.GetFullPath(), wxT("wb"));
//int i;
if( !file.IsOpened() ){ if( !file.IsOpened() ){
// Can't do anything else. // Can't do anything else.
@ -572,8 +566,8 @@ void SimpleBlockFile::WriteCacheToDisk()
if (!GetNeedWriteCacheToDisk()) if (!GetNeedWriteCacheToDisk())
return; return;
if (WriteSimpleBlockFile(mCache.sampleData, mLen, mCache.format, if (WriteSimpleBlockFile(mCache.sampleData.get(), mLen, mCache.format,
mCache.summaryData)) mCache.summaryData.get()))
mCache.needWrite = false; mCache.needWrite = false;
} }

View File

@ -22,8 +22,9 @@ struct SimpleBlockFileCache {
bool active; bool active;
bool needWrite; bool needWrite;
sampleFormat format; sampleFormat format;
samplePtr sampleData; ArrayOf<char> sampleData, summaryData;
void* summaryData;
SimpleBlockFileCache() {}
}; };
// The AU formats we care about // The AU formats we care about

View File

@ -99,8 +99,9 @@ bool CompareAudioCommand::Apply(CommandExecutionContext context)
// Initialize buffers for track data to be analyzed // Initialize buffers for track data to be analyzed
auto buffSize = std::min(mTrack0->GetMaxBlockSize(), mTrack1->GetMaxBlockSize()); auto buffSize = std::min(mTrack0->GetMaxBlockSize(), mTrack1->GetMaxBlockSize());
float *buff0 = new float[buffSize];
float *buff1 = new float[buffSize]; Floats buff0{ buffSize };
Floats buff1{ buffSize };
// Compare tracks block by block // Compare tracks block by block
auto s0 = mTrack0->TimeToLongSamples(mT0); auto s0 = mTrack0->TimeToLongSamples(mT0);
@ -113,8 +114,8 @@ bool CompareAudioCommand::Apply(CommandExecutionContext context)
auto block = limitSampleBufferSize( auto block = limitSampleBufferSize(
mTrack0->GetBestBlockSize(position), s1 - position mTrack0->GetBestBlockSize(position), s1 - position
); );
mTrack0->Get((samplePtr)buff0, floatSample, position, block); mTrack0->Get((samplePtr)buff0.get(), floatSample, position, block);
mTrack1->Get((samplePtr)buff1, floatSample, position, block); mTrack1->Get((samplePtr)buff1.get(), floatSample, position, block);
for (decltype(block) buffPos = 0; buffPos < block; ++buffPos) for (decltype(block) buffPos = 0; buffPos < block; ++buffPos)
{ {
@ -131,9 +132,6 @@ bool CompareAudioCommand::Apply(CommandExecutionContext context)
); );
} }
delete [] buff0;
delete [] buff1;
// Output the results // Output the results
double errorSeconds = mTrack0->LongSamplesToTime(errorCount); double errorSeconds = mTrack0->LongSamplesToTime(errorCount);
Status(wxString::Format(wxT("%li"), errorCount)); Status(wxString::Format(wxT("%li"), errorCount));