BIND 10 trac536, updated. 8599f0c0c2ebe7043dd65458a2302837f7c43895 [trac536] Allocation and dealocation routines

BIND 10 source code commits bind10-changes at lists.isc.org
Fri Jan 28 19:38:46 UTC 2011


The branch, trac536 has been updated
       via  8599f0c0c2ebe7043dd65458a2302837f7c43895 (commit)
      from  902005dceae646aefc03c2005e1ff2896efb909c (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 8599f0c0c2ebe7043dd65458a2302837f7c43895
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Fri Jan 28 20:37:15 2011 +0100

    [trac536] Allocation and dealocation routines
    
    The rest of class is still not updated, it doesn't compile yet.

-----------------------------------------------------------------------

Summary of changes:
 src/lib/dns/buffer.h |   70 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 67 insertions(+), 3 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/lib/dns/buffer.h b/src/lib/dns/buffer.h
index e100876..0f9514b 100644
--- a/src/lib/dns/buffer.h
+++ b/src/lib/dns/buffer.h
@@ -15,6 +15,8 @@
 #ifndef __BUFFER_H
 #define __BUFFER_H 1
 
+#include <cstdlib>
+#include <cstring>
 #include <vector>
 
 #include <string.h>
@@ -286,9 +288,51 @@ public:
     /// \brief Constructor from the initial size of the buffer.
     ///
     /// \param len The initial length of the buffer in bytes.
-    OutputBuffer(size_t len) { data_.reserve(len); }
+    OutputBuffer(size_t len) :
+        buffer_(NULL),
+        size_(0),
+        allocated_(len)
+    {
+        // We use malloc and free instead of C++ new[] and delete[].
+        // This way we can use realloc, which may in fact do it without a copy.
+        buffer_ = static_cast<uint8_t*>(malloc(allocated_));
+        if (buffer_ == NULL) {
+            throw std::bad_alloc();
+        }
+    }
+
+    /// \brief Copy constructor
+    OutputBuffer(const OutputBuffer& other) :
+        buffer_(NULL),
+        size_(other.size_),
+        allocated_(other.allocated_)
+    {
+        buffer_ = static_cast<uint8_t*>(malloc(allocated_));
+        if (buffer_ == NULL) {
+            throw std::bad_alloc();
+        }
+        memcpy(buffer_, other.buffer_, size_);
+    }
+
+    ~ OutputBuffer() {
+        free(buffer_);
+    }
     //@}
 
+    /// \brief Assignment operator
+    OutputBuffer& operator =(const OutputBuffer& other) {
+        uint8_t* newbuff(static_cast<uint8_t*>(malloc(other.allocated_)));
+        if (newbuff == NULL) {
+            throw std::bad_alloc();
+        }
+        free(buffer_);
+        buffer_ = newbuff;
+        size_ = other.size_;
+        allocated_ = other.allocated_;
+        memcpy(buffer_, other.buffer_, size_);
+        return (*this);
+    }
+
     ///
     /// \name Getter Methods
     ///
@@ -408,9 +452,29 @@ public:
         data_.insert(data_.end(), cp, cp + len);
     }
     //@}
-    
+
 private:
-    std::vector<uint8_t> data_;
+    uint8_t* buffer_;
+    size_t size_;
+    size_t allocated_;
+    void ensureAllocated(size_t needed_size) {
+        if (allocated_ < needed_size) {
+            // Guess some bigger size
+            size_t new_size = (allocated_ == 0) ? 1024 : allocated_;
+            while (new_size < needed_size) {
+                new_size *= 2;
+            }
+            // Allocate bigger space
+            uint8_t* new_buffer_(static_cast<uint8_t*>(realloc(buffer_,
+                new_size)));
+            if (new_buffer_ == NULL) {
+                // If it fails, the original block is left intact by it
+                throw std::bad_alloc();
+            }
+            buffer_ = new_buffer_;
+            allocated_ = new_size;
+        }
+    }
 };
 
 /// \brief Pointer-like types pointing to \c InputBuffer or \c OutputBuffer




More information about the bind10-changes mailing list