mirror of https://github.com/Icinga/icinga2.git
Simplified FIFO implementation
Fixed a memoryleak in Netstring class
This commit is contained in:
parent
badb51338c
commit
c7f8ec38fd
|
@ -5,7 +5,8 @@ using namespace icinga;
|
|||
FIFO::FIFO(void)
|
||||
{
|
||||
m_Buffer = NULL;
|
||||
m_BufferSize = 0;
|
||||
m_DataSize = 0;
|
||||
m_AllocSize = 0;
|
||||
m_Offset = 0;
|
||||
}
|
||||
|
||||
|
@ -14,44 +15,29 @@ FIFO::~FIFO(void)
|
|||
Memory::Free(m_Buffer);
|
||||
}
|
||||
|
||||
char *FIFO::ResizeBuffer(char *buffer, size_t oldSize, size_t newSize)
|
||||
void FIFO::ResizeBuffer(size_t newSize)
|
||||
{
|
||||
if (oldSize != 0)
|
||||
oldSize += FIFO::BlockSize - (oldSize % FIFO::BlockSize);
|
||||
if (m_AllocSize >= newSize)
|
||||
return;
|
||||
|
||||
size_t ceilNewSize = newSize + FIFO::BlockSize - (newSize % FIFO::BlockSize);
|
||||
size_t oldBlocks = oldSize / FIFO::BlockSize;
|
||||
size_t newBlocks = ceilNewSize / FIFO::BlockSize;
|
||||
newSize = (newSize / FIFO::BlockSize + 1) * FIFO::BlockSize;
|
||||
|
||||
if (oldBlocks == newBlocks)
|
||||
return buffer;
|
||||
|
||||
if (newSize == 0) {
|
||||
Memory::Free(buffer);
|
||||
return NULL;
|
||||
} else {
|
||||
return (char *)Memory::Reallocate(buffer, newBlocks * FIFO::BlockSize);
|
||||
}
|
||||
m_Buffer = (char *)Memory::Reallocate(m_Buffer, newSize);
|
||||
m_AllocSize = newSize;
|
||||
}
|
||||
|
||||
void FIFO::Optimize(void)
|
||||
{
|
||||
char *newBuffer;
|
||||
//char *newBuffer;
|
||||
|
||||
if (m_Offset == 0 || m_Offset < m_BufferSize / 5)
|
||||
return;
|
||||
|
||||
if (m_BufferSize - m_Offset == 0) {
|
||||
free(m_Buffer);
|
||||
|
||||
m_Buffer = 0;
|
||||
m_BufferSize = 0;
|
||||
if (m_DataSize < m_Offset) {
|
||||
memcpy(m_Buffer, m_Buffer + m_Offset, m_DataSize);
|
||||
m_Offset = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
newBuffer = (char *)ResizeBuffer(NULL, 0, m_BufferSize - m_Offset);
|
||||
/*newBuffer = (char *)ResizeBuffer(NULL, 0, m_BufferSize - m_Offset);
|
||||
|
||||
if (newBuffer == NULL)
|
||||
return;
|
||||
|
@ -61,12 +47,12 @@ void FIFO::Optimize(void)
|
|||
free(m_Buffer);
|
||||
m_Buffer = newBuffer;
|
||||
m_BufferSize -= m_Offset;
|
||||
m_Offset = 0;
|
||||
m_Offset = 0;*/
|
||||
}
|
||||
|
||||
size_t FIFO::GetSize(void) const
|
||||
{
|
||||
return m_BufferSize - m_Offset;
|
||||
return m_DataSize;
|
||||
}
|
||||
|
||||
const void *FIFO::GetReadBuffer(void) const
|
||||
|
@ -76,11 +62,12 @@ const void *FIFO::GetReadBuffer(void) const
|
|||
|
||||
size_t FIFO::Read(void *buffer, size_t count)
|
||||
{
|
||||
count = (count <= m_BufferSize) ? count : m_BufferSize;
|
||||
count = (count <= m_DataSize) ? count : m_DataSize;
|
||||
|
||||
if (buffer != NULL)
|
||||
memcpy(buffer, m_Buffer + m_Offset, count);
|
||||
|
||||
m_DataSize -= count;
|
||||
m_Offset += count;
|
||||
|
||||
Optimize();
|
||||
|
@ -90,16 +77,9 @@ size_t FIFO::Read(void *buffer, size_t count)
|
|||
|
||||
void *FIFO::GetWriteBuffer(size_t count)
|
||||
{
|
||||
char *new_buffer;
|
||||
ResizeBuffer(m_Offset + m_DataSize + count);
|
||||
|
||||
new_buffer = ResizeBuffer(m_Buffer, m_BufferSize, m_BufferSize + count);
|
||||
|
||||
if (new_buffer == NULL)
|
||||
throw exception(/*"Out of memory."*/);
|
||||
|
||||
m_Buffer = new_buffer;
|
||||
|
||||
return new_buffer + m_Offset + m_BufferSize;
|
||||
return m_Buffer + m_Offset + m_DataSize;
|
||||
}
|
||||
|
||||
size_t FIFO::Write(const void *buffer, size_t count)
|
||||
|
@ -109,7 +89,7 @@ size_t FIFO::Write(const void *buffer, size_t count)
|
|||
memcpy(target_buffer, buffer, count);
|
||||
}
|
||||
|
||||
m_BufferSize += count;
|
||||
m_DataSize += count;
|
||||
|
||||
return count;
|
||||
}
|
||||
|
|
|
@ -8,10 +8,11 @@ class FIFO : public Object
|
|||
{
|
||||
private:
|
||||
char *m_Buffer;
|
||||
size_t m_BufferSize;
|
||||
size_t m_DataSize;
|
||||
size_t m_AllocSize;
|
||||
size_t m_Offset;
|
||||
|
||||
char *ResizeBuffer(char *buffer, size_t oldSize, size_t newSize);
|
||||
void ResizeBuffer(size_t newSize);
|
||||
void Optimize(void);
|
||||
|
||||
public:
|
||||
|
|
|
@ -66,5 +66,7 @@ void Netstring::WriteJSONToFIFO(FIFO::RefType fifo, cJSON *object)
|
|||
|
||||
fifo->Write(strLength, strlen(strLength));
|
||||
fifo->Write(json, len);
|
||||
free(json);
|
||||
|
||||
fifo->Write(",", 1);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue