From e6d52ecfa2379b1f7f3007e65ded5e4076abc5e9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 8 Jan 2010 17:01:08 +0200 Subject: Add support in buffer class for reusing existing buffer without freeing it --- documentation/cxx/tree/manual/index.xhtml | 5 +++-- libxsd/xsd/cxx/tree/buffer.hxx | 9 +++++---- libxsd/xsd/cxx/tree/buffer.txx | 24 +++++++++--------------- libxsd/xsd/cxx/tree/types.hxx | 8 ++++---- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/documentation/cxx/tree/manual/index.xhtml b/documentation/cxx/tree/manual/index.xhtml index a3e4c44..f1696c0 100644 --- a/documentation/cxx/tree/manual/index.xhtml +++ b/documentation/cxx/tree/manual/index.xhtml @@ -1511,8 +1511,9 @@ public: }; -

If the assume_ownership argument to the constructor - is true, the instance assumes ownership of the +

The last overloaded constructor reuses an existing data buffer instead + of making a copy. If the assume_ownership argument is + true, the instance assumes ownership of the memory block pointed to by the data argument and will eventually release it by calling operator delete. The capacity and size modifier functions return diff --git a/libxsd/xsd/cxx/tree/buffer.hxx b/libxsd/xsd/cxx/tree/buffer.hxx index 45a1a82..24da287 100644 --- a/libxsd/xsd/cxx/tree/buffer.hxx +++ b/libxsd/xsd/cxx/tree/buffer.hxx @@ -45,12 +45,12 @@ namespace xsd virtual ~buffer_base () { - if (data_) + if (free_ && data_) operator delete (data_); } buffer_base () - : data_ (0), size_ (0), capacity_ (0) + : data_ (0), size_ (0), capacity_ (0), free_ (true) { } @@ -58,6 +58,7 @@ namespace xsd char* data_; size_t size_; size_t capacity_; + bool free_; }; //@endcond @@ -138,13 +139,13 @@ namespace xsd buffer (const void* data, size_t size, size_t capacity); /** - * @brief Assume ownership of the specified %buffer. + * @brief Reuse an existing %buffer. * * If the @a assume_ownership argument is true, the %buffer will * assume ownership of @a data and will release the memory * by calling @c operator @c delete(). * - * @param data A %buffer to assume ownership of. + * @param data A %buffer to reuse. * @param size A %buffer size in bytes. * @param capacity A %buffer capacity in bytes. * @param assume_ownership A boolean value indication whether to diff --git a/libxsd/xsd/cxx/tree/buffer.txx b/libxsd/xsd/cxx/tree/buffer.txx index ab11f40..f88f140 100644 --- a/libxsd/xsd/cxx/tree/buffer.txx +++ b/libxsd/xsd/cxx/tree/buffer.txx @@ -60,20 +60,10 @@ namespace xsd if (size > capacity) throw bounds (); - if (own) - { - data_ = reinterpret_cast (data); - size_ = size; - capacity_ = capacity; - } - else - { - this->capacity (capacity); - size_ = size; - - if (size_) - std::memcpy (data_, data, size_); - } + data_ = reinterpret_cast (data); + size_ = size; + capacity_ = capacity; + free_ = own; } template @@ -111,14 +101,17 @@ namespace xsd char* tmp_data (data_); size_t tmp_size (size_); size_t tmp_capacity (capacity_); + bool tmp_free (free_); data_ = other.data_; size_ = other.size_; capacity_ = other.capacity_; + free_ = other.free_; other.data_ = tmp_data; other.size_ = tmp_size; other.capacity_ = tmp_capacity; + other.free_ = tmp_free; } template @@ -139,11 +132,12 @@ namespace xsd if (copy && size_ > 0) std::memcpy (data, data_, size_); - if (data_) + if (free_ && data_) operator delete (data_); data_ = data; capacity_ = capacity; + free_ = true; return true; } diff --git a/libxsd/xsd/cxx/tree/types.hxx b/libxsd/xsd/cxx/tree/types.hxx index 2ef5c09..e6dc5a4 100644 --- a/libxsd/xsd/cxx/tree/types.hxx +++ b/libxsd/xsd/cxx/tree/types.hxx @@ -3098,13 +3098,13 @@ namespace xsd base64_binary (const void* data, size_t size, size_t capacity); /** - * @brief Assume ownership of the specified %buffer. + * @brief Reuse an existing %buffer. * * If the @a assume_ownership argument is true, the %buffer will * assume ownership of @a data and will release the memory * by calling @c operator @c delete(). * - * @param data A %buffer to assume ownership of. + * @param data A %buffer to reuse. * @param size A %buffer size in bytes. * @param capacity A %buffer capacity in bytes. * @param assume_ownership A boolean value indication whether to @@ -3294,13 +3294,13 @@ namespace xsd hex_binary (const void* data, size_t size, size_t capacity); /** - * @brief Assume ownership of the specified %buffer. + * @brief Reuse an existing %buffer.. * * If the @a assume_ownership argument is true, the %buffer will * assume ownership of @a data and will release the memory * by calling @c operator @c delete(). * - * @param data A %buffer to assume ownership of. + * @param data A %buffer to reuse. * @param size A %buffer size in bytes. * @param capacity A %buffer capacity in bytes. * @param assume_ownership A boolean value indication whether to -- cgit v1.1