From c7f8ec38fd536ab8d4d7d50bdd67ee47ad274f27 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 29 Mar 2012 07:42:30 +0200 Subject: [PATCH] Simplified FIFO implementation Fixed a memoryleak in Netstring class --- base/fifo.cpp | 58 ++++++++++++++----------------------------- base/fifo.h | 5 ++-- jsonrpc/netstring.cpp | 2 ++ 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/base/fifo.cpp b/base/fifo.cpp index fb75286fa..6330256b8 100644 --- a/base/fifo.cpp +++ b/base/fifo.cpp @@ -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); - - size_t ceilNewSize = newSize + FIFO::BlockSize - (newSize % FIFO::BlockSize); - size_t oldBlocks = oldSize / FIFO::BlockSize; - size_t newBlocks = ceilNewSize / FIFO::BlockSize; + if (m_AllocSize >= newSize) + return; - if (oldBlocks == newBlocks) - return buffer; + newSize = (newSize / FIFO::BlockSize + 1) * FIFO::BlockSize; - 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; - - if (m_Offset == 0 || m_Offset < m_BufferSize / 5) - return; + //char *newBuffer; - 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; - - new_buffer = ResizeBuffer(m_Buffer, m_BufferSize, m_BufferSize + count); - - if (new_buffer == NULL) - throw exception(/*"Out of memory."*/); - - m_Buffer = new_buffer; + ResizeBuffer(m_Offset + m_DataSize + count); - 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; } diff --git a/base/fifo.h b/base/fifo.h index 03e8ee30d..71654e2f2 100644 --- a/base/fifo.h +++ b/base/fifo.h @@ -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: diff --git a/jsonrpc/netstring.cpp b/jsonrpc/netstring.cpp index 3762f754c..7b0885041 100644 --- a/jsonrpc/netstring.cpp +++ b/jsonrpc/netstring.cpp @@ -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); } -- 2.40.0