This is an automated email from the ASF dual-hosted git repository.

wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new 08142bf  ARROW-3250: [C++] Buffer implementation which owns memory 
from a std::string
08142bf is described below

commit 08142bfcfbb485a1400deed093117b87afa45725
Author: Wes McKinney <[email protected]>
AuthorDate: Mon Oct 1 04:09:10 2018 -0400

    ARROW-3250: [C++] Buffer implementation which owns memory from a std::string
    
    Author: Wes McKinney <[email protected]>
    Author: Atri Sharma <[email protected]>
    
    Closes #2660 from atris/stlstring_buffer and squashes the following commits:
    
    60a10d553 <Wes McKinney> Initiative differently to avoid SSO issues
    dafb078ef <Wes McKinney> Move implementation to Buffer::FromString with 
rvalue-reference
    ea342b8da <Atri Sharma> ARROW-3250: Buffer implementation to own memory 
from a std::string
---
 cpp/src/arrow/buffer-test.cc | 15 +++++++++++++++
 cpp/src/arrow/buffer.cc      | 16 ++++++++++++++++
 cpp/src/arrow/buffer.h       |  6 ++++++
 3 files changed, 37 insertions(+)

diff --git a/cpp/src/arrow/buffer-test.cc b/cpp/src/arrow/buffer-test.cc
index b664250..6ee6076 100644
--- a/cpp/src/arrow/buffer-test.cc
+++ b/cpp/src/arrow/buffer-test.cc
@@ -141,6 +141,21 @@ TEST(TestMutableBuffer, Wrap) {
   ASSERT_EQ(4, values[1]);
 }
 
+TEST(TestBuffer, FromStringRvalue) {
+  std::string expected = "input data";
+
+  std::shared_ptr<Buffer> buffer;
+  {
+    std::string data_str = "input data";
+    buffer = Buffer::FromString(std::move(data_str));
+  }
+
+  ASSERT_FALSE(buffer->is_mutable());
+
+  ASSERT_EQ(0, memcmp(buffer->data(), expected.c_str(), expected.size()));
+  ASSERT_EQ(static_cast<int64_t>(expected.size()), buffer->size());
+}
+
 TEST(TestBuffer, SliceMutableBuffer) {
   std::string data_str = "some data to slice";
   auto data = reinterpret_cast<const uint8_t*>(data_str.c_str());
diff --git a/cpp/src/arrow/buffer.cc b/cpp/src/arrow/buffer.cc
index 006fe0f..01bb0c3 100644
--- a/cpp/src/arrow/buffer.cc
+++ b/cpp/src/arrow/buffer.cc
@@ -71,6 +71,22 @@ Status Buffer::FromString(const std::string& data, 
std::shared_ptr<Buffer>* out)
   return FromString(data, default_memory_pool(), out);
 }
 
+class StlStringBuffer : public Buffer {
+ public:
+  explicit StlStringBuffer(std::string&& data) : Buffer(nullptr, 0), 
input_(data) {
+    data_ = reinterpret_cast<const uint8_t*>(input_.c_str());
+    size_ = static_cast<int64_t>(input_.size());
+    capacity_ = size_;
+  }
+
+ private:
+  std::string input_;
+};
+
+std::shared_ptr<Buffer> Buffer::FromString(std::string&& data) {
+  return std::make_shared<StlStringBuffer>(std::move(data));
+}
+
 std::string Buffer::ToString() const {
   return std::string(reinterpret_cast<const char*>(data_), 
static_cast<size_t>(size_));
 }
diff --git a/cpp/src/arrow/buffer.h b/cpp/src/arrow/buffer.h
index 75d0c21..d37a336 100644
--- a/cpp/src/arrow/buffer.h
+++ b/cpp/src/arrow/buffer.h
@@ -124,6 +124,12 @@ class ARROW_EXPORT Buffer {
   /// using the default memory pool
   static Status FromString(const std::string& data, std::shared_ptr<Buffer>* 
out);
 
+  /// \brief Construct an immutable buffer that takes ownership of the contents
+  /// of an std::string
+  /// \param[in] data an rvalue-reference of a string
+  /// \return a new Buffer instance
+  static std::shared_ptr<Buffer> FromString(std::string&& data);
+
   /// \brief Create buffer referencing typed memory with some length without
   /// copying
   /// \param[in] data the typed memory as C array

Reply via email to