mirror of
https://github.com/cjcliffe/CubicSDR.git
synced 2024-11-26 21:58:37 -05:00
ReBuffer somewhat cleaner, safer code
This commit is contained in:
parent
44cdfa8521
commit
a0526f757e
@ -27,6 +27,12 @@ struct map_string_less : public std::binary_function<std::string,std::string,boo
|
|||||||
template <typename PtrType>
|
template <typename PtrType>
|
||||||
class ReBufferAge {
|
class ReBufferAge {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
ReBufferAge(PtrType p, int a) {
|
||||||
|
ptr = p;
|
||||||
|
age = a;
|
||||||
|
}
|
||||||
|
|
||||||
PtrType ptr;
|
PtrType ptr;
|
||||||
int age;
|
int age;
|
||||||
|
|
||||||
@ -50,10 +56,10 @@ public:
|
|||||||
|
|
||||||
std::lock_guard < std::mutex > lock(m_mutex);
|
std::lock_guard < std::mutex > lock(m_mutex);
|
||||||
|
|
||||||
// iterate the ReBuffer_ptr list: if the std::shared_ptr count == 1, it means
|
// iterate the ReBufferAge list: if the std::shared_ptr count == 1, it means
|
||||||
//it is only referenced in outputBuffers itself, so available for re-use.
|
//it is only referenced in outputBuffers itself, so available for re-use.
|
||||||
//else if the std::shared_ptr count <= 1, make it age.
|
//else if the std::shared_ptr count <= 1, make it age.
|
||||||
//else the ReBuffer_ptr is in use, don't use it.
|
//else the ReBufferPtr is in use, don't use it.
|
||||||
|
|
||||||
ReBufferPtr buf = nullptr;
|
ReBufferPtr buf = nullptr;
|
||||||
|
|
||||||
@ -66,18 +72,18 @@ public:
|
|||||||
//is used.
|
//is used.
|
||||||
long use = it->ptr.use_count();
|
long use = it->ptr.use_count();
|
||||||
|
|
||||||
//1. If we encounter a shared_ptr with a use count of 0, this
|
//1. If we encounter a ReBufferPtr with a use count of 0, this
|
||||||
//is a bug since it is supposed to be at least 1, because it is referenced here.
|
//is a bug since it is supposed to be at least 1, because it is referenced here.
|
||||||
//in this case, purge it from here and trace.
|
//in this case, purge it from here and trace.
|
||||||
if (use == 0) {
|
if (use == 0) {
|
||||||
it = outputBuffers.erase(it);
|
|
||||||
std::cout << "Warning: in ReBuffer '" << bufferId << "' count '" << outputBuffers.size() << "', found 1 dangling buffer !" << std::endl << std::flush;
|
std::cout << "Warning: in ReBuffer '" << bufferId << "' count '" << outputBuffers.size() << "', found 1 dangling buffer !" << std::endl << std::flush;
|
||||||
|
it = outputBuffers.erase(it);
|
||||||
}
|
}
|
||||||
else if (use == 1) {
|
else if (use == 1) {
|
||||||
if (buf == nullptr) {
|
if (buf == nullptr) {
|
||||||
it->age = 1; //select this one.
|
it->age = 1; //select this one.
|
||||||
buf = it->ptr;
|
buf = it->ptr;
|
||||||
//std::cout << "**" << std::flush;
|
// std::cout << "**" << std::flush;
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -107,17 +113,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
//3.We need to allocate a new buffer.
|
//3.We need to allocate a new buffer.
|
||||||
ReBufferAge < ReBufferPtr > newBuffer;
|
ReBufferAge < ReBufferPtr > newBuffer(std::make_shared<BufferType>(), 1);
|
||||||
|
|
||||||
//careful here: newBuffer.ptr is already constructed, so we need to set "in place" its
|
|
||||||
//ownership to a (new BufferType()).
|
|
||||||
newBuffer.ptr.reset(new BufferType());
|
|
||||||
newBuffer.age = 1;
|
|
||||||
|
|
||||||
outputBuffers.push_back(newBuffer);
|
outputBuffers.push_back(newBuffer);
|
||||||
|
|
||||||
//std::cout << "++" << std::flush;
|
// std::cout << "++" << std::flush;
|
||||||
|
|
||||||
return newBuffer.ptr;
|
return newBuffer.ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ public:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~AudioThreadInput() {
|
virtual ~AudioThreadInput() {
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user