1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-10-26 07:13:49 +01: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

@@ -68,7 +68,7 @@ void ComputeLegacySummaryInfo(const wxFileName &fileName,
// 64K summary data
//
float *summary = new float[info->frames64K * fields];
Floats summary{ info->frames64K * fields };
SampleBuffer data(info->frames64K * fields,
info->format);
@@ -100,7 +100,7 @@ void ComputeLegacySummaryInfo(const wxFileName &fileName,
int count = read / info->bytesPerFrame;
CopySamples(data.ptr(), info->format,
(samplePtr)summary, floatSample, count);
(samplePtr)summary.get(), floatSample, count);
(*min) = FLT_MAX;
(*max) = FLT_MIN;
@@ -118,8 +118,6 @@ void ComputeLegacySummaryInfo(const wxFileName &fileName,
(*rms) = sqrt(sumsq / count);
else
(*rms) = 0;
delete[] summary;
}
/// 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);
/* 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.
delete [] mFileNameChar;
mFileNameChar = new char[strlen(mFileName.GetFullPath().mb_str(wxConvUTF8))+1];
strcpy(mFileNameChar,mFileName.GetFullPath().mb_str(wxConvUTF8)); */
mFileNameChar.reinit(strlen(mFileName.GetFullPath().mb_str(wxConvUTF8))+1);
strcpy(mFileNameChar.get(), mFileName.GetFullPath().mb_str(wxConvUTF8)); */
mFileNameMutex.Unlock();
}
@@ -409,6 +408,7 @@ void *ODDecodeBlockFile::CalcSummary(samplePtr buffer, size_t len,
float *summary64K = (float *)(localFullSummary + mSummaryInfo.offset64K);
float *summary256 = (float *)(localFullSummary + mSummaryInfo.offset256);
Floats floats;
float *fbuffer;
//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
{
fbuffer = new float[len];
floats.reinit(len);
fbuffer = floats.get();
CopySamples(buffer, format,
(samplePtr)fbuffer, floatSample, len);
}
BlockFile::CalcSummaryFromBuffer(fbuffer, len, summary256, summary64K);
//if we've used the float sample..
if(format!=floatSample)
{
delete[] fbuffer;
}
return localFullSummary;
}

View File

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

View File

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

View File

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