2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* 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; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* 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 *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
using std::bad_alloc;
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the FIFO class.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
FIFO::FIFO(void)
|
|
|
|
{
|
|
|
|
m_Buffer = NULL;
|
2012-03-29 07:42:30 +02:00
|
|
|
m_DataSize = 0;
|
|
|
|
m_AllocSize = 0;
|
2012-03-28 13:24:49 +02:00
|
|
|
m_Offset = 0;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Destructor for the FIFO class.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
FIFO::~FIFO(void)
|
|
|
|
{
|
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.
|
|
|
|
*/
|
2012-03-29 07:42:30 +02:00
|
|
|
void FIFO::ResizeBuffer(size_t newSize)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-29 07:42:30 +02:00
|
|
|
if (m_AllocSize >= newSize)
|
|
|
|
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
|
|
|
|
2012-05-26 20:19:32 +02:00
|
|
|
char *newBuffer = (char *)realloc(m_Buffer, newSize);
|
|
|
|
|
|
|
|
if (newBuffer == NULL)
|
|
|
|
throw bad_alloc();
|
|
|
|
|
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.
|
|
|
|
*/
|
2012-03-28 13:24:49 +02:00
|
|
|
void FIFO::Optimize(void)
|
|
|
|
{
|
2012-03-29 07:42:30 +02:00
|
|
|
//char *newBuffer;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-03-29 07:42:30 +02:00
|
|
|
if (m_DataSize < m_Offset) {
|
|
|
|
memcpy(m_Buffer, m_Buffer + m_Offset, m_DataSize);
|
2012-03-28 13:24:49 +02:00
|
|
|
m_Offset = 0;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-29 07:42:30 +02:00
|
|
|
/*newBuffer = (char *)ResizeBuffer(NULL, 0, m_BufferSize - m_Offset);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
if (newBuffer == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(newBuffer, m_Buffer + m_Offset, m_BufferSize - m_Offset);
|
|
|
|
|
|
|
|
free(m_Buffer);
|
|
|
|
m_Buffer = newBuffer;
|
|
|
|
m_BufferSize -= m_Offset;
|
2012-03-29 07:42:30 +02:00
|
|
|
m_Offset = 0;*/
|
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::GetAvailableBytes().
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2012-07-16 00:02:31 +02:00
|
|
|
size_t FIFO::GetAvailableBytes(void) const
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-29 07:42:30 +02:00
|
|
|
return m_DataSize;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Returns a pointer to the start of the read buffer.
|
|
|
|
*
|
|
|
|
* @returns Pointer to the read buffer.
|
|
|
|
*/
|
2012-07-16 00:02:31 +02:00
|
|
|
/*const void *FIFO::GetReadBuffer(void) const
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
return m_Buffer + m_Offset;
|
2012-07-16 00:02:31 +02:00
|
|
|
}*/
|
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::Peek.
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2012-07-16 00:02:31 +02:00
|
|
|
void FIFO::Peek(void *buffer, size_t count)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-07-16 00:02:31 +02:00
|
|
|
assert(m_DataSize >= count);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
if (buffer != NULL)
|
|
|
|
memcpy(buffer, m_Buffer + m_Offset, count);
|
2012-07-16 00:02:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements IOQueue::Read.
|
|
|
|
*/
|
|
|
|
void FIFO::Read(void *buffer, size_t count)
|
|
|
|
{
|
|
|
|
Peek(buffer, count);
|
2012-03-28 13:24:49 +02: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-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Returns a pointer to the start of the write buffer.
|
|
|
|
*
|
|
|
|
* @param count Minimum size of the buffer; on return this parameter
|
|
|
|
* contains the actual size of the available buffer which can
|
|
|
|
* be larger than the requested size.
|
|
|
|
*/
|
2012-07-16 00:02:31 +02:00
|
|
|
/*void *FIFO::GetWriteBuffer(size_t *count)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-29 20:03:29 +02:00
|
|
|
ResizeBuffer(m_Offset + m_DataSize + *count);
|
|
|
|
*count = m_AllocSize - m_Offset - m_DataSize;
|
2012-03-28 19:50:55 +02:00
|
|
|
|
2012-03-29 07:42:30 +02:00
|
|
|
return m_Buffer + m_Offset + m_DataSize;
|
2012-07-16 00:02:31 +02:00
|
|
|
}*/
|
2012-03-28 19:50:55 +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
|
|
|
{
|
2012-07-16 00:02:31 +02:00
|
|
|
ResizeBuffer(m_Offset + m_DataSize + count);
|
|
|
|
memcpy(m_Buffer + m_Offset + m_DataSize, buffer, count);
|
2012-03-29 07:42:30 +02:00
|
|
|
m_DataSize += count;
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|