avoid extra memory allocations when using PcapWriter
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon Jun 08 16:43:21 2009 +0200 (8 months ago)
changeset 453331f2881aa214
parent 4532 3530045a96da
child 4534 d3d369596c56
avoid extra memory allocations when using PcapWriter
src/common/buffer.cc
src/common/buffer.h
src/common/packet.cc
src/common/packet.h
src/common/pcap-writer.cc
     1.1 --- a/src/common/buffer.cc	Mon Jun 08 16:42:06 2009 +0200
     1.2 +++ b/src/common/buffer.cc	Mon Jun 08 16:43:21 2009 +0200
     1.3 @@ -643,6 +643,32 @@
     1.4    return m_data->m_data + m_start;
     1.5  }
     1.6  
     1.7 +void
     1.8 +Buffer::CopyData(std::ostream *os, uint32_t size) const
     1.9 +{
    1.10 +  if (size == GetSize ())
    1.11 +    {
    1.12 +      // fast path
    1.13 +      os->write((const char*)(m_data->m_data + m_start), m_zeroAreaStart-m_start);
    1.14 +      char zero = 0;
    1.15 +      for (uint32_t i = 0; i < m_zeroAreaEnd - m_zeroAreaStart; ++i)
    1.16 +        {
    1.17 +          os->write (&zero, 1);
    1.18 +        }
    1.19 +      os->write ((const char*)(m_data->m_data + m_zeroAreaStart), m_end - m_zeroAreaEnd);
    1.20 +    }
    1.21 +  else
    1.22 +    {
    1.23 +      // slow path
    1.24 +      Buffer::Iterator i = Begin ();
    1.25 +      while (!i.IsEnd () && size > 0)
    1.26 +        {
    1.27 +          char byte = i.ReadU8 ();
    1.28 +          os->write (&byte, 1);
    1.29 +        }
    1.30 +    }
    1.31 +}
    1.32 +
    1.33  /******************************************************
    1.34   *            The buffer iterator below.
    1.35   ******************************************************/
     2.1 --- a/src/common/buffer.h	Mon Jun 08 16:42:06 2009 +0200
     2.2 +++ b/src/common/buffer.h	Mon Jun 08 16:43:21 2009 +0200
     2.3 @@ -22,6 +22,7 @@
     2.4  
     2.5  #include <stdint.h>
     2.6  #include <vector>
     2.7 +#include <ostream>
     2.8  
     2.9  #define BUFFER_HEURISTICS 1
    2.10  #define BUFFER_USE_INLINE 1
    2.11 @@ -486,6 +487,8 @@
    2.12    int32_t GetCurrentStartOffset (void) const;
    2.13    int32_t GetCurrentEndOffset (void) const;
    2.14  
    2.15 +  void CopyData (std::ostream *os, uint32_t size) const;
    2.16 +
    2.17    Buffer (Buffer const &o);
    2.18    Buffer &operator = (Buffer const &o);
    2.19    Buffer ();
     3.1 --- a/src/common/packet.cc	Mon Jun 08 16:42:06 2009 +0200
     3.2 +++ b/src/common/packet.cc	Mon Jun 08 16:43:21 2009 +0200
     3.3 @@ -356,6 +356,12 @@
     3.4    return cur;
     3.5  }
     3.6  
     3.7 +void
     3.8 +Packet::CopyData(std::ostream *os, uint32_t size) const
     3.9 +{
    3.10 +  return m_buffer.CopyData (os, size);
    3.11 +}
    3.12 +
    3.13  uint32_t 
    3.14  Packet::GetUid (void) const
    3.15  {
     4.1 --- a/src/common/packet.h	Mon Jun 08 16:42:06 2009 +0200
     4.2 +++ b/src/common/packet.h	Mon Jun 08 16:43:21 2009 +0200
     4.3 @@ -344,6 +344,8 @@
     4.4     */
     4.5    uint32_t CopyData (uint8_t *buffer, uint32_t size) const;
     4.6  
     4.7 +  void CopyData(std::ostream *os, uint32_t size) const;
     4.8 +
     4.9    /**
    4.10     * A packet is allocated a new uid when it is created
    4.11     * empty or with zero-filled payload.
     5.1 --- a/src/common/pcap-writer.cc	Mon Jun 08 16:42:06 2009 +0200
     5.2 +++ b/src/common/pcap-writer.cc	Mon Jun 08 16:43:21 2009 +0200
     5.3 @@ -164,7 +164,7 @@
     5.4        Write32 (us & 0xffffffff);
     5.5        Write32 (packet->GetSize ());
     5.6        Write32 (packet->GetSize ());
     5.7 -      WriteData (packet->PeekData (), packet->GetSize ());
     5.8 +      packet->CopyData (m_writer, packet->GetSize ());
     5.9      }
    5.10  }
    5.11  
    5.12 @@ -412,7 +412,7 @@
    5.13      }    
    5.14  
    5.15    // finally, write rest of packet
    5.16 -  WriteData (packet->PeekData (), packet->GetSize ());
    5.17 +  packet->CopyData (m_writer, packet->GetSize ());
    5.18  }
    5.19      
    5.20