gnash-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gnash-commit] /srv/bzr/gnash/rtmp r9641: Use scoped_array instead of a


From: rob
Subject: [Gnash-commit] /srv/bzr/gnash/rtmp r9641: Use scoped_array instead of a raw pointer.
Date: Fri, 19 Sep 2008 16:11:12 -0600
User-agent: Bazaar (1.5)

------------------------------------------------------------
revno: 9641
committer: address@hidden
branch nick: rtmp
timestamp: Fri 2008-09-19 16:11:12 -0600
message:
  Use scoped_array instead of a raw pointer.
  Drop extraneous copy and append methods, implement them as operator= and
  operator+= instead.
modified:
  libamf/buffer.cpp
  libamf/buffer.h
  testsuite/libamf.all/test_buffer.cpp
=== modified file 'libamf/buffer.cpp'
--- a/libamf/buffer.cpp 2008-08-02 22:55:21 +0000
+++ b/libamf/buffer.cpp 2008-09-19 22:11:12 +0000
@@ -18,10 +18,12 @@
 
 #include <boost/cstdint.hpp>
 #include <iostream>
+
 #include "buffer.h"
 #include "amf.h"
 #include "log.h"
 #include "network.h"
+#include "GnashException.h"
 
 using namespace std;
 using namespace gnash;
@@ -74,28 +76,27 @@
 }
 #endif
 
-void *
-Buffer::init(size_t nbytes)
+// Initialize a Buffer's storage to the specified size
+Buffer &
+Buffer::init(size_t size)
 {
 //    GNASH_REPORT_FUNCTION;
-    if (_ptr == 0) {
-        _ptr = new Network::byte_t[nbytes];
-       _seekptr = _ptr;
-       if (_ptr == 0) {
-           return _ptr;
-       }
-        _nbytes = nbytes;
+    if (!_data) {
+       _data.reset(new Network::byte_t[size]);
+       _seekptr = _data.get();
     }
+    _seekptr = _data.get();
+    _nbytes = size;
 
 #ifdef USE_STATS_BUFFERS
     clock_gettime (CLOCK_REALTIME, &_stamp);
 #endif
-    return _ptr;
+    
+    return *this;
 }
 
 Buffer::Buffer() 
-    : _seekptr(0),
-      _ptr(0)
+    : _seekptr(0)
 {
 //    GNASH_REPORT_FUNCTION;
     _nbytes = gnash::NETBUFSIZE;
@@ -104,8 +105,7 @@
     
 // Create with a size other than the default
 Buffer::Buffer(size_t nbytes)
-    : _seekptr(0),
-      _ptr(0)
+    : _seekptr(0)
 {
 //    GNASH_REPORT_FUNCTION;
     _nbytes = nbytes;
@@ -116,29 +116,51 @@
 Buffer::~Buffer()
 {
 //    GNASH_REPORT_FUNCTION;
-    if (_ptr) {
+    if (_data) {
 #ifdef USE_STATS_BUFFERS
        struct timespec now;
        clock_gettime (CLOCK_REALTIME, &now);
        log_debug("Buffer %x (%d) stayed in queue for %f seconds",
-                 (void *)_ptr, _nbytes,
+                 (void *)_data.get(), _nbytes,
                  (float)((now.tv_sec - _stamp.tv_sec) + ((now.tv_nsec - 
_stamp.tv_nsec)/1e9)));
 #endif
-        delete[] _ptr;
-        _seekptr = _ptr = 0;
+        _seekptr = 0;
         _nbytes = 0;
     }
 }
 
 // Put data into the buffer
-void
+Buffer &
 Buffer::copy(Network::byte_t *data, size_t nbytes)
 {    
 //    GNASH_REPORT_FUNCTION;
-    std::copy(data, data + nbytes, _ptr);
-    _seekptr = _ptr + nbytes;
-}
-
+    if (_data) {
+       std::copy(data, data + nbytes, _data.get());
+       _seekptr = _data.get() + nbytes;
+    } else {
+       throw GnashException("Not enough storage was allocated to hold the 
data!");
+    }
+    return *this;
+}
+
+
+Buffer &
+Buffer::append(gnash::Network::byte_t *data, size_t nbytes)
+{
+//    GNASH_REPORT_FUNCTION;
+    if (_data) {
+       if (spaceLeft() >= nbytes) {
+           std::copy(data, data + nbytes, _seekptr);
+           _seekptr += nbytes;
+       } else {
+           throw GnashException("Not enough storage was allocated to hold the 
appended data!");
+       }
+    }
+
+    return *this;
+}
+
+#if 0
 void
 Buffer::copy(const string &str)
 {    
@@ -151,7 +173,7 @@
 Buffer::copy(boost::uint16_t length)
 {
     Network::byte_t *data = reinterpret_cast<Network::byte_t *>(&length);
-    std::copy(data, data + sizeof(boost::uint16_t), _ptr);    
+    std::copy(data, data + sizeof(boost::uint16_t), _ptr);
     _seekptr = _ptr + sizeof(boost::uint16_t);
 }
 
@@ -160,7 +182,7 @@
 {
 //    GNASH_REPORT_FUNCTION;
     Network::byte_t *ptr = reinterpret_cast<Network::byte_t *>(&num);
-    std::copy(ptr, ptr + amf::AMF0_NUMBER_SIZE, _ptr);    
+    std::copy(ptr, ptr + amf::AMF0_NUMBER_SIZE, _ptr);
     _seekptr = _ptr + amf::AMF0_NUMBER_SIZE;
 }
 
@@ -171,6 +193,7 @@
     *_ptr = val;
     _seekptr = _ptr + sizeof(bool);
 }
+#endif
 
 #if 0
 void
@@ -181,6 +204,7 @@
 }
 #endif
 
+#if 0
 Network::byte_t *
 Buffer::append(boost::uint16_t length)
 {
@@ -189,6 +213,7 @@
     if ((_seekptr + sizeof(boost::uint16_t)) <= (_ptr + _nbytes)) {
        Network::byte_t *data = reinterpret_cast<Network::byte_t *>(&length);
        std::copy(data, data + sizeof(boost::uint16_t), _seekptr);
+       std::copy(data, data + sizeof(boost::uint16_t), _data.get());
        _seekptr += sizeof(boost::uint16_t);
        return _seekptr;
     }
@@ -201,7 +226,7 @@
 //    GNASH_REPORT_FUNCTION;
 
     if ((_seekptr + nbytes) <= (_ptr + _nbytes)) {
-       std::copy(data, data + nbytes, _seekptr);    
+       std::copy(data, data + nbytes, _seekptr);
        _seekptr += nbytes;
        return _seekptr;
     }
@@ -214,7 +239,7 @@
 //    GNASH_REPORT_FUNCTION;
     if ((_seekptr + sizeof(double)) <= (_ptr + _nbytes)) {
        Network::byte_t *ptr = reinterpret_cast<Network::byte_t *>(&num);
-       std::copy(ptr, ptr + amf::AMF0_NUMBER_SIZE, _seekptr);    
+       std::copy(ptr, ptr + amf::AMF0_NUMBER_SIZE, _seekptr);
        _seekptr += amf::AMF0_NUMBER_SIZE;
        return _seekptr;
     }
@@ -293,33 +318,17 @@
 
     return _seekptr;
 }
+#endif
 
 // make ourselves be able to appended too. If the source
 // buffer is larger than the current buffer has room for,
 // resize ourselves (which copies the data too) to make
 // enough room.
-// note that using this may have a performance hit due to
-// the resize operation, which has to copy data.
 Buffer &
 Buffer::operator+=(Buffer &buf)
 {
-    return operator+=(&buf);
-}
-
-Buffer &
-Buffer::operator+=(Buffer *buf)
-{
-//    GNASH_REPORT_FUNCTION;
-    size_t diff = 0;
-    if (buf->size() >= _nbytes) {
-       diff = _seekptr - _ptr;
-       resize(buf->size() + diff);
-    }
-    
-    if ((_seekptr + buf->size()) <= (_ptr + _nbytes)) {
-       std::copy(buf->begin(), buf->end(), _seekptr);
-       _seekptr += buf->size();
-    }
+// //    GNASH_REPORT_FUNCTION;
+    append(buf.reference(), buf.size());
     return *this;
 }
 
@@ -327,33 +336,44 @@
 Buffer::operator+=(char byte)
 {
 //    GNASH_REPORT_FUNCTION;
-    return operator+=(static_cast<Network::byte_t>(byte));
+    Network::byte_t nb = static_cast<Network::byte_t>(byte);
+    return operator+=(nb);
 }
 
 Buffer &
 Buffer::operator+=(Network::byte_t byte)
 {
 //    GNASH_REPORT_FUNCTION;
-    if ((_seekptr + 1) <= (_ptr + _nbytes)) {
+    if ((_seekptr + 1) <= (_data.get() + _nbytes)) {
        *_seekptr = byte;
        _seekptr += sizeof(char);
     }
     return *this;
 }
 
-// make ourselves be able to be copied.
 Buffer &
-Buffer::operator=(Buffer *buf)
+Buffer::operator+=(const std::string &str)
 {
 //    GNASH_REPORT_FUNCTION;
-    if (buf->size() > _nbytes) {
-         resize(buf->size());
-    }
+    Network::byte_t *ptr = const_cast<Network::byte_t 
*>(reinterpret_cast<const Network::byte_t *>(str.c_str()));
+    return append(ptr, str.size());
     
-    std::copy(buf->begin(), buf->end(), _ptr);
-    _seekptr += buf->size();
-
-    return *this;
+}
+
+Buffer &
+Buffer::operator+=(double num)
+{
+//    GNASH_REPORT_FUNCTION;
+    Network::byte_t *ptr = reinterpret_cast<Network::byte_t *>(&num);
+    return append(ptr, AMF0_NUMBER_SIZE);
+}
+
+Buffer &
+Buffer::operator+=(boost::uint16_t length)
+{
+//    GNASH_REPORT_FUNCTION;
+    Network::byte_t *ptr = reinterpret_cast<Network::byte_t *>(&length);
+    return append(ptr, sizeof(boost::uint16_t));
 }
 
 Buffer &
@@ -361,11 +381,54 @@
 {
 //    GNASH_REPORT_FUNCTION;
     if (buf.size() != _nbytes) {
-         resize(buf.size());
+       resize(buf.size());
+    }
+    copy(buf.reference(), buf.size());
+
+    return *this;
+}
+
+Buffer &
+Buffer::operator=(const std::string &str)
+{
+//    GNASH_REPORT_FUNCTION;
+    Network::byte_t *ptr = const_cast<Network::byte_t 
*>(reinterpret_cast<const Network::byte_t *>(str.c_str()));
+    return copy(ptr, str.size());
+}
+
+Buffer &
+Buffer::operator=(double num)
+{
+//    GNASH_REPORT_FUNCTION;
+    Network::byte_t *ptr = reinterpret_cast<Network::byte_t *>(&num);
+    return copy(ptr, AMF0_NUMBER_SIZE);
+}
+
+Buffer &
+Buffer::operator=(boost::uint16_t length)
+{
+//    GNASH_REPORT_FUNCTION;
+    Network::byte_t *ptr = reinterpret_cast<Network::byte_t *>(&length);
+    return copy(ptr, sizeof(boost::uint16_t));
+}
+
+Buffer &
+Buffer::operator=(gnash::Network::byte_t byte)
+{
+//    GNASH_REPORT_FUNCTION;
+    return copy(&byte, 1);
+}
+
+Buffer &
+Buffer::operator=(gnash::Network::byte_t *data)
+{
+//    GNASH_REPORT_FUNCTION;
+    if (data) {
+       _data.reset(data);
+    } else {
+       throw ParserException("Passing invalid pointer!");
     }
     
-    std::copy(buf.begin(), buf.end(), _ptr);
-
     return *this;
 }
 
@@ -376,7 +439,7 @@
 //    GNASH_REPORT_FUNCTION;
     Network::byte_t *bufptr = buf->reference();
     if (buf->size() == _nbytes) {
-        if (memcmp(bufptr, _ptr, _nbytes) == 0)  {
+        if (memcmp(bufptr, _data.get(), _nbytes) == 0)  {
             return true;
         }
     }
@@ -404,8 +467,8 @@
 {
 //    GNASH_REPORT_FUNCTION;
     for (size_t i=0; i< _nbytes; i++) {
-       if (memcmp((_ptr + i), b, size) == 0) {
-           return _ptr + i;
+       if (memcmp((_data.get() + i), b, size) == 0) {
+           return _data.get() + i;
        }
     }
     return 0;
@@ -416,8 +479,8 @@
 {
 //    GNASH_REPORT_FUNCTION;
     for (size_t i=0; i< _nbytes; i++) {
-       if (*(_ptr + i) == c) {
-           return _ptr + i;
+       if (*(_data.get() + i) == c) {
+           return _data.get() + i;
        }
     }
     return 0;
@@ -429,7 +492,7 @@
 //    GNASH_REPORT_FUNCTION;
     Network::byte_t *start = find(c);
 
-    log_debug("Byte is at %x", (void *)start);
+//    log_debug("Byte is at %x", (void *)start);
     
     if (start == 0) {
        return 0;
@@ -439,27 +502,27 @@
 //    *(end()) = 0;
     _nbytes--;
 
-    return _ptr;
+    return _data.get();
 }
 
 Network::byte_t *
 Buffer::remove(int start)
 {
 //    GNASH_REPORT_FUNCTION;
-    std::copy((_ptr + start + 1), end(), (_ptr + start)),
+    std::copy((_data.get() + start + 1), end(), (_data.get() + start)),
 //    *end() = 0;
     _nbytes--;
-    return _ptr;
+    return _data.get();
 }
 
 Network::byte_t *
 Buffer::remove(int start, int stop)
 {
 //    GNASH_REPORT_FUNCTION;
-    std::copy((_ptr + stop + 1), end(), (_ptr + start)),
+    std::copy((_data.get() + stop + 1), end(), (_data.get() + start)),
 //    *end() = 0;
     _nbytes -= stop-start;
-    return _ptr;
+    return _data.get();
 }
 
 // Just reset to having no data, but still having storage
@@ -467,68 +530,68 @@
 Buffer::clear()
 {
 //    GNASH_REPORT_FUNCTION;
-    if (_ptr) {
-        memset(_ptr, 0, _nbytes);
+    if (_data.get()) {
+        memset(_data.get(), 0, _nbytes);
     }
-    _seekptr = _ptr;
+    _seekptr = _data.get();
 }
 
 // Resize the buffer that holds the data.
-// Resize the buffer that holds the data.
-void *
+Buffer &
 Buffer::resize()
 {
 //    GNASH_REPORT_FUNCTION;
-    return resize(_seekptr - _ptr);
+    return resize(_seekptr - _data.get());
 }
 
-void *
+// Resize the block of memory used for this Buffer.
+Buffer &
 Buffer::resize(size_t size)
 {
 //    GNASH_REPORT_FUNCTION;
-    // Allocate a new memory block
+    boost::scoped_array<gnash::Network::byte_t> tmp;
+    
     if (_nbytes == 0) {
-       init(size);
+       return init(size);
     } else {
-       size_t diff =_seekptr - _ptr;
-       Network::byte_t *tmp = new Network::byte_t[size];
-       // The size is the same, don't do anything.
+       // Don't bother to resize without really changing anything
        if (size == _nbytes) {
-           return _ptr;
-       }
-       // And copy ourselves into it
-       if (size > _nbytes) {
-           std::copy(_ptr, _ptr + _nbytes, tmp);
-           // Delete the old block, it's unused now
-           delete[] _ptr;
-           // Make the memory block use the new space
-           _ptr = tmp;
-           // Make the seekptr point into the new space with the correct offset
-           _seekptr = tmp + diff;
-       }
-       if (size < _nbytes) {
-           std::copy(_ptr, _ptr + size, tmp);
-           // Delete the old block, it's unused now
-           delete[] _ptr;
-           // Make the memory block use the new space
-           _ptr = tmp;
-           // Make the seekptr point into the new space with the correct offset
-           _seekptr = _ptr + size;
-       }
+           return *this;
+       }
+
+       // Cache the number of bytes currently being held
+       size_t used = _seekptr - _data.get();
+       
+       // Copy the existing data into the new block of memory. The data
+       // held currently is moved to the temporary array, and then gets
+       // deleted when this method returns.
+       tmp.swap(_data);
+       
+       // We loose data if we resize smaller than the data currently held.
+       if (size < used) {
+           log_error("Truncating data (%d bytes) while resizing!", used - 
size);
+           used = size;
+       }
+       _data.reset(new Network::byte_t[size]);
+       std::copy(tmp.get(), tmp.get() + used, _data.get());
+       
+       // Make the seekptr point into the new space with the correct offset
+       _seekptr = _data.get() + used;
+
+       // Adjust the size
+       _nbytes = size;
     }
-    // Adjust the size
-    _nbytes = size;
     
-    return _ptr;
+    return *this;
 }
 
 void
 Buffer::dump()
 {
-    cerr << "Buffer is " << _nbytes << " bytes at " << (void *)_ptr << endl;
+    cerr << "Buffer is " << _nbytes << " bytes at " << (void *)_data.get() << 
endl;
     if (_nbytes < 0xffff) {
-       cerr << gnash::hexify((unsigned char *)_ptr, _nbytes, false) << endl;
-       cerr << gnash::hexify((unsigned char *)_ptr, _nbytes, true) << endl;
+       cerr << gnash::hexify((unsigned char *)_data.get(), _nbytes, false) << 
endl;
+       cerr << gnash::hexify((unsigned char *)_data.get(), _nbytes, true) << 
endl;
     } else {
        cerr << "ERROR: Buffer size out of range!" << endl;
        abort();

=== modified file 'libamf/buffer.h'
--- a/libamf/buffer.h   2008-07-27 17:15:53 +0000
+++ b/libamf/buffer.h   2008-09-19 22:11:12 +0000
@@ -20,6 +20,7 @@
 #define __BUFFER_H__ 1
 
 #include <boost/cstdint.hpp>
+#include <boost/scoped_array.hpp>
 #include <string>
 
 #include "getclocktime.hpp"
@@ -41,37 +42,33 @@
     
     // Delete the allocate memory
     ~Buffer();
+    // Clear the contents of the buffer by setting to zeros.
     void clear();
     bool empty() { return (_seekptr)?true:false; };
 
     // Resize the buffer that holds the data
-    void *resize();
-    void *resize(size_t nbytes);
+    Buffer &resize();
+    Buffer &resize(size_t nbytes);
 
     // Put data into the buffer. This overwrites all data, and resets the seek 
ptr.
-    void copy(gnash::Network::byte_t *data, size_t nbytes);
-    void copy(gnash::Network::byte_t *data) { copy(data, _nbytes); };
-    void copy(const std::string &str);
-    void copy(double num);
-    void copy(boost::uint16_t length);
-    void copy(gnash::Network::byte_t byte);
-//    void copy(bool);
-//     void copy(boost::uint32_t val);
-//     void copy(Element::amf_type_e type);
-
-    // Append data to the existing data in the buffer. This assume the
-    // buffer has been sized to hold the data as it is appended.
-    gnash::Network::byte_t *append(Buffer *buf);
-    gnash::Network::byte_t *append(Buffer &buf);
-    gnash::Network::byte_t *append(boost::uint32_t val);
-    gnash::Network::byte_t *append(bool);
-    gnash::Network::byte_t *append(double num);
-    gnash::Network::byte_t *append(Element::amf0_type_e type);
-    gnash::Network::byte_t *append(boost::uint16_t length);
-    gnash::Network::byte_t *append(gnash::Network::byte_t *data, size_t 
nbytes);
-    gnash::Network::byte_t *append(gnash::Network::byte_t byte);
-    gnash::Network::byte_t *append(const std::string &str);
-
+    Buffer &copy(gnash::Network::byte_t *data, size_t nbytes);
+    // Append data into an existing partially filled bufer
+    Buffer &append(gnash::Network::byte_t *data, size_t nbytes);
+
+    Buffer &operator=(Buffer &buf);
+    Buffer &operator=(const std::string &str);
+    Buffer &operator=(double num);
+    Buffer &operator=(boost::uint16_t length);
+    Buffer &operator=(gnash::Network::byte_t byte);
+    Buffer &operator=(gnash::Network::byte_t *data);    
+    
+    Buffer &operator+=(Buffer &buf);
+    Buffer &operator+=(const std::string &str);
+    Buffer &operator+=(double num);
+    Buffer &operator+=(boost::uint16_t length);
+    Buffer &operator+=(gnash::Network::byte_t byte);
+    Buffer &operator+=(char byte);
+    
     // Find a byte in the buffer
 //    Network::byte_t *find(char c);
     gnash::Network::byte_t *find(gnash::Network::byte_t b);
@@ -84,41 +81,39 @@
     gnash::Network::byte_t *remove(int x, int y);
     
     // Accessors
-    gnash::Network::byte_t *begin() { return _ptr ; };
-    gnash::Network::byte_t *end() { return _ptr + _nbytes; };
-    gnash::Network::byte_t *reference() { return _ptr; }
+    gnash::Network::byte_t *begin() { return _data.get() ; };
+    gnash::Network::byte_t *end() { return begin() + _nbytes; };
+    gnash::Network::byte_t *reference() { return _data.get(); }
     size_t size() { return _nbytes; }
     void setSize(size_t nbytes) { _nbytes = nbytes; };
     
-    // make ourselves be able to be copied.
-    Buffer &operator=(Buffer *buf);
-    Buffer &operator=(Buffer &buf);
-
     // Test against other buffers
     bool operator==(Buffer *buf);
     bool operator==(Buffer &buf);
-    Buffer &operator+=(Buffer *buf);
-    Buffer &operator+=(gnash::Network::byte_t byte);
-    Buffer &operator+=(char byte);
-    Buffer &operator+=(Buffer &buf);
-    gnash::Network::byte_t operator[](int x) { return *(_ptr + x); };
-    gnash::Network::byte_t *at(int x) { return _ptr + x; };
-//    Buffer *hex2mem(const char *str);
+
+    gnash::Network::byte_t operator[](int x) { return _data[x]; };
+    gnash::Network::byte_t *at(int x) { return _data.get() + x; };
+    Buffer *hex2mem(const char *str);
 
     // How much room is left in the buffer past the seek pointer. This is
     // primarily used to see if the buffer is full populated with data.
-    size_t spaceLeft() { return (_nbytes - (_seekptr - _ptr)); };
+    size_t spaceLeft() { return (_nbytes - (_seekptr - _data.get())); };
     
     // debug stuff, not need for running Cygnal
     void dump();
   protected:
-    void *init(size_t nbytes);
     gnash::Network::byte_t *_seekptr;
-    gnash::Network::byte_t *_ptr;
+//    gnash::Network::byte_t *_ptr;
+    boost::scoped_array<gnash::Network::byte_t> _data;
     size_t         _nbytes;
 #ifdef USE_STATS_BUFFERS
     struct timespec _stamp;    // used for timing how long data stays in the 
queue.
 #endif
+  private:
+    // Initialize a block of memory for this buffer
+    Buffer &init(size_t nbytes);
+    
+    gnash::Network::byte_t hex2digit (gnash::Network::byte_t digit);
 };
 
 

=== modified file 'testsuite/libamf.all/test_buffer.cpp'
--- a/testsuite/libamf.all/test_buffer.cpp      2008-04-30 02:35:30 +0000
+++ b/testsuite/libamf.all/test_buffer.cpp      2008-09-19 22:11:12 +0000
@@ -40,6 +40,7 @@
 #endif
 #include "buffer.h"
 #include "arg_parser.h"
+#include "GnashException.h"
 
 using namespace std;
 using namespace amf;
@@ -49,6 +50,7 @@
 static void usage();
 
 // Prototypes for test cases
+static void test_resize();
 static void test_construct();
 static void test_copy();
 static void test_find();
@@ -130,39 +132,6 @@
     }
 #endif
     
-    Buffer buf;
-#ifdef HAVE_MALLINFO
-    if (memdebug) {
-        mem->addStats(__LINE__);             // take a sample
-    }
-#endif    
-    if (buf.size() == gnash::NETBUFSIZE) {
-         runtest.pass ("Buffer::size()");
-     } else {
-         runtest.fail ("Buffer::size()");
-    }
-
-#ifdef HAVE_MALLINFO
-    if (memdebug) {
-        mem->addStats(__LINE__);             // take a sample
-    }
-#endif
-    buf.resize(112);
-#ifdef HAVE_MALLINFO
-    if (memdebug) {
-        mem->addStats(__LINE__);             // take a sample
-    }
-#endif
-    if (buf.size() == 112) {
-         runtest.pass ("Buffer::resize()");
-     } else {
-         runtest.fail ("Buffer::resize()");
-    }
-#ifdef HAVE_MALLINFO
-    if (memdebug) {
-        mem->addStats(__LINE__);             // take a sample
-    }
-#endif
 // these tests are bogus unless you have both mallinfo()
 // and also have memory statistics gathering turned on.
 #if defined(HAVE_MALLINFO) && defined(USE_STATS_MEMORY)
@@ -171,7 +140,8 @@
     // test destroying Buffers
     test_destruct();
 #endif
-    
+
+    test_resize();
     test_copy();
     test_find();
     test_append();
@@ -199,6 +169,53 @@
 }
 
 void
+test_resize()
+{
+    Buffer buf;
+#ifdef HAVE_MALLINFO
+    if (memdebug) {
+        mem->addStats(__LINE__);             // take a sample
+    }
+#endif    
+    if (buf.size() == gnash::NETBUFSIZE) {
+        runtest.pass ("Buffer::size(NETBUFSIZE)");
+    } else {
+        runtest.fail ("Buffer::size(NETBUFSIZE)");
+    }
+    
+#ifdef HAVE_MALLINFO
+    if (memdebug) {
+        mem->addStats(__LINE__);             // take a sample
+    }
+#endif
+    buf.resize(112);
+#ifdef HAVE_MALLINFO
+    if (memdebug) {
+        mem->addStats(__LINE__);             // take a sample
+    }
+#endif
+    if (buf.size() == 112) {
+         runtest.pass ("Buffer::resize(112)");
+     } else {
+         runtest.fail ("Buffer::resize(112)");
+    }
+#ifdef HAVE_MALLINFO
+    if (memdebug) {
+        mem->addStats(__LINE__);             // take a sample
+    }
+#endif
+
+    string str = "Hello World";
+    buf = str;
+    buf.resize(5);
+    if (memcmp(buf.begin(), str.c_str(), 5) == 0) {
+        runtest.pass ("Buffer resize(5)");
+    } else {
+        runtest.fail ("Buffer resize(5)");
+    }
+}
+
+void
 test_copy()
 {
     // Make some data for the buffers
@@ -222,7 +239,7 @@
 
     const char *str = "I'm bored";
     string str1 = str;
-    buf1.copy(str1);
+    buf1 = str1;
     if (memcmp(ptr1, str, 9) == 0) {
          runtest.pass ("Buffer::copy(std::string &)");
     } else {
@@ -230,7 +247,7 @@
     }
 
     Buffer buf2;
-    buf2.copy(str);
+    buf2 = str;
     Network::byte_t *ptr2 = buf2.reference();
     if (memcmp(ptr2, str, 9) == 0) {
          runtest.pass ("Buffer::copy(const char *)");
@@ -240,7 +257,7 @@
 
     boost::uint16_t length = 12;
     Buffer buf3;
-    buf3.copy(length);
+    buf3 = length;
     Network::byte_t *ptr3 = buf3.reference();
     boost::uint16_t newlen = *(reinterpret_cast<boost::uint16_t *>(ptr3));
     if (length == newlen) {
@@ -336,7 +353,7 @@
     buf2.clear();
     buf2.copy(data1, 10);
     Network::byte_t byte = '@';
-    buf2.append(byte);
+    buf2 += byte;
     memset(data3, 0, 20);
     memcpy(data3, data1, 10);
     *(data3 + 10) = '@';
@@ -351,7 +368,7 @@
     Buffer buf3;
     buf3.clear();
     buf3.copy(data1, 10);
-    buf3.append(num);
+    buf3 += num;
     
     memset(data3, 0, 20);
     memcpy(data3, data1, 10);
@@ -588,7 +605,7 @@
          runtest.fail ("Buffer::operator+=(char)");
     }
 
-    Buffer buf6(10);
+    Buffer buf6(6);
     buf6.clear();
     buf6 += 'D';
     buf6 += 'E';
@@ -596,13 +613,26 @@
     buf5 += buf6;
     ptr3 = buf5.reference();    // refresh the pointer, as it changes
                                 // on a resize()
-    // The size should now be the default 10, plus the 3 characters
-    // already added.
-    if ((memcmp(ptr3, "abcDEF", 6) == 0) && (buf5.size() == 13)) {
+    if ((memcmp(ptr3, "abcDEF", 6) == 0) && (buf5.size() == 10)) {
          runtest.pass ("Buffer::operator+=(Buffer &)");
     } else {
          runtest.fail ("Buffer::operator+=(Buffer &)");
     }
+
+    bool caught = false;
+    try {
+        buf5 += buf6;
+    }
+
+    catch (GnashException& ge) {
+        caught = true;
+//        log_debug("Got exeception from operator+=: %s", ge.what());
+    }
+    if (caught) {
+         runtest.pass ("Buffer::operator+=(Buffer &) error");
+    } else {
+         runtest.fail ("Buffer::operator+=(Buffer &) error");
+    }
 }
 
 static void


reply via email to

[Prev in Thread] Current Thread [Next in Thread]