1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-07-30 19:23:07 +03:00

Added a ByteStreamPool class for cases where we'd otherwise

have to do something complicated, or constantly create and dispose
of ByteStreams.
This commit is contained in:
Patrick LeBlanc
2019-01-22 10:09:08 -06:00
parent d84dcb9ccc
commit d92be01ee2
2 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,78 @@
/* Copyright (C) 2019 MariaDB Corporaton
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of
the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "bytestreampool.h"
namespace messageqcpp
{
ByteStreamPool::ByteStreamPool()
{
maxBufferSize = 1 << 20; // 1MB
maxFreeBuffers = 10;
}
ByteStreamPool::ByteStreamPool(uint largeBufferSize)
{
maxBufferSize = largeBufferSize;
maxFreeBuffers = 10;
}
ByteStreamPool::ByteStreamPool(uint largeBufferSize, uint freeBufferLimit)
{
maxBufferSize = largeBufferSize;
maxFreeBuffers = freeBufferLimit;
}
ByteStreamPool::~ByteStreamPool()
{
while (!freeByteStreams.empty())
{
ByteStream *next = freeByteStreams.front();
freeByteStreams.pop_front();
delete next;
}
}
ByteStream * ByteStreamPool::getByteStream()
{
boost::mutex::scoped_lock s(mutex);
ByteStream *ret;
if (!freeByteStreams.empty())
{
ret = freeByteStreams.front();
freeByteStreams.pop_front();
}
else
ret = new ByteStream();
return ret;
}
void ByteStreamPool::returnByteStream(ByteStream *bs)
{
if (bs->getBufferSize() > maxBufferSize)
delete bs;
else
{
boost::mutex::scoped_lock s(mutex);
freeByteStreams.push_back(bs);
}
}
}

View File

@ -0,0 +1,53 @@
/* Copyright (C) 2019 MariaDB Corporaton
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2 of
the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA. */
#ifndef BYTESTREAMPOOL_H_
#define BYTESTREAMPOOL_H_
/* This class defines a pool of bytestreams to improve BS reuse, reducing
the need to use the dynamic allocator. It allocates as many BS's as needed,
and automatically disposes of buffers that are 'large' to reduce mem usage.
Initially, 'large' is defined as 1MB.
*/
#include <deque>
#include <boost/thread/mutex.hpp>
namespace messageqcpp
{
class ByteStreamPool
{
public:
ByteStreamPool();
ByteStreamPool(uint largeBufferSize); // BS's above largeBufferSize get deallocated on return to the pool
ByteStreamPool(uint largeBufferSize, uint freeBufferLimit); // freeBufferLimit is the max # of BSs to reserve
virtual ~ByteStreamPool();
ByteStream * getByteStream();
void returnByteStream(ByteStream *);
private:
std::deque<ByteStream *> freeByteStreams;
boost::mutex mutex;
uint maxBufferSize;
uint maxFreeBuffers;
};
}
#endif