icinga2/lib/base/fifo.cpp

125 lines
2.0 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
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;
/**
* Destructor for the FIFO class.
*/
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
}
/**
* 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)
return;
2012-03-28 13:24:49 +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;
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;
m_AllocSize = newSize;
2012-03-28 13:24:49 +02:00
}
/**
* Optimizes memory usage of the FIFO buffer by reallocating
* and moving the buffer.
*/
void FIFO::Optimize()
2012-03-28 13:24:49 +02:00
{
if (m_Offset > m_DataSize / 10 && m_Offset - m_DataSize > 1024) {
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-06-22 11:11:21 +02:00
ASSERT(allow_partial);
if (count > m_DataSize)
count = m_DataSize;
2017-12-14 15:37:20 +01:00
if (buffer)
std::memcpy(buffer, m_Buffer + m_Offset, count);
return count;
}
/**
2013-04-04 16:08:02 +02:00
* Implements IOQueue::Read.
*/
size_t FIFO::Read(void *buffer, size_t count, bool allow_partial)
2012-03-28 13:24:49 +02: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)
std::memcpy(buffer, m_Buffer + m_Offset, count);
2012-11-22 12:04:32 +01: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-07-16 00:02:31 +02:00
* Implements IOQueue::Write.
*/
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);
std::memcpy(m_Buffer + m_Offset + m_DataSize, buffer, count);
m_DataSize += count;
SignalDataAvailable();
2012-03-28 13:24:49 +02:00
}
2013-04-04 16:08:02 +02:00
void FIFO::Close()
2013-04-04 16:08:02 +02:00
{ }
bool FIFO::IsEof() const
{
return false;
}
size_t FIFO::GetAvailableBytes() const
2013-04-04 16:08:02 +02:00
{
return m_DataSize;
}
bool FIFO::SupportsWaiting() const
{
return true;
}
bool FIFO::IsDataAvailable() const
{
return m_DataSize > 0;
2015-06-22 11:11:21 +02:00
}