prevent copying of buffers

This commit is contained in:
Emiel Bruijntjes 2017-03-09 09:25:26 +01:00
parent 1f5f641d8b
commit 71eba4c5d3
4 changed files with 8 additions and 15 deletions

View File

@ -348,7 +348,7 @@ public:
*
* @param buffer the buffer with data to send
*/
bool send(const CopiedBuffer &buffer);
bool send(CopiedBuffer &&buffer);
/**
* Get a channel by its identifier

View File

@ -82,17 +82,10 @@ public:
}
/**
* Copy constructor
* Disabled copy constructor to prevent expensive copy operations
* @param that
*/
CopiedBuffer(const CopiedBuffer &that) :
_capacity(that._capacity),
_buffer((char *)malloc(_capacity)),
_size(that._size)
{
// copy the data
memcpy(_buffer, that._buffer, _size);
}
CopiedBuffer(const CopiedBuffer &that) = delete;
/**
* Move constructor

View File

@ -727,15 +727,15 @@ void ChannelImpl::onSynchronized()
while (_connection && !_synchronous && !_queue.empty())
{
// retrieve the first buffer and synchronous
const auto &pair = _queue.front();
auto &pair = _queue.front();
// mark as synchronous if necessary
_synchronous = pair.first;
// send it over the connection
_connection->send(pair.second);
_connection->send(std::move(pair.second));
// the user space handler may have destructed the channel
// the user space handler may have destructed this channel object
if (!monitor.valid()) return;
// remove from the list

View File

@ -353,7 +353,7 @@ bool ConnectionImpl::send(const Frame &frame)
*
* @param buffer the buffer with data to send
*/
bool ConnectionImpl::send(const CopiedBuffer &buffer)
bool ConnectionImpl::send(CopiedBuffer &&buffer)
{
// this only works when we are already connected
if (_state != state_connected) return false;
@ -367,7 +367,7 @@ bool ConnectionImpl::send(const CopiedBuffer &buffer)
else
{
// add to the list of waiting buffers
_queue.push(buffer);
_queue.emplace(std::move(buffer));
}
// done