summaryrefslogtreecommitdiffstats
path: root/base/memory
diff options
context:
space:
mode:
authorscottmg@chromium.org <scottmg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-30 20:30:18 +0000
committerscottmg@chromium.org <scottmg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-04-30 20:30:18 +0000
commit6a497d7a743cd99e675d3f40606690d961df145a (patch)
tree5407b203c1c15eb2de79fc635a55eb0592e312b5 /base/memory
parent48a9ad9dc7be28316b777cb9f8525d6b664cfe3b (diff)
downloadchromium_src-6a497d7a743cd99e675d3f40606690d961df145a.zip
chromium_src-6a497d7a743cd99e675d3f40606690d961df145a.tar.gz
chromium_src-6a497d7a743cd99e675d3f40606690d961df145a.tar.bz2
Add pointer+size ctor to RefCountedBytes
Split out from https://codereview.chromium.org/255543006/ . R=brettw@chromium.org BUG=170859 Review URL: https://codereview.chromium.org/264563009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267321 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/memory')
-rw-r--r--base/memory/ref_counted_memory.cc3
-rw-r--r--base/memory/ref_counted_memory.h6
-rw-r--r--base/memory/ref_counted_memory_unittest.cc10
3 files changed, 17 insertions, 2 deletions
diff --git a/base/memory/ref_counted_memory.cc b/base/memory/ref_counted_memory.cc
index b1deee1..477c941 100644
--- a/base/memory/ref_counted_memory.cc
+++ b/base/memory/ref_counted_memory.cc
@@ -37,6 +37,9 @@ RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
: data_(initializer) {
}
+RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
+ : data_(p, p + size) {}
+
RefCountedBytes* RefCountedBytes::TakeVector(
std::vector<unsigned char>* to_destroy) {
RefCountedBytes* bytes = new RefCountedBytes;
diff --git a/base/memory/ref_counted_memory.h b/base/memory/ref_counted_memory.h
index 15a4307..a238c3a 100644
--- a/base/memory/ref_counted_memory.h
+++ b/base/memory/ref_counted_memory.h
@@ -64,8 +64,7 @@ class BASE_EXPORT RefCountedStaticMemory : public RefCountedMemory {
DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
};
-// An implementation of RefCountedMemory, where we own our the data in a
-// vector.
+// An implementation of RefCountedMemory, where we own the data in a vector.
class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
public:
RefCountedBytes();
@@ -73,6 +72,9 @@ class BASE_EXPORT RefCountedBytes : public RefCountedMemory {
// Constructs a RefCountedBytes object by _copying_ from |initializer|.
explicit RefCountedBytes(const std::vector<unsigned char>& initializer);
+ // Constructs a RefCountedBytes object by copying |size| bytes from |p|.
+ RefCountedBytes(const unsigned char* p, size_t size);
+
// Constructs a RefCountedBytes object by performing a swap. (To non
// destructively build a RefCountedBytes, use the constructor that takes a
// vector.)
diff --git a/base/memory/ref_counted_memory_unittest.cc b/base/memory/ref_counted_memory_unittest.cc
index 29bcb42..5bfc1c7 100644
--- a/base/memory/ref_counted_memory_unittest.cc
+++ b/base/memory/ref_counted_memory_unittest.cc
@@ -27,6 +27,16 @@ TEST(RefCountedMemoryUnitTest, RefCountedBytes) {
EXPECT_EQ(2U, mem->size());
EXPECT_EQ(45U, mem->front()[0]);
EXPECT_EQ(99U, mem->front()[1]);
+
+ scoped_refptr<RefCountedMemory> mem2;
+ {
+ unsigned char data2[] = { 12, 11, 99 };
+ mem2 = new RefCountedBytes(data2, 3);
+ }
+ EXPECT_EQ(3U, mem2->size());
+ EXPECT_EQ(12U, mem2->front()[0]);
+ EXPECT_EQ(11U, mem2->front()[1]);
+ EXPECT_EQ(99U, mem2->front()[2]);
}
TEST(RefCountedMemoryUnitTest, RefCountedString) {