1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/fifo.hpp"
5 using namespace icinga;
8 * Destructor for the FIFO class.
16 * Resizes the FIFO's buffer so that it is at least newSize bytes long.
18 * @param newSize The minimum new size of the FIFO buffer.
20 void FIFO::ResizeBuffer(size_t newSize, bool decrease)
22 if (m_AllocSize >= newSize && !decrease)
25 newSize = (newSize / FIFO::BlockSize + 1) * FIFO::BlockSize;
27 if (newSize == m_AllocSize)
30 auto *newBuffer = static_cast<char *>(realloc(m_Buffer, newSize));
33 BOOST_THROW_EXCEPTION(std::bad_alloc());
37 m_AllocSize = newSize;
41 * Optimizes memory usage of the FIFO buffer by reallocating
42 * and moving the buffer.
46 if (m_Offset > m_DataSize / 10 && m_Offset - m_DataSize > 1024) {
47 std::memmove(m_Buffer, m_Buffer + m_Offset, m_DataSize);
51 ResizeBuffer(m_DataSize, true);
57 size_t FIFO::Peek(void *buffer, size_t count, bool allow_partial)
59 ASSERT(allow_partial);
61 if (count > m_DataSize)
65 std::memcpy(buffer, m_Buffer + m_Offset, count);
71 * Implements IOQueue::Read.
73 size_t FIFO::Read(void *buffer, size_t count, bool allow_partial)
75 ASSERT(allow_partial);
77 if (count > m_DataSize)
81 std::memcpy(buffer, m_Buffer + m_Offset, count);
92 * Implements IOQueue::Write.
94 void FIFO::Write(const void *buffer, size_t count)
96 ResizeBuffer(m_Offset + m_DataSize + count, false);
97 std::memcpy(m_Buffer + m_Offset + m_DataSize, buffer, count);
100 SignalDataAvailable();
106 bool FIFO::IsEof() const
111 size_t FIFO::GetAvailableBytes() const
116 bool FIFO::SupportsWaiting() const
121 bool FIFO::IsDataAvailable() const
123 return m_DataSize > 0;