diff --git a/utils/messageqcpp/bytestreampool.cpp b/utils/messageqcpp/bytestreampool.cpp new file mode 100644 index 000000000..88b828b96 --- /dev/null +++ b/utils/messageqcpp/bytestreampool.cpp @@ -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); + } +} + +} diff --git a/utils/messageqcpp/bytestreampool.h b/utils/messageqcpp/bytestreampool.h new file mode 100644 index 000000000..075a29fe9 --- /dev/null +++ b/utils/messageqcpp/bytestreampool.h @@ -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 +#include + +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 freeByteStreams; + boost::mutex mutex; + uint maxBufferSize; + uint maxFreeBuffers; +}; + +} + +#endif