Commit 3ce322c1 authored by Alejandro Homs Puron's avatar Alejandro Homs Puron

Merge branch 'perf-buffer-mgr' into 'master'

Avoid deep copying MemBuffer and remove one level of indirection in SoftBufferAllocMgr

See merge request !63
parents 01e67a5e 06dfb07d
Pipeline #8124 passed with stages
in 12 minutes and 1 second
Subproject commit 752e393336baec691a10686e4f1e2e2d2659fd0c
Subproject commit 110a27995464539c10e6f4fda89b0de77ed874a2
......@@ -47,11 +47,17 @@ class LIMACORE_API MemBuffer
MemBuffer();
MemBuffer(int size);
MemBuffer(const MemBuffer& buffer);
~MemBuffer();
MemBuffer(const MemBuffer&);
MemBuffer& operator=(const MemBuffer&);
// MemBuffer are move-constructible or move-assignable.
MemBuffer(MemBuffer&&) = default;
MemBuffer& operator=(MemBuffer&&) = default;
void alloc(int size);
void copy(const MemBuffer& buffer);
void deepCopy(const MemBuffer& buffer);
void release();
int getSize() const;
......@@ -63,8 +69,6 @@ class LIMACORE_API MemBuffer
operator void*();
operator const void*() const;
MemBuffer& operator =(const MemBuffer& buffer);
private:
int m_size;
void *m_ptr;
......
......@@ -146,7 +146,7 @@ MemBuffer::MemBuffer(int size)
MemBuffer::MemBuffer(const MemBuffer& buffer)
: m_size(0), m_ptr(NULL)
{
copy(buffer);
deepCopy(buffer);
}
MemBuffer::~MemBuffer()
......@@ -174,13 +174,19 @@ void MemBuffer::alloc(int size)
m_size = size;
}
void MemBuffer::copy(const MemBuffer& buffer)
void MemBuffer::deepCopy(const MemBuffer& buffer)
{
int size = buffer.getSize();
alloc(size);
memcpy(getPtr(), buffer.getConstPtr(), size);
}
MemBuffer& MemBuffer::operator =(const MemBuffer& buffer)
{
deepCopy(buffer);
return *this;
}
void MemBuffer::release()
{
if (!m_size)
......@@ -195,12 +201,6 @@ void MemBuffer::release()
m_size = 0;
}
MemBuffer& MemBuffer::operator =(const MemBuffer& buffer)
{
copy(buffer);
return *this;
}
void MemBuffer::clear()
{
ClearBuffer(getPtr(), 1, FrameDim(getSize(), 1, Bpp8));
......
......@@ -48,6 +48,10 @@ class LIMACORE_API BufferAllocMgr
BufferAllocMgr();
virtual ~BufferAllocMgr();
// BufferAllocMgr are **not** copy-constructible nor copy-assignable.
BufferAllocMgr(const BufferAllocMgr&) = delete;
BufferAllocMgr& operator=(const BufferAllocMgr&) = delete;
virtual int getMaxNbBuffers(const FrameDim& frame_dim) = 0;
virtual void allocBuffers(int nb_buffers,
const FrameDim& frame_dim) = 0;
......@@ -87,7 +91,7 @@ class LIMACORE_API SoftBufferAllocMgr : public BufferAllocMgr
virtual void *getBufferPtr(int buffer_nb);
private:
typedef std::vector<MemBuffer *> BufferList;
typedef std::vector<MemBuffer> BufferList;
typedef BufferList::const_reverse_iterator BufferListCRIt;
FrameDim m_frame_dim;
......
......@@ -40,6 +40,9 @@ using namespace lima;
virtual void clearBuffer(int buffer_nb);
virtual void clearAllBuffers();
private:
BufferAllocMgr(const BufferAllocMgr&);
};
class SoftBufferAllocMgr : BufferAllocMgr
......
......@@ -105,18 +105,12 @@ void SoftBufferAllocMgr::allocBuffers(int nb_buffers,
try {
BufferList& bl = m_buffer_list;
if (to_alloc > 0) {
bl.reserve(nb_buffers);
bl.resize(nb_buffers);
DEB_TRACE() << "Allocating " << to_alloc << " buffers";
while (int(bl.size()) != nb_buffers) {
MemBuffer *buffer = new MemBuffer(frame_size);
bl.push_back(buffer);
}
for (int i = 0; i < nb_buffers; i++)
bl[i].alloc(frame_size);
} else {
DEB_TRACE() << "Releasing " << -to_alloc << " buffers";
while (int(bl.size()) != nb_buffers) {
delete bl.back();
bl.pop_back();
}
}
} catch (...) {
DEB_ERROR() << "Error alloc. buffer #" << m_buffer_list.size();
......@@ -132,8 +126,6 @@ void SoftBufferAllocMgr::releaseBuffers()
DEB_MEMBER_FUNCT();
BufferList& bl = m_buffer_list;
for (BufferListCRIt it = bl.rbegin(); it != bl.rend(); ++it)
delete *it;
bl.clear();
m_frame_dim = FrameDim();
}
......@@ -155,7 +147,7 @@ void SoftBufferAllocMgr::getNbBuffers(int& nb_buffers)
void *SoftBufferAllocMgr::getBufferPtr(int buffer_nb)
{
DEB_MEMBER_FUNCT();
void *ptr = m_buffer_list[buffer_nb]->getPtr();
void *ptr = m_buffer_list[buffer_nb].getPtr();
DEB_RETURN() << DEB_VAR1(ptr);
return ptr;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment