avoid extra memory allocations when using PcapWriter
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Mon, 08 Jun 2009 16:43:21 +0200
changeset 4518 31f2881aa214
parent 4517 3530045a96da
child 4519 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
--- a/src/common/buffer.cc	Mon Jun 08 16:42:06 2009 +0200
+++ b/src/common/buffer.cc	Mon Jun 08 16:43:21 2009 +0200
@@ -643,6 +643,32 @@
   return m_data->m_data + m_start;
 }
 
+void
+Buffer::CopyData(std::ostream *os, uint32_t size) const
+{
+  if (size == GetSize ())
+    {
+      // fast path
+      os->write((const char*)(m_data->m_data + m_start), m_zeroAreaStart-m_start);
+      char zero = 0;
+      for (uint32_t i = 0; i < m_zeroAreaEnd - m_zeroAreaStart; ++i)
+        {
+          os->write (&zero, 1);
+        }
+      os->write ((const char*)(m_data->m_data + m_zeroAreaStart), m_end - m_zeroAreaEnd);
+    }
+  else
+    {
+      // slow path
+      Buffer::Iterator i = Begin ();
+      while (!i.IsEnd () && size > 0)
+        {
+          char byte = i.ReadU8 ();
+          os->write (&byte, 1);
+        }
+    }
+}
+
 /******************************************************
  *            The buffer iterator below.
  ******************************************************/
--- a/src/common/buffer.h	Mon Jun 08 16:42:06 2009 +0200
+++ b/src/common/buffer.h	Mon Jun 08 16:43:21 2009 +0200
@@ -22,6 +22,7 @@
 
 #include <stdint.h>
 #include <vector>
+#include <ostream>
 
 #define BUFFER_HEURISTICS 1
 #define BUFFER_USE_INLINE 1
@@ -486,6 +487,8 @@
   int32_t GetCurrentStartOffset (void) const;
   int32_t GetCurrentEndOffset (void) const;
 
+  void CopyData (std::ostream *os, uint32_t size) const;
+
   Buffer (Buffer const &o);
   Buffer &operator = (Buffer const &o);
   Buffer ();
--- a/src/common/packet.cc	Mon Jun 08 16:42:06 2009 +0200
+++ b/src/common/packet.cc	Mon Jun 08 16:43:21 2009 +0200
@@ -356,6 +356,12 @@
   return cur;
 }
 
+void
+Packet::CopyData(std::ostream *os, uint32_t size) const
+{
+  return m_buffer.CopyData (os, size);
+}
+
 uint32_t 
 Packet::GetUid (void) const
 {
--- a/src/common/packet.h	Mon Jun 08 16:42:06 2009 +0200
+++ b/src/common/packet.h	Mon Jun 08 16:43:21 2009 +0200
@@ -344,6 +344,8 @@
    */
   uint32_t CopyData (uint8_t *buffer, uint32_t size) const;
 
+  void CopyData(std::ostream *os, uint32_t size) const;
+
   /**
    * A packet is allocated a new uid when it is created
    * empty or with zero-filled payload.
--- a/src/common/pcap-writer.cc	Mon Jun 08 16:42:06 2009 +0200
+++ b/src/common/pcap-writer.cc	Mon Jun 08 16:43:21 2009 +0200
@@ -164,7 +164,7 @@
       Write32 (us & 0xffffffff);
       Write32 (packet->GetSize ());
       Write32 (packet->GetSize ());
-      WriteData (packet->PeekData (), packet->GetSize ());
+      packet->CopyData (m_writer, packet->GetSize ());
     }
 }
 
@@ -412,7 +412,7 @@
     }    
 
   // finally, write rest of packet
-  WriteData (packet->PeekData (), packet->GetSize ());
+  packet->CopyData (m_writer, packet->GetSize ());
 }