Commit 4c0e4cbf authored by Han Leon's avatar Han Leon Committed by Commit Bot

[DeviceService] Update device::SendBuffer to avoid unnecessary vector copy

- Makes device::SendBuffer ctor take 'const std::vector<uint8_t>&'
  rather than 'const std::vector<char>&'
- Changes type of its member |data_| from 'const std::vector<char>' to
  'const std::vector<uint8_t>'.

BUG=749515
TBR=charliea@chromium.org

Change-Id: I27424642d866174be84f7916f260b1f5e6b769a8
Reviewed-on: https://chromium-review.googlesource.com/620207
Commit-Queue: Han Leon <leon.han@intel.com>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#496571}
parent a40ace7e
...@@ -14,13 +14,13 @@ ReadOnlyBuffer::~ReadOnlyBuffer() { ...@@ -14,13 +14,13 @@ ReadOnlyBuffer::~ReadOnlyBuffer() {
WritableBuffer::~WritableBuffer() { WritableBuffer::~WritableBuffer() {
} }
SendBuffer::SendBuffer(const std::vector<char>& data, SendBuffer::SendBuffer(const std::vector<uint8_t>& data,
SendCompleteCallback callback) SendCompleteCallback callback)
: data_(data), callback_(std::move(callback)) {} : data_(data), callback_(std::move(callback)) {}
SendBuffer::~SendBuffer() {} SendBuffer::~SendBuffer() {}
const char* SendBuffer::GetData() { const uint8_t* SendBuffer::GetData() {
return data_.data(); return data_.data();
} }
......
...@@ -20,7 +20,7 @@ namespace device { ...@@ -20,7 +20,7 @@ namespace device {
class ReadOnlyBuffer { class ReadOnlyBuffer {
public: public:
virtual ~ReadOnlyBuffer(); virtual ~ReadOnlyBuffer();
virtual const char* GetData() = 0; virtual const uint8_t* GetData() = 0;
virtual uint32_t GetSize() = 0; virtual uint32_t GetSize() = 0;
virtual void Done(uint32_t bytes_read) = 0; virtual void Done(uint32_t bytes_read) = 0;
virtual void DoneWithError(uint32_t bytes_read, int32_t error) = 0; virtual void DoneWithError(uint32_t bytes_read, int32_t error) = 0;
...@@ -45,16 +45,16 @@ class SendBuffer : public device::ReadOnlyBuffer { ...@@ -45,16 +45,16 @@ class SendBuffer : public device::ReadOnlyBuffer {
public: public:
using SendCompleteCallback = using SendCompleteCallback =
base::OnceCallback<void(int, device::mojom::SerialSendError)>; base::OnceCallback<void(int, device::mojom::SerialSendError)>;
SendBuffer(const std::vector<char>& data, SendCompleteCallback callback); SendBuffer(const std::vector<uint8_t>& data, SendCompleteCallback callback);
~SendBuffer() override; ~SendBuffer() override;
const char* GetData() override; const uint8_t* GetData() override;
uint32_t GetSize() override; uint32_t GetSize() override;
void Done(uint32_t bytes_read) override; void Done(uint32_t bytes_read) override;
void DoneWithError(uint32_t bytes_read, int32_t error) override; void DoneWithError(uint32_t bytes_read, int32_t error) override;
private: private:
const std::vector<char> data_; const std::vector<uint8_t> data_;
SendCompleteCallback callback_; SendCompleteCallback callback_;
}; };
......
...@@ -176,7 +176,7 @@ class SerialIoHandler : public base::RefCountedThreadSafe<SerialIoHandler> { ...@@ -176,7 +176,7 @@ class SerialIoHandler : public base::RefCountedThreadSafe<SerialIoHandler> {
bool read_canceled() const { return read_canceled_; } bool read_canceled() const { return read_canceled_; }
const char* pending_write_buffer() const { const uint8_t* pending_write_buffer() const {
return pending_write_buffer_ ? pending_write_buffer_->GetData() : NULL; return pending_write_buffer_ ? pending_write_buffer_->GetData() : NULL;
} }
......
...@@ -42,8 +42,8 @@ void TestSerialIoHandler::ReadImpl() { ...@@ -42,8 +42,8 @@ void TestSerialIoHandler::ReadImpl() {
size_t num_bytes = size_t num_bytes =
std::min(buffer_.size(), static_cast<size_t>(pending_read_buffer_len())); std::min(buffer_.size(), static_cast<size_t>(pending_read_buffer_len()));
memcpy(pending_read_buffer(), buffer_.c_str(), num_bytes); memcpy(pending_read_buffer(), buffer_.data(), num_bytes);
buffer_ = buffer_.substr(num_bytes); buffer_.erase(buffer_.begin(), buffer_.begin() + num_bytes);
ReadCompleted(static_cast<uint32_t>(num_bytes), ReadCompleted(static_cast<uint32_t>(num_bytes),
mojom::SerialReceiveError::NONE); mojom::SerialReceiveError::NONE);
} }
...@@ -57,7 +57,8 @@ void TestSerialIoHandler::WriteImpl() { ...@@ -57,7 +57,8 @@ void TestSerialIoHandler::WriteImpl() {
std::move(send_callback_).Run(); std::move(send_callback_).Run();
return; return;
} }
buffer_ += std::string(pending_write_buffer(), pending_write_buffer_len()); buffer_.insert(buffer_.end(), pending_write_buffer(),
pending_write_buffer() + pending_write_buffer_len());
WriteCompleted(pending_write_buffer_len(), mojom::SerialSendError::NONE); WriteCompleted(pending_write_buffer_len(), mojom::SerialSendError::NONE);
if (pending_read_buffer()) if (pending_read_buffer())
ReadImpl(); ReadImpl();
......
...@@ -60,7 +60,7 @@ class TestSerialIoHandler : public SerialIoHandler { ...@@ -60,7 +60,7 @@ class TestSerialIoHandler : public SerialIoHandler {
bool dtr_; bool dtr_;
bool rts_; bool rts_;
mutable int flushes_; mutable int flushes_;
std::string buffer_; std::vector<uint8_t> buffer_;
base::OnceClosure send_callback_; base::OnceClosure send_callback_;
DISALLOW_COPY_AND_ASSIGN(TestSerialIoHandler); DISALLOW_COPY_AND_ASSIGN(TestSerialIoHandler);
......
...@@ -88,8 +88,7 @@ class FakeSerialIoHandler : public device::mojom::SerialIoHandler { ...@@ -88,8 +88,7 @@ class FakeSerialIoHandler : public device::mojom::SerialIoHandler {
void Write(const std::vector<uint8_t>& data, void Write(const std::vector<uint8_t>& data,
WriteCallback callback) override { WriteCallback callback) override {
test_io_handler_->Write(base::MakeUnique<device::SendBuffer>( test_io_handler_->Write(base::MakeUnique<device::SendBuffer>(
std::vector<char>(data.data(), data.data() + data.size()), data, base::BindOnce(
base::BindOnce(
[](WriteCallback callback, int bytes_sent, [](WriteCallback callback, int bytes_sent,
device::mojom::SerialSendError error) { device::mojom::SerialSendError error) {
std::move(callback).Run(bytes_sent, error); std::move(callback).Run(bytes_sent, error);
......
...@@ -50,8 +50,7 @@ void SerialIoHandlerImpl::Read(uint32_t bytes, ReadCallback callback) { ...@@ -50,8 +50,7 @@ void SerialIoHandlerImpl::Read(uint32_t bytes, ReadCallback callback) {
void SerialIoHandlerImpl::Write(const std::vector<uint8_t>& data, void SerialIoHandlerImpl::Write(const std::vector<uint8_t>& data,
WriteCallback callback) { WriteCallback callback) {
io_handler_->Write(base::MakeUnique<SendBuffer>( io_handler_->Write(base::MakeUnique<SendBuffer>(
std::vector<char>(data.data(), data.data() + data.size()), data, base::BindOnce(
base::BindOnce(
[](WriteCallback callback, int bytes_sent, [](WriteCallback callback, int bytes_sent,
mojom::SerialSendError error) { mojom::SerialSendError error) {
std::move(callback).Run(bytes_sent, error); std::move(callback).Run(bytes_sent, error);
......
...@@ -122,11 +122,11 @@ void BattOrConnectionImpl::Close() { ...@@ -122,11 +122,11 @@ void BattOrConnectionImpl::Close() {
void BattOrConnectionImpl::SendBytes(BattOrMessageType type, void BattOrConnectionImpl::SendBytes(BattOrMessageType type,
const void* buffer, const void* buffer,
size_t bytes_to_send) { size_t bytes_to_send) {
const char* bytes = reinterpret_cast<const char*>(buffer); const uint8_t* bytes = reinterpret_cast<const uint8_t*>(buffer);
// Reserve a send buffer with enough extra bytes for the start, type, end, and // Reserve a send buffer with enough extra bytes for the start, type, end, and
// escape bytes. // escape bytes.
vector<char> data; vector<uint8_t> data;
data.reserve(2 * bytes_to_send + 3); data.reserve(2 * bytes_to_send + 3);
data.push_back(BATTOR_CONTROL_BYTE_START); data.push_back(BATTOR_CONTROL_BYTE_START);
...@@ -143,7 +143,7 @@ void BattOrConnectionImpl::SendBytes(BattOrMessageType type, ...@@ -143,7 +143,7 @@ void BattOrConnectionImpl::SendBytes(BattOrMessageType type,
data.push_back(BATTOR_CONTROL_BYTE_END); data.push_back(BATTOR_CONTROL_BYTE_END);
LogSerial(StringPrintf("Bytes sent: %s.", CharVectorToString(data).c_str())); LogSerial(StringPrintf("Bytes sent: %s.", ByteVectorToString(data).c_str()));
pending_write_length_ = data.size(); pending_write_length_ = data.size();
io_handler_->Write(base::MakeUnique<device::SendBuffer>( io_handler_->Write(base::MakeUnique<device::SendBuffer>(
......
...@@ -96,9 +96,9 @@ class BattOrConnectionImplTest : public testing::Test, ...@@ -96,9 +96,9 @@ class BattOrConnectionImplTest : public testing::Test,
// Writes the specified bytes directly to the serial connection. // Writes the specified bytes directly to the serial connection.
void SendBytesRaw(const char* data, uint16_t bytes_to_send) { void SendBytesRaw(const char* data, uint16_t bytes_to_send) {
std::vector<char> data_vector(data, data + bytes_to_send);
connection_->GetIoHandler()->Write(base::MakeUnique<device::SendBuffer>( connection_->GetIoHandler()->Write(base::MakeUnique<device::SendBuffer>(
data_vector, base::BindOnce(&NullWriteCallback))); std::vector<uint8_t>(data, data + bytes_to_send),
base::BindOnce(&NullWriteCallback)));
task_runner_->RunUntilIdle(); task_runner_->RunUntilIdle();
} }
......
...@@ -6,13 +6,13 @@ ...@@ -6,13 +6,13 @@
namespace battor { namespace battor {
std::string CharVectorToString(const std::vector<char> data) { std::string ByteVectorToString(const std::vector<uint8_t>& data) {
std::string s; std::string s;
// Reserve enough bytes for '0x', the two data characters, a space, and a null // Reserve enough bytes for '0x', the two data characters, a space, and a null
// terminating byte. // terminating byte.
char num_buff[6]; char num_buff[6];
for (char d : data) { for (uint8_t d : data) {
// We use sprintf because stringstream's hex support wants to print our // We use sprintf because stringstream's hex support wants to print our
// characters as signed. // characters as signed.
sprintf(num_buff, "0x%02hhx ", d); sprintf(num_buff, "0x%02hhx ", d);
...@@ -23,7 +23,7 @@ std::string CharVectorToString(const std::vector<char> data) { ...@@ -23,7 +23,7 @@ std::string CharVectorToString(const std::vector<char> data) {
} }
std::string CharArrayToString(const char* bytes, size_t len) { std::string CharArrayToString(const char* bytes, size_t len) {
return CharVectorToString(std::vector<char>(bytes, bytes + len)); return ByteVectorToString(std::vector<uint8_t>(bytes, bytes + len));
} }
} // namespace battor } // namespace battor
...@@ -13,7 +13,7 @@ namespace battor { ...@@ -13,7 +13,7 @@ namespace battor {
// Prints |bytes| as a space separated list of hex numbers (e.g. {'A', 'J'} // Prints |bytes| as a space separated list of hex numbers (e.g. {'A', 'J'}
// would return "0x41 0x4A"). // would return "0x41 0x4A").
std::string CharArrayToString(const char* bytes, size_t len); std::string CharArrayToString(const char* bytes, size_t len);
std::string CharVectorToString(const std::vector<char> bytes); std::string ByteVectorToString(const std::vector<uint8_t>& bytes);
} // namespace battor } // namespace battor
......
...@@ -10,17 +10,16 @@ using namespace testing; ...@@ -10,17 +10,16 @@ using namespace testing;
namespace battor { namespace battor {
TEST(SerialUtilsTest, CharVectorToStringLengthZero) { TEST(SerialUtilsTest, ByteVectorToStringLengthZero) {
EXPECT_EQ("", CharVectorToString(std::vector<char>())); EXPECT_EQ("", ByteVectorToString(std::vector<uint8_t>()));
} }
TEST(SerialUtilsTest, CharVectorToStringLengthOne) { TEST(SerialUtilsTest, ByteVectorToStringLengthOne) {
EXPECT_EQ("0x41", CharVectorToString(std::vector<char>({'A'}))); EXPECT_EQ("0x41", ByteVectorToString(std::vector<uint8_t>({'A'})));
} }
TEST(SerialUtilsTest, CharVectorToStringLengthTwo) { TEST(SerialUtilsTest, ByteVectorToStringLengthTwo) {
EXPECT_EQ("0x41 0x4a", EXPECT_EQ("0x41 0x4a", ByteVectorToString(std::vector<uint8_t>({'A', 'J'})));
CharVectorToString(std::vector<char>({'A', 'J'})));
} }
TEST(SerialUtilsTest, CharArrayToStringLengthOne) { TEST(SerialUtilsTest, CharArrayToStringLengthOne) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment