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 2d649f9 ARROW-1623: [C++] Add convenience method to construct Buffer
from a string that owns its memory
2d649f9 is described below
commit 2d649f9a75250b5a908369ed0246218712830fc4
Author: Panchen Xue <[email protected]>
AuthorDate: Thu Feb 1 12:14:48 2018 -0500
ARROW-1623: [C++] Add convenience method to construct Buffer from a string
that owns its memory
Add static member function Buffer::FromString to create a new buffer that
owns its memory from given std::string. The memory is allocated from a given
memory pool or the default one if not specified.
Author: Panchen Xue <[email protected]>
Author: Wes McKinney <[email protected]>
Closes #1518 from xuepanchen/ARROW-1623 and squashes the following commits:
e6f7355f [Wes McKinney] clang-format
ce18950a [Panchen Xue] Add Buffer::FromString method that takes default
memory pool and modify test cast
f2c5e3ea [Panchen Xue] ARROW-1623: [C++] Add test case for
Buffer::FromString method
2385637c [Panchen Xue] ARROW-1623: [C++] Add Buffer::FromString to
construct a buffer that owns its memory from a std::string
---
cpp/src/arrow/buffer-test.cc | 17 +++++++++++++++++
cpp/src/arrow/buffer.cc | 12 ++++++++++++
cpp/src/arrow/buffer.h | 14 ++++++++++++++
3 files changed, 43 insertions(+)
diff --git a/cpp/src/arrow/buffer-test.cc b/cpp/src/arrow/buffer-test.cc
index 398cc06..a24384a 100644
--- a/cpp/src/arrow/buffer-test.cc
+++ b/cpp/src/arrow/buffer-test.cc
@@ -52,6 +52,23 @@ TEST(TestBuffer, FromStdString) {
ASSERT_EQ(static_cast<int64_t>(val.size()), buf.size());
}
+TEST(TestBuffer, FromStdStringWithMemory) {
+ std::string expected = "hello, world";
+ std::shared_ptr<Buffer> buf;
+
+ {
+ std::string temp = "hello, world";
+ ASSERT_OK(Buffer::FromString(temp, &buf));
+ ASSERT_EQ(0, memcmp(buf->data(), temp.c_str(), temp.size()));
+ ASSERT_EQ(static_cast<int64_t>(temp.size()), buf->size());
+ }
+
+ // Now temp goes out of scope and we check if created buffer
+ // is still valid to make sure it actually owns its space
+ ASSERT_EQ(0, memcmp(buf->data(), expected.c_str(), expected.size()));
+ ASSERT_EQ(static_cast<int64_t>(expected.size()), buf->size());
+}
+
TEST(TestBuffer, Resize) {
PoolBuffer buf;
diff --git a/cpp/src/arrow/buffer.cc b/cpp/src/arrow/buffer.cc
index 1b8e437..29e2c24 100644
--- a/cpp/src/arrow/buffer.cc
+++ b/cpp/src/arrow/buffer.cc
@@ -58,6 +58,18 @@ bool Buffer::Equals(const Buffer& other) const {
!memcmp(data_, other.data_,
static_cast<size_t>(size_))));
}
+Status Buffer::FromString(const std::string& data, MemoryPool* pool,
+ std::shared_ptr<Buffer>* out) {
+ auto size = static_cast<int64_t>(data.size());
+ RETURN_NOT_OK(AllocateBuffer(pool, size, out));
+ std::copy(data.c_str(), data.c_str() + size, (*out)->mutable_data());
+ return Status::OK();
+}
+
+Status Buffer::FromString(const std::string& data, std::shared_ptr<Buffer>*
out) {
+ return FromString(data, default_memory_pool(), out);
+}
+
PoolBuffer::PoolBuffer(MemoryPool* pool) : ResizableBuffer(nullptr, 0) {
if (pool == nullptr) {
pool = default_memory_pool();
diff --git a/cpp/src/arrow/buffer.h b/cpp/src/arrow/buffer.h
index 44c352a..d12eeb4 100644
--- a/cpp/src/arrow/buffer.h
+++ b/cpp/src/arrow/buffer.h
@@ -97,6 +97,20 @@ class ARROW_EXPORT Buffer {
Status Copy(const int64_t start, const int64_t nbytes,
std::shared_ptr<Buffer>* out) const;
+ /// \brief Construct a new buffer that owns its memory from a std::string
+ ///
+ /// \param[in] data a std::string object
+ /// \param[in] pool a memory pool
+ /// \param[out] out the created buffer
+ ///
+ /// \return Status message
+ static Status FromString(const std::string& data, MemoryPool* pool,
+ std::shared_ptr<Buffer>* out);
+
+ /// \brief Construct a new buffer that owns its memory from a std::string
+ /// using the default memory pool
+ static Status FromString(const std::string& data, std::shared_ptr<Buffer>*
out);
+
int64_t capacity() const { return capacity_; }
const uint8_t* data() const { return data_; }
uint8_t* mutable_data() { return mutable_data_; }
--
To stop receiving notification emails like this one, please contact
[email protected].