Skip to content

Commit 08142bf

Browse files
committed
ARROW-3250: [C++] Buffer implementation which owns memory from a std::string
Author: Wes McKinney <wesm+git@apache.org> Author: Atri Sharma <atri@linux.com> Closes apache#2660 from atris/stlstring_buffer and squashes the following commits: 60a10d5 <Wes McKinney> Initiative differently to avoid SSO issues dafb078 <Wes McKinney> Move implementation to Buffer::FromString with rvalue-reference ea342b8 <Atri Sharma> ARROW-3250: Buffer implementation to own memory from a std::string
1 parent acb0b13 commit 08142bf

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

cpp/src/arrow/buffer-test.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ TEST(TestMutableBuffer, Wrap) {
141141
ASSERT_EQ(4, values[1]);
142142
}
143143

144+
TEST(TestBuffer, FromStringRvalue) {
145+
std::string expected = "input data";
146+
147+
std::shared_ptr<Buffer> buffer;
148+
{
149+
std::string data_str = "input data";
150+
buffer = Buffer::FromString(std::move(data_str));
151+
}
152+
153+
ASSERT_FALSE(buffer->is_mutable());
154+
155+
ASSERT_EQ(0, memcmp(buffer->data(), expected.c_str(), expected.size()));
156+
ASSERT_EQ(static_cast<int64_t>(expected.size()), buffer->size());
157+
}
158+
144159
TEST(TestBuffer, SliceMutableBuffer) {
145160
std::string data_str = "some data to slice";
146161
auto data = reinterpret_cast<const uint8_t*>(data_str.c_str());

cpp/src/arrow/buffer.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,22 @@ Status Buffer::FromString(const std::string& data, std::shared_ptr<Buffer>* out)
7171
return FromString(data, default_memory_pool(), out);
7272
}
7373

74+
class StlStringBuffer : public Buffer {
75+
public:
76+
explicit StlStringBuffer(std::string&& data) : Buffer(nullptr, 0), input_(data) {
77+
data_ = reinterpret_cast<const uint8_t*>(input_.c_str());
78+
size_ = static_cast<int64_t>(input_.size());
79+
capacity_ = size_;
80+
}
81+
82+
private:
83+
std::string input_;
84+
};
85+
86+
std::shared_ptr<Buffer> Buffer::FromString(std::string&& data) {
87+
return std::make_shared<StlStringBuffer>(std::move(data));
88+
}
89+
7490
std::string Buffer::ToString() const {
7591
return std::string(reinterpret_cast<const char*>(data_), static_cast<size_t>(size_));
7692
}

cpp/src/arrow/buffer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ class ARROW_EXPORT Buffer {
124124
/// using the default memory pool
125125
static Status FromString(const std::string& data, std::shared_ptr<Buffer>* out);
126126

127+
/// \brief Construct an immutable buffer that takes ownership of the contents
128+
/// of an std::string
129+
/// \param[in] data an rvalue-reference of a string
130+
/// \return a new Buffer instance
131+
static std::shared_ptr<Buffer> FromString(std::string&& data);
132+
127133
/// \brief Create buffer referencing typed memory with some length without
128134
/// copying
129135
/// \param[in] data the typed memory as C array

0 commit comments

Comments
 (0)