- use size_t in Audio::DataWindow and be more strict in memory management
This commit is contained in:
@@ -9,11 +9,7 @@
|
||||
using namespace Audio;
|
||||
|
||||
DataWindow::DataWindow()
|
||||
{
|
||||
mFilled = 0;
|
||||
mData = nullptr;
|
||||
mCapacity = 0;
|
||||
}
|
||||
{}
|
||||
|
||||
DataWindow::~DataWindow()
|
||||
{
|
||||
@@ -24,24 +20,30 @@ DataWindow::~DataWindow()
|
||||
}
|
||||
}
|
||||
|
||||
void DataWindow::setCapacity(int capacity)
|
||||
void DataWindow::setCapacity(size_t capacity)
|
||||
{
|
||||
Lock l(mMutex);
|
||||
int tail = capacity - mCapacity;
|
||||
char* buffer = mData;
|
||||
mData = (char*)realloc(mData, capacity);
|
||||
if (!mData)
|
||||
|
||||
if (capacity >= mCapacity)
|
||||
{
|
||||
// Realloc failed
|
||||
mData = buffer;
|
||||
throw std::bad_alloc();
|
||||
size_t tail = capacity - mCapacity;
|
||||
char* buffer = mData;
|
||||
mData = (char*)realloc(mData, capacity);
|
||||
if (!mData)
|
||||
{
|
||||
// Realloc failed
|
||||
mData = buffer;
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
if (tail > 0)
|
||||
memset(mData + mCapacity, 0, tail);
|
||||
mCapacity = capacity;
|
||||
}
|
||||
if (tail > 0)
|
||||
memset(mData + mCapacity, 0, tail);
|
||||
mCapacity = capacity;
|
||||
else
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
|
||||
void DataWindow::addZero(int length)
|
||||
void DataWindow::addZero(size_t length)
|
||||
{
|
||||
Lock l(mMutex);
|
||||
|
||||
@@ -60,7 +62,7 @@ void DataWindow::addZero(int length)
|
||||
}
|
||||
|
||||
|
||||
void DataWindow::add(const void* data, int length)
|
||||
void DataWindow::add(const void* data, size_t length)
|
||||
{
|
||||
Lock l(mMutex);
|
||||
|
||||
@@ -94,7 +96,7 @@ void DataWindow::add(short sample)
|
||||
add(&sample, sizeof sample);
|
||||
}
|
||||
|
||||
void DataWindow::erase(int length)
|
||||
void DataWindow::erase(size_t length)
|
||||
{
|
||||
Lock l(mMutex);
|
||||
if (length > mFilled)
|
||||
@@ -120,21 +122,21 @@ void DataWindow::clear()
|
||||
mFilled = 0;
|
||||
}
|
||||
|
||||
short DataWindow::shortAt(int index) const
|
||||
short DataWindow::shortAt(size_t index) const
|
||||
{
|
||||
Lock l(mMutex);
|
||||
assert(index < mFilled / 2);
|
||||
return ((short*)mData)[index];
|
||||
}
|
||||
|
||||
void DataWindow::setShortAt(short value, int index)
|
||||
void DataWindow::setShortAt(short value, size_t index)
|
||||
{
|
||||
Lock l(mMutex);
|
||||
assert(index < mFilled / 2);
|
||||
((short*)mData)[index] = value;
|
||||
}
|
||||
|
||||
int DataWindow::read(void* buffer, int length)
|
||||
size_t DataWindow::read(void* buffer, size_t length)
|
||||
{
|
||||
Lock l(mMutex);
|
||||
if (length > mFilled)
|
||||
@@ -150,25 +152,27 @@ int DataWindow::read(void* buffer, int length)
|
||||
return length;
|
||||
}
|
||||
|
||||
int DataWindow::filled() const
|
||||
size_t DataWindow::filled() const
|
||||
{
|
||||
Lock l(mMutex);
|
||||
return mFilled;
|
||||
}
|
||||
|
||||
void DataWindow::setFilled(int filled)
|
||||
void DataWindow::setFilled(size_t filled)
|
||||
{
|
||||
Lock l(mMutex);
|
||||
if (filled > mCapacity)
|
||||
throw std::bad_alloc();
|
||||
mFilled = filled;
|
||||
}
|
||||
|
||||
int DataWindow::capacity() const
|
||||
size_t DataWindow::capacity() const
|
||||
{
|
||||
Lock l(mMutex);
|
||||
return mCapacity;
|
||||
}
|
||||
|
||||
void DataWindow::zero(int length)
|
||||
void DataWindow::zero(size_t length)
|
||||
{
|
||||
Lock l(mMutex);
|
||||
assert(length <= mCapacity);
|
||||
@@ -189,10 +193,10 @@ size_t DataWindow::moveTo(DataWindow& dst, size_t size)
|
||||
return avail;
|
||||
}
|
||||
|
||||
std::chrono::milliseconds DataWindow::getTimeLength(int samplerate, int channels) const
|
||||
std::chrono::milliseconds DataWindow::getTimeLength(const Audio::Format& fmt) const
|
||||
{
|
||||
Lock l(mMutex);
|
||||
return std::chrono::milliseconds(mFilled / sizeof(short) / channels / (samplerate / 1000));
|
||||
return std::chrono::milliseconds(mFilled / sizeof(short) / fmt.channels() / (fmt.rate()/ 1000));
|
||||
}
|
||||
|
||||
void DataWindow::makeStereoFromMono(DataWindow& dst, DataWindow& src)
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "../helper/HL_ByteBuffer.h"
|
||||
#include "../helper/HL_Sync.h"
|
||||
#include "Audio_Interface.h"
|
||||
|
||||
namespace Audio
|
||||
{
|
||||
@@ -17,34 +18,34 @@ public:
|
||||
DataWindow();
|
||||
~DataWindow();
|
||||
|
||||
void setCapacity(int capacity);
|
||||
int capacity() const;
|
||||
void setCapacity(size_t capacity);
|
||||
size_t capacity() const;
|
||||
|
||||
void addZero(int length);
|
||||
void add(const void* data, int length);
|
||||
void addZero(size_t length);
|
||||
void add(const void* data, size_t length);
|
||||
void add(short sample);
|
||||
int read(void* buffer, int length);
|
||||
void erase(int length = -1);
|
||||
size_t read(void* buffer, size_t length);
|
||||
void erase(size_t length);
|
||||
const char* data() const;
|
||||
char* mutableData();
|
||||
int filled() const;
|
||||
void setFilled(int filled);
|
||||
size_t filled() const;
|
||||
void setFilled(size_t filled);
|
||||
void clear();
|
||||
|
||||
short shortAt(int index) const;
|
||||
void setShortAt(short value, int index);
|
||||
void zero(int length);
|
||||
size_t moveTo(DataWindow& dst, size_t size);
|
||||
short shortAt(size_t index) const;
|
||||
void setShortAt(short value, size_t index);
|
||||
void zero(size_t length);
|
||||
size_t moveTo(DataWindow& dst, size_t size /* in bytes*/ );
|
||||
|
||||
std::chrono::milliseconds getTimeLength(int samplerate, int channels) const;
|
||||
std::chrono::milliseconds getTimeLength(const Format& fmt) const;
|
||||
|
||||
static void makeStereoFromMono(DataWindow& dst, DataWindow& src);
|
||||
|
||||
protected:
|
||||
mutable Mutex mMutex;
|
||||
char* mData;
|
||||
int mFilled;
|
||||
int mCapacity;
|
||||
char* mData = nullptr;
|
||||
size_t mFilled = 0;
|
||||
size_t mCapacity = 0;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user