Browse Source

[878] Pkt6 now uses boost::shared_array to manage its buffer.

Tomek Mrugalski 13 years ago
parent
commit
68653f1c82

+ 2 - 2
src/bin/dhcp6/iface_mgr.cc

@@ -366,7 +366,7 @@ IfaceMgr::send(Pkt6 &pkt) {
      * "scatter-gather" stuff... we only have a single chunk
      * of data to send, so we declare a single vector entry.)
      */
-    v.iov_base = (char *) pkt.data_;
+    v.iov_base = (char *) &pkt.data_[0];
     v.iov_len = pkt.data_len_;
     m.msg_iov = &v;
     m.msg_iovlen = 1;
@@ -445,7 +445,7 @@ IfaceMgr::receive() {
      * "scatter-gather" stuff... but we that doesn't really make
      * sense for us, so we use a single vector entry.)
      */
-    v.iov_base = pkt->data_;
+    v.iov_base = (void*)&pkt->data_[0];
     v.iov_len = pkt->data_len_;
     m.msg_iov = &v;
     m.msg_iovlen = 1;

+ 2 - 32
src/bin/dhcp6/pkt6.cc

@@ -22,55 +22,25 @@ namespace isc {
 ///
 /// constructor
 ///
-/// This constructor is used during packet reception.
-///
-/// Note: Pkt6 will take ownership of any data passed
-/// (due to performance reasons). Copying data on creation
-/// would be more elegant, but slower.
-///
-/// \param data
-/// \param dataLen
-///
-Pkt6::Pkt6(char * data, int dataLen)
-    :local_addr_("::"),
-     remote_addr_("::")
-{
-    data_ = data;
-    data_len_ = dataLen;
-}
-
-///
-/// constructor
-///
-/// This constructor is used for generated packets.
-///
-/// Note: Pkt6 will take ownership of any data passed
-/// (due to performance reasons). Copying data on creation
-/// would be more elegant, but slower.
-///
 /// \param dataLen - length of the data to be allocated
 ///
 Pkt6::Pkt6(int dataLen)
     :local_addr_("::"),
      remote_addr_("::") {
     try {
-	data_ = new char[dataLen];
+	data_ = boost::shared_array<char>(new char[dataLen]);
 	data_len_ = dataLen;
     } catch (const std::exception& ex) {
 	// TODO move to LOG_FATAL()
 	// let's continue with empty pkt for now
         std::cout << "Failed to allocate " << dataLen << " bytes."
                   << std::endl;
-        data_ = 0;
         data_len_ = 0;
     }
 }
 
 Pkt6::~Pkt6() {
-    if (data_) {
-        delete [] data_;
-        data_ = 0;
-    }
+    // no need to delete anything shared_ptr will take care of data_
 }
 
 };

+ 17 - 3
src/bin/dhcp6/pkt6.h

@@ -16,32 +16,46 @@
 #define PKT6_H
 
 #include <iostream>
+#include <boost/shared_array.hpp>
 #include "io_address.h"
 
 namespace isc {
 
     class Pkt6 {
     public:
-        Pkt6(char * data, int dataLen);
         Pkt6(int len);
         ~Pkt6();
 
         // XXX: probably need getter/setter wrappers
         //      and hide fields as protected
-        char * data_;
+        // buffer that holds memory. It is shared_array as options may
+        // share pointer to this buffer
+        boost::shared_array<char> data_;
+
+        // length of the data
         int data_len_;
 
+        // local address (destination if receiving packet, source if sending packet)
         isc::asiolink::IOAddress local_addr_;
+
+        // remote address (source if receiving packet, destination if sending packet)
         isc::asiolink::IOAddress remote_addr_;
 
+        // name of the network interface the packet was received/to be sent over
         std::string iface_;
+
+        // interface index (each network interface has assigned unique ifindex
+        // it is functional equvalent of name, but sometimes more useful, e.g.
+        // when using crazy systems that allow spaces in interface names (Windows)
         int ifindex_;
 
+        // local TDP or UDP port
         int local_port_;
+
+        // remote TCP or UDP port
         int remote_port_;
 
         // XXX: add *a lot* here
-
     };
 }
 

+ 1 - 1
src/bin/dhcp6/tests/iface_mgr_unittest.cc

@@ -186,7 +186,7 @@ TEST_F(IfaceMgrTest, sendReceive) {
 
     // let's check that we received what was sent
     EXPECT_EQ(sendPkt.data_len_, rcvPkt->data_len_);
-    EXPECT_EQ(0, memcmp(sendPkt.data_, rcvPkt->data_, rcvPkt->data_len_) );
+    EXPECT_EQ(0, memcmp(&sendPkt.data_[0], &rcvPkt->data_[0], rcvPkt->data_len_) );
 
     EXPECT_EQ(sendPkt.remote_addr_, rcvPkt->remote_addr_);
     EXPECT_EQ(rcvPkt->remote_port_, 10546);

+ 0 - 9
src/bin/dhcp6/tests/pkt6_unittest.cc

@@ -38,16 +38,7 @@ TEST_F(Pkt6Test, constructor) {
     
     ASSERT_EQ(pkt1->data_len_, 17);
 
-    char * buf = new char[23];
-    // can't use char buf[23], as Pkt6 takes ownership of the data
-
-    Pkt6 * pkt2 = new Pkt6(buf, 23);
-
-    ASSERT_EQ(pkt2->data_len_, 23);
-    ASSERT_EQ(pkt2->data_, buf);
-
     delete pkt1;
-    delete pkt2;
 }
 
 }