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.
This commit is contained in:
Stephen Anthony 2018-05-11 21:26:22 -02:30
parent 741515a520
commit 4c7ad7a0b6
6 changed files with 26 additions and 53 deletions

View File

@ -32,22 +32,16 @@ AudioQueue::AudioQueue(uInt32 fragmentSize, uInt32 capacity, bool isStereo, uInt
{ {
const uInt8 sampleSize = myIsStereo ? 2 : 1; const uInt8 sampleSize = myIsStereo ? 2 : 1;
myFragmentBuffer = new Int16[myFragmentSize * sampleSize * (capacity + 2)]; myFragmentBuffer = make_unique<Int16[]>(myFragmentSize * sampleSize * (capacity + 2));
for (uInt32 i = 0; i < capacity; i++) 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 = myAllFragments[capacity] = myFirstFragmentForEnqueue =
myFragmentBuffer + capacity * sampleSize * myFragmentSize; myFragmentBuffer.get() + capacity * sampleSize * myFragmentSize;
myAllFragments[capacity + 1] = myFirstFragmentForDequeue = myAllFragments[capacity + 1] = myFirstFragmentForDequeue =
myFragmentBuffer + (capacity + 1) * sampleSize * myFragmentSize; myFragmentBuffer.get() + (capacity + 1) * sampleSize * myFragmentSize;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
AudioQueue::~AudioQueue()
{
delete[] myFragmentBuffer;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -47,11 +47,6 @@ class AudioQueue
*/ */
AudioQueue(uInt32 fragmentSize, uInt32 capacity, bool isStereo, uInt16 sampleRate); AudioQueue(uInt32 fragmentSize, uInt32 capacity, bool isStereo, uInt16 sampleRate);
/**
We need a destructor to deallocate the individual fragment buffers.
*/
~AudioQueue();
/** /**
Capacity getter. Capacity getter.
*/ */
@ -119,7 +114,7 @@ class AudioQueue
vector<Int16*> myAllFragments; vector<Int16*> myAllFragments;
// We allocate a consecutive slice of memory for the fragments. // We allocate a consecutive slice of memory for the fragments.
Int16* myFragmentBuffer; unique_ptr<Int16[]> myFragmentBuffer;
// The nubmer if queued fragments // The nubmer if queued fragments
uInt32 mySize; uInt32 mySize;

View File

@ -18,16 +18,12 @@
#include "ConvolutionBuffer.hxx" #include "ConvolutionBuffer.hxx"
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ConvolutionBuffer::ConvolutionBuffer(uInt32 size) : myFirstIndex(0), mySize(size) ConvolutionBuffer::ConvolutionBuffer(uInt32 size)
: myFirstIndex(0),
mySize(size)
{ {
myData = new float[mySize]; myData = make_unique<float[]>(mySize);
memset(myData, 0, mySize * sizeof(float)); memset(myData.get(), 0, mySize * sizeof(float));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ConvolutionBuffer::~ConvolutionBuffer()
{
delete[] myData;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -20,20 +20,19 @@
#include "bspf.hxx" #include "bspf.hxx"
class ConvolutionBuffer { class ConvolutionBuffer
{
public: public:
ConvolutionBuffer(uInt32 size); ConvolutionBuffer(uInt32 size);
~ConvolutionBuffer();
void shift(float nextValue); void shift(float nextValue);
float convoluteWith(float* kernel) const; float convoluteWith(float* kernel) const;
private: private:
float* myData; unique_ptr<float[]> myData;
uInt32 myFirstIndex; uInt32 myFirstIndex;

View File

@ -75,35 +75,24 @@ LanczosResampler::LanczosResampler(
myKernelSize(2 * kernelParameter), myKernelSize(2 * kernelParameter),
myCurrentKernelIndex(0), myCurrentKernelIndex(0),
myKernelParameter(kernelParameter), myKernelParameter(kernelParameter),
myBuffer(nullptr),
myBufferL(nullptr),
myBufferR(nullptr),
myCurrentFragment(nullptr), myCurrentFragment(nullptr),
myFragmentIndex(0), myFragmentIndex(0),
myIsUnderrun(true), myIsUnderrun(true),
myTimeIndex(0) myTimeIndex(0)
{ {
myPrecomputedKernels = new float[myPrecomputedKernelCount * myKernelSize]; myPrecomputedKernels = make_unique<float[]>(myPrecomputedKernelCount * myKernelSize);
if (myFormatFrom.stereo) { if (myFormatFrom.stereo)
myBufferL = new ConvolutionBuffer(myKernelSize); {
myBufferR = new ConvolutionBuffer(myKernelSize); myBufferL = make_unique<ConvolutionBuffer>(myKernelSize);
myBufferR = make_unique<ConvolutionBuffer>(myKernelSize);
} }
else else
myBuffer = new ConvolutionBuffer(myKernelSize); myBuffer = make_unique<ConvolutionBuffer>(myKernelSize);
precomputeKernels(); precomputeKernels();
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
LanczosResampler::~LanczosResampler() {
delete[] myPrecomputedKernels;
if (myBuffer) delete myBuffer;
if (myBufferL) delete myBufferL;
if (myBufferR) delete myBufferR;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void LanczosResampler::precomputeKernels() void LanczosResampler::precomputeKernels()
{ {
@ -111,7 +100,7 @@ void LanczosResampler::precomputeKernels()
uInt32 timeIndex = 0; uInt32 timeIndex = 0;
for (uInt32 i = 0; i < myPrecomputedKernelCount; i++) { 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 // The kernel is normalized such to be evaluate on time * formatFrom.sampleRate
float center = float center =
static_cast<float>(timeIndex) / static_cast<float>(myFormatTo.sampleRate); static_cast<float>(timeIndex) / static_cast<float>(myFormatTo.sampleRate);
@ -155,7 +144,7 @@ void LanczosResampler::fillFragment(float* fragment, uInt32 length)
const uInt32 outputSamples = myFormatTo.stereo ? (length >> 1) : length; const uInt32 outputSamples = myFormatTo.stereo ? (length >> 1) : length;
for (uInt32 i = 0; i < outputSamples; i++) { for (uInt32 i = 0; i < outputSamples; i++) {
float* kernel = myPrecomputedKernels + (myCurrentKernelIndex * myKernelSize); float* kernel = myPrecomputedKernels.get() + (myCurrentKernelIndex * myKernelSize);
myCurrentKernelIndex = (myCurrentKernelIndex + 1) % myPrecomputedKernelCount; myCurrentKernelIndex = (myCurrentKernelIndex + 1) % myPrecomputedKernelCount;
if (myFormatFrom.stereo) { if (myFormatFrom.stereo) {

View File

@ -34,7 +34,7 @@ class LanczosResampler : public Resampler
virtual void fillFragment(float* fragment, uInt32 length); virtual void fillFragment(float* fragment, uInt32 length);
virtual ~LanczosResampler(); virtual ~LanczosResampler() = default;
private: private:
@ -46,14 +46,14 @@ class LanczosResampler : public Resampler
uInt32 myPrecomputedKernelCount; uInt32 myPrecomputedKernelCount;
uInt32 myKernelSize; uInt32 myKernelSize;
float* myPrecomputedKernels;
uInt32 myCurrentKernelIndex; uInt32 myCurrentKernelIndex;
unique_ptr<float[]> myPrecomputedKernels;
uInt32 myKernelParameter; uInt32 myKernelParameter;
ConvolutionBuffer* myBuffer; unique_ptr<ConvolutionBuffer> myBuffer;
ConvolutionBuffer* myBufferL; unique_ptr<ConvolutionBuffer> myBufferL;
ConvolutionBuffer* myBufferR; unique_ptr<ConvolutionBuffer> myBufferR;
Int16* myCurrentFragment; Int16* myCurrentFragment;
uInt32 myFragmentIndex; uInt32 myFragmentIndex;