Commit c064d341 authored by Reilly Grant's avatar Reilly Grant Committed by Commit Bot

Increase usability of RefCountedBytes as a mutable buffer

This patch adds a new constructor for RefCountedBytes that initializes
it with a given length of zeros and non-const versions of the front()
and front_as<>() methods to accompany the non-const data() method.

This is split out from https://crrev.com/c/786680 which uses this class
as a replacement for net::IOBufferWithSize (but with an unsigned
buffer).

Change-Id: I36c16d02e0a524da3da8679b883321388febaf82
Reviewed-on: https://chromium-review.googlesource.com/822165Reviewed-by: default avatarLei Zhang <thestig@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#524160}
parent 4280af78
...@@ -38,6 +38,8 @@ RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer) ...@@ -38,6 +38,8 @@ RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size) RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
: data_(p, p + size) {} : data_(p, p + size) {}
RefCountedBytes::RefCountedBytes(size_t size) : data_(size, 0) {}
scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector( scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector(
std::vector<unsigned char>* to_destroy) { std::vector<unsigned char>* to_destroy) {
scoped_refptr<RefCountedBytes> bytes(new RefCountedBytes); scoped_refptr<RefCountedBytes> bytes(new RefCountedBytes);
......
...@@ -78,6 +78,10 @@ class BASE_EXPORT RefCountedBytes : public RefCountedMemory { ...@@ -78,6 +78,10 @@ class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
// Constructs a RefCountedBytes object by copying |size| bytes from |p|. // Constructs a RefCountedBytes object by copying |size| bytes from |p|.
RefCountedBytes(const unsigned char* p, size_t size); RefCountedBytes(const unsigned char* p, size_t size);
// Constructs a RefCountedBytes object by zero-initializing a new vector of
// |size| bytes.
explicit RefCountedBytes(size_t size);
// Constructs a RefCountedBytes object by performing a swap. (To non // Constructs a RefCountedBytes object by performing a swap. (To non
// destructively build a RefCountedBytes, use the constructor that takes a // destructively build a RefCountedBytes, use the constructor that takes a
// vector.) // vector.)
...@@ -91,6 +95,14 @@ class BASE_EXPORT RefCountedBytes : public RefCountedMemory { ...@@ -91,6 +95,14 @@ class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
const std::vector<unsigned char>& data() const { return data_; } const std::vector<unsigned char>& data() const { return data_; }
std::vector<unsigned char>& data() { return data_; } std::vector<unsigned char>& data() { return data_; }
// Non-const versions of front() and front_as() that are simply shorthand for
// data().data().
unsigned char* front() { return data_.data(); }
template <typename T>
T* front_as() {
return reinterpret_cast<T*>(front());
}
private: private:
~RefCountedBytes() override; ~RefCountedBytes() override;
......
...@@ -6,8 +6,12 @@ ...@@ -6,8 +6,12 @@
#include <stdint.h> #include <stdint.h>
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
using testing::Each;
using testing::ElementsAre;
namespace base { namespace base {
TEST(RefCountedMemoryUnitTest, RefCountedStaticMemory) { TEST(RefCountedMemoryUnitTest, RefCountedStaticMemory) {
...@@ -41,6 +45,20 @@ TEST(RefCountedMemoryUnitTest, RefCountedBytes) { ...@@ -41,6 +45,20 @@ TEST(RefCountedMemoryUnitTest, RefCountedBytes) {
EXPECT_EQ(99U, mem2->front()[2]); EXPECT_EQ(99U, mem2->front()[2]);
} }
TEST(RefCountedMemoryUnitTest, RefCountedBytesMutable) {
auto mem = base::MakeRefCounted<RefCountedBytes>(10);
ASSERT_EQ(10U, mem->size());
EXPECT_THAT(mem->data(), Each(0U));
// Test non-const versions of data(), front() and front_as<>().
mem->data()[0] = 1;
mem->front()[1] = 2;
mem->front_as<char>()[2] = 3;
EXPECT_THAT(mem->data(), ElementsAre(1, 2, 3, 0, 0, 0, 0, 0, 0, 0));
}
TEST(RefCountedMemoryUnitTest, RefCountedString) { TEST(RefCountedMemoryUnitTest, RefCountedString) {
std::string s("destroy me"); std::string s("destroy me");
scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s); scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s);
......
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