From 4c7ad7a0b65555ba1570756e3afc1e6b4d7c44f9 Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Fri, 11 May 2018 21:26:22 -0230 Subject: [PATCH] Replace 'new' calls with unique_ptr. - @DirtHairy, you can revert this if you like, but unless there is some issue, I'd rather use smart pointers with auto-deallocation. --- src/common/AudioQueue.cxx | 14 ++++--------- src/common/AudioQueue.hxx | 7 +------ src/common/audio/ConvolutionBuffer.cxx | 14 +++++-------- src/common/audio/ConvolutionBuffer.hxx | 7 +++---- src/common/audio/LanczosResampler.cxx | 27 ++++++++------------------ src/common/audio/LanczosResampler.hxx | 10 +++++----- 6 files changed, 26 insertions(+), 53 deletions(-) diff --git a/src/common/AudioQueue.cxx b/src/common/AudioQueue.cxx index 3e5f1e0fc..bc6964fe3 100644 --- a/src/common/AudioQueue.cxx +++ b/src/common/AudioQueue.cxx @@ -32,22 +32,16 @@ AudioQueue::AudioQueue(uInt32 fragmentSize, uInt32 capacity, bool isStereo, uInt { const uInt8 sampleSize = myIsStereo ? 2 : 1; - myFragmentBuffer = new Int16[myFragmentSize * sampleSize * (capacity + 2)]; + myFragmentBuffer = make_unique(myFragmentSize * sampleSize * (capacity + 2)); for (uInt32 i = 0; i < capacity; i++) - myFragmentQueue[i] = myAllFragments[i] = myFragmentBuffer + i * sampleSize * myFragmentSize; + myFragmentQueue[i] = myAllFragments[i] = myFragmentBuffer.get() + i * sampleSize * myFragmentSize; myAllFragments[capacity] = myFirstFragmentForEnqueue = - myFragmentBuffer + capacity * sampleSize * myFragmentSize; + myFragmentBuffer.get() + capacity * sampleSize * myFragmentSize; myAllFragments[capacity + 1] = myFirstFragmentForDequeue = - myFragmentBuffer + (capacity + 1) * sampleSize * myFragmentSize; -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -AudioQueue::~AudioQueue() -{ - delete[] myFragmentBuffer; + myFragmentBuffer.get() + (capacity + 1) * sampleSize * myFragmentSize; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/common/AudioQueue.hxx b/src/common/AudioQueue.hxx index 7d64c010b..e0d7c4868 100644 --- a/src/common/AudioQueue.hxx +++ b/src/common/AudioQueue.hxx @@ -47,11 +47,6 @@ class AudioQueue */ AudioQueue(uInt32 fragmentSize, uInt32 capacity, bool isStereo, uInt16 sampleRate); - /** - We need a destructor to deallocate the individual fragment buffers. - */ - ~AudioQueue(); - /** Capacity getter. */ @@ -119,7 +114,7 @@ class AudioQueue vector myAllFragments; // We allocate a consecutive slice of memory for the fragments. - Int16* myFragmentBuffer; + unique_ptr myFragmentBuffer; // The nubmer if queued fragments uInt32 mySize; diff --git a/src/common/audio/ConvolutionBuffer.cxx b/src/common/audio/ConvolutionBuffer.cxx index 0badf2d5e..c0513ad45 100644 --- a/src/common/audio/ConvolutionBuffer.cxx +++ b/src/common/audio/ConvolutionBuffer.cxx @@ -18,16 +18,12 @@ #include "ConvolutionBuffer.hxx" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -ConvolutionBuffer::ConvolutionBuffer(uInt32 size) : myFirstIndex(0), mySize(size) +ConvolutionBuffer::ConvolutionBuffer(uInt32 size) + : myFirstIndex(0), + mySize(size) { - myData = new float[mySize]; - memset(myData, 0, mySize * sizeof(float)); -} - -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -ConvolutionBuffer::~ConvolutionBuffer() -{ - delete[] myData; + myData = make_unique(mySize); + memset(myData.get(), 0, mySize * sizeof(float)); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/common/audio/ConvolutionBuffer.hxx b/src/common/audio/ConvolutionBuffer.hxx index bf85ace41..d0a71ad3b 100644 --- a/src/common/audio/ConvolutionBuffer.hxx +++ b/src/common/audio/ConvolutionBuffer.hxx @@ -20,20 +20,19 @@ #include "bspf.hxx" -class ConvolutionBuffer { +class ConvolutionBuffer +{ public: ConvolutionBuffer(uInt32 size); - ~ConvolutionBuffer(); - void shift(float nextValue); float convoluteWith(float* kernel) const; private: - float* myData; + unique_ptr myData; uInt32 myFirstIndex; diff --git a/src/common/audio/LanczosResampler.cxx b/src/common/audio/LanczosResampler.cxx index 0bce3e1ac..56fa9aac1 100644 --- a/src/common/audio/LanczosResampler.cxx +++ b/src/common/audio/LanczosResampler.cxx @@ -75,35 +75,24 @@ LanczosResampler::LanczosResampler( myKernelSize(2 * kernelParameter), myCurrentKernelIndex(0), myKernelParameter(kernelParameter), - myBuffer(nullptr), - myBufferL(nullptr), - myBufferR(nullptr), myCurrentFragment(nullptr), myFragmentIndex(0), myIsUnderrun(true), myTimeIndex(0) { - myPrecomputedKernels = new float[myPrecomputedKernelCount * myKernelSize]; + myPrecomputedKernels = make_unique(myPrecomputedKernelCount * myKernelSize); - if (myFormatFrom.stereo) { - myBufferL = new ConvolutionBuffer(myKernelSize); - myBufferR = new ConvolutionBuffer(myKernelSize); + if (myFormatFrom.stereo) + { + myBufferL = make_unique(myKernelSize); + myBufferR = make_unique(myKernelSize); } else - myBuffer = new ConvolutionBuffer(myKernelSize); + myBuffer = make_unique(myKernelSize); precomputeKernels(); } -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -LanczosResampler::~LanczosResampler() { - delete[] myPrecomputedKernels; - - if (myBuffer) delete myBuffer; - if (myBufferL) delete myBufferL; - if (myBufferR) delete myBufferR; -} - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void LanczosResampler::precomputeKernels() { @@ -111,7 +100,7 @@ void LanczosResampler::precomputeKernels() uInt32 timeIndex = 0; for (uInt32 i = 0; i < myPrecomputedKernelCount; i++) { - float* kernel = myPrecomputedKernels + myKernelSize * i; + float* kernel = myPrecomputedKernels.get() + myKernelSize * i; // The kernel is normalized such to be evaluate on time * formatFrom.sampleRate float center = static_cast(timeIndex) / static_cast(myFormatTo.sampleRate); @@ -155,7 +144,7 @@ void LanczosResampler::fillFragment(float* fragment, uInt32 length) const uInt32 outputSamples = myFormatTo.stereo ? (length >> 1) : length; for (uInt32 i = 0; i < outputSamples; i++) { - float* kernel = myPrecomputedKernels + (myCurrentKernelIndex * myKernelSize); + float* kernel = myPrecomputedKernels.get() + (myCurrentKernelIndex * myKernelSize); myCurrentKernelIndex = (myCurrentKernelIndex + 1) % myPrecomputedKernelCount; if (myFormatFrom.stereo) { diff --git a/src/common/audio/LanczosResampler.hxx b/src/common/audio/LanczosResampler.hxx index 2ea5a6c04..0f92f48d5 100644 --- a/src/common/audio/LanczosResampler.hxx +++ b/src/common/audio/LanczosResampler.hxx @@ -34,7 +34,7 @@ class LanczosResampler : public Resampler virtual void fillFragment(float* fragment, uInt32 length); - virtual ~LanczosResampler(); + virtual ~LanczosResampler() = default; private: @@ -46,14 +46,14 @@ class LanczosResampler : public Resampler uInt32 myPrecomputedKernelCount; uInt32 myKernelSize; - float* myPrecomputedKernels; uInt32 myCurrentKernelIndex; + unique_ptr myPrecomputedKernels; uInt32 myKernelParameter; - ConvolutionBuffer* myBuffer; - ConvolutionBuffer* myBufferL; - ConvolutionBuffer* myBufferR; + unique_ptr myBuffer; + unique_ptr myBufferL; + unique_ptr myBufferR; Int16* myCurrentFragment; uInt32 myFragmentIndex;