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;
myFragmentBuffer = new Int16[myFragmentSize * sampleSize * (capacity + 2)];
myFragmentBuffer = make_unique<Int16[]>(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;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -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<Int16*> myAllFragments;
// We allocate a consecutive slice of memory for the fragments.
Int16* myFragmentBuffer;
unique_ptr<Int16[]> myFragmentBuffer;
// The nubmer if queued fragments
uInt32 mySize;

View File

@ -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<float[]>(mySize);
memset(myData.get(), 0, mySize * sizeof(float));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -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<float[]> myData;
uInt32 myFirstIndex;

View File

@ -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<float[]>(myPrecomputedKernelCount * myKernelSize);
if (myFormatFrom.stereo) {
myBufferL = new ConvolutionBuffer(myKernelSize);
myBufferR = new ConvolutionBuffer(myKernelSize);
if (myFormatFrom.stereo)
{
myBufferL = make_unique<ConvolutionBuffer>(myKernelSize);
myBufferR = make_unique<ConvolutionBuffer>(myKernelSize);
}
else
myBuffer = new ConvolutionBuffer(myKernelSize);
myBuffer = make_unique<ConvolutionBuffer>(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<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;
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) {

View File

@ -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<float[]> myPrecomputedKernels;
uInt32 myKernelParameter;
ConvolutionBuffer* myBuffer;
ConvolutionBuffer* myBufferL;
ConvolutionBuffer* myBufferR;
unique_ptr<ConvolutionBuffer> myBuffer;
unique_ptr<ConvolutionBuffer> myBufferL;
unique_ptr<ConvolutionBuffer> myBufferR;
Int16* myCurrentFragment;
uInt32 myFragmentIndex;