2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2012-05-10 12:06:41 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/fifo.hpp"
|
2012-06-15 19:32:41 +02:00
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Destructor for the FIFO class.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
FIFO::~FIFO()
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-05-26 20:19:32 +02:00
|
|
|
free(m_Buffer);
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Resizes the FIFO's buffer so that it is at least newSize bytes long.
|
|
|
|
*
|
|
|
|
* @param newSize The minimum new size of the FIFO buffer.
|
|
|
|
*/
|
2013-04-08 09:44:12 +02:00
|
|
|
void FIFO::ResizeBuffer(size_t newSize, bool decrease)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2013-04-08 09:44:12 +02:00
|
|
|
if (m_AllocSize >= newSize && !decrease)
|
2012-03-29 07:42:30 +02:00
|
|
|
return;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-03-29 07:42:30 +02:00
|
|
|
newSize = (newSize / FIFO::BlockSize + 1) * FIFO::BlockSize;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2013-04-08 09:44:12 +02:00
|
|
|
if (newSize == m_AllocSize)
|
|
|
|
return;
|
|
|
|
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *newBuffer = static_cast<char *>(realloc(m_Buffer, newSize));
|
2012-05-26 20:19:32 +02:00
|
|
|
|
2017-12-14 15:37:20 +01:00
|
|
|
if (!newBuffer)
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::bad_alloc());
|
2012-05-26 20:19:32 +02:00
|
|
|
|
2012-05-27 00:27:55 +02:00
|
|
|
m_Buffer = newBuffer;
|
|
|
|
|
2012-03-29 07:42:30 +02:00
|
|
|
m_AllocSize = newSize;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Optimizes memory usage of the FIFO buffer by reallocating
|
|
|
|
* and moving the buffer.
|
|
|
|
*/
|
2018-01-04 04:25:35 +01:00
|
|
|
void FIFO::Optimize()
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2017-08-17 18:03:37 +02:00
|
|
|
if (m_Offset > m_DataSize / 10 && m_Offset - m_DataSize > 1024) {
|
2016-02-01 12:58:42 +01:00
|
|
|
std::memmove(m_Buffer, m_Buffer + m_Offset, m_DataSize);
|
2012-03-28 13:24:49 +02:00
|
|
|
m_Offset = 0;
|
|
|
|
|
2013-04-08 09:44:12 +02:00
|
|
|
if (m_DataSize > 0)
|
|
|
|
ResizeBuffer(m_DataSize, true);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
return;
|
2013-04-05 14:05:00 +02:00
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 11:11:21 +02:00
|
|
|
size_t FIFO::Peek(void *buffer, size_t count, bool allow_partial)
|
2015-02-13 21:02:48 +01:00
|
|
|
{
|
2015-06-22 11:11:21 +02:00
|
|
|
ASSERT(allow_partial);
|
|
|
|
|
2015-02-13 21:02:48 +01:00
|
|
|
if (count > m_DataSize)
|
|
|
|
count = m_DataSize;
|
|
|
|
|
2017-12-14 15:37:20 +01:00
|
|
|
if (buffer)
|
2015-02-13 21:02:48 +01:00
|
|
|
std::memcpy(buffer, m_Buffer + m_Offset, count);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
2013-04-04 16:08:02 +02:00
|
|
|
* Implements IOQueue::Read.
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2015-02-14 16:34:36 +01:00
|
|
|
size_t FIFO::Read(void *buffer, size_t count, bool allow_partial)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2015-02-14 16:34:36 +01:00
|
|
|
ASSERT(allow_partial);
|
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
if (count > m_DataSize)
|
|
|
|
count = m_DataSize;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2017-12-14 15:37:20 +01:00
|
|
|
if (buffer)
|
2014-09-02 13:02:22 +02:00
|
|
|
std::memcpy(buffer, m_Buffer + m_Offset, count);
|
2012-11-22 12:04:32 +01:00
|
|
|
|
2012-03-29 07:42:30 +02:00
|
|
|
m_DataSize -= count;
|
2012-03-28 13:24:49 +02:00
|
|
|
m_Offset += count;
|
|
|
|
|
|
|
|
Optimize();
|
2012-11-22 12:04:32 +01:00
|
|
|
|
|
|
|
return count;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
2012-07-16 00:02:31 +02:00
|
|
|
* Implements IOQueue::Write.
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2012-07-16 00:02:31 +02:00
|
|
|
void FIFO::Write(const void *buffer, size_t count)
|
2012-03-28 19:50:55 +02:00
|
|
|
{
|
2013-04-08 09:44:12 +02:00
|
|
|
ResizeBuffer(m_Offset + m_DataSize + count, false);
|
2014-09-02 13:02:22 +02:00
|
|
|
std::memcpy(m_Buffer + m_Offset + m_DataSize, buffer, count);
|
2012-03-29 07:42:30 +02:00
|
|
|
m_DataSize += count;
|
2015-02-14 16:34:36 +01:00
|
|
|
|
|
|
|
SignalDataAvailable();
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
2013-04-04 16:08:02 +02:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void FIFO::Close()
|
2013-04-04 16:08:02 +02:00
|
|
|
{ }
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool FIFO::IsEof() const
|
2013-08-27 12:21:41 +02:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
size_t FIFO::GetAvailableBytes() const
|
2013-04-04 16:08:02 +02:00
|
|
|
{
|
|
|
|
return m_DataSize;
|
|
|
|
}
|
2015-02-14 16:34:36 +01:00
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool FIFO::SupportsWaiting() const
|
2015-02-14 16:34:36 +01:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
bool FIFO::IsDataAvailable() const
|
2015-02-14 16:34:36 +01:00
|
|
|
{
|
|
|
|
return m_DataSize > 0;
|
2015-06-22 11:11:21 +02:00
|
|
|
}
|