diff options
Diffstat (limited to 'base/memory')
-rw-r--r-- | base/memory/ref_counted_memory.cc | 34 | ||||
-rw-r--r-- | base/memory/ref_counted_memory.h | 51 | ||||
-rw-r--r-- | base/memory/ref_counted_memory_unittest.cc | 45 |
3 files changed, 12 insertions, 118 deletions
diff --git a/base/memory/ref_counted_memory.cc b/base/memory/ref_counted_memory.cc index 7e034f9..aa16031 100644 --- a/base/memory/ref_counted_memory.cc +++ b/base/memory/ref_counted_memory.cc @@ -4,8 +4,6 @@ #include "base/memory/ref_counted_memory.h" -#include "base/logging.h" - RefCountedMemory::RefCountedMemory() { } @@ -24,49 +22,25 @@ RefCountedBytes::RefCountedBytes() { } RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer) - : data_(initializer) { + : data(initializer) { } RefCountedBytes* RefCountedBytes::TakeVector( std::vector<unsigned char>* to_destroy) { RefCountedBytes* bytes = new RefCountedBytes; - bytes->data_.swap(*to_destroy); + bytes->data.swap(*to_destroy); return bytes; } const unsigned char* RefCountedBytes::front() const { // STL will assert if we do front() on an empty vector, but calling code // expects a NULL. - return size() ? &data_.front() : NULL; + return size() ? &data.front() : NULL; } size_t RefCountedBytes::size() const { - return data_.size(); + return data.size(); } RefCountedBytes::~RefCountedBytes() { } - -namespace base { - -RefCountedString::RefCountedString() {} - -RefCountedString::~RefCountedString() {} - -// static -RefCountedString* RefCountedString::TakeString(std::string* to_destroy) { - RefCountedString* self = new RefCountedString; - to_destroy->swap(self->data_); - return self; -} - -const unsigned char* RefCountedString::front() const { - return data_.empty() ? NULL : - reinterpret_cast<const unsigned char*>(data_.data()); -} - -size_t RefCountedString::size() const { - return data_.size(); -} - -} // namespace base diff --git a/base/memory/ref_counted_memory.h b/base/memory/ref_counted_memory.h index 0d48f64..1a0f51ee 100644 --- a/base/memory/ref_counted_memory.h +++ b/base/memory/ref_counted_memory.h @@ -6,11 +6,9 @@ #define BASE_MEMORY_REF_COUNTED_MEMORY_H_ #pragma once -#include <string> #include <vector> #include "base/base_api.h" -#include "base/compiler_specific.h" #include "base/memory/ref_counted.h" // TODO(erg): The contents of this file should be in a namespace. This would @@ -42,9 +40,9 @@ class BASE_API RefCountedStaticMemory : public RefCountedMemory { RefCountedStaticMemory() : data_(NULL), length_(0) {} RefCountedStaticMemory(const unsigned char* data, size_t length) - : data_(length ? data : NULL), length_(length) {} + : data_(data), length_(length) {} - // Overridden from RefCountedMemory: + // Overriden from RefCountedMemory: virtual const unsigned char* front() const; virtual size_t size() const; @@ -69,51 +67,18 @@ class BASE_API RefCountedBytes : public RefCountedMemory { // vector.) static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); - // Overridden from RefCountedMemory: - virtual const unsigned char* front() const OVERRIDE; - virtual size_t size() const OVERRIDE; + // Overriden from RefCountedMemory: + virtual const unsigned char* front() const; + virtual size_t size() const; - const std::vector<unsigned char>& data() const { return data_; } - std::vector<unsigned char>& data() { return data_; } + std::vector<unsigned char> data; - private: + protected: friend class base::RefCountedThreadSafe<RefCountedBytes>; virtual ~RefCountedBytes(); - std::vector<unsigned char> data_; - - DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); -}; - -namespace base { - -// An implementation of RefCountedMemory, where the bytes are stored in an STL -// string. Use this if your data naturally arrives in that format. -class BASE_API RefCountedString : public RefCountedMemory { - public: - RefCountedString(); - - // Constructs a RefCountedString object by performing a swap. (To non - // destructively build a RefCountedString, use the default constructor and - // copy into object->data()). - static RefCountedString* TakeString(std::string* to_destroy); - - // Overridden from RefCountedMemory: - virtual const unsigned char* front() const OVERRIDE; - virtual size_t size() const OVERRIDE; - - const std::string& data() const { return data_; } - std::string& data() { return data_; } - private: - friend class base::RefCountedThreadSafe<RefCountedString>; - virtual ~RefCountedString(); - - std::string data_; - - DISALLOW_COPY_AND_ASSIGN(RefCountedString); + DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); }; -} // namespace base - #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_ diff --git a/base/memory/ref_counted_memory_unittest.cc b/base/memory/ref_counted_memory_unittest.cc deleted file mode 100644 index 1936040..0000000 --- a/base/memory/ref_counted_memory_unittest.cc +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/memory/ref_counted_memory.h" - -#include "testing/gtest/include/gtest/gtest.h" - -namespace base { - -TEST(RefCountedMemoryUnitTest, RefCountedStaticMemory) { - scoped_refptr<RefCountedMemory> mem = new RefCountedStaticMemory( - reinterpret_cast<const uint8*>("static mem00"), 10); - - EXPECT_EQ(10U, mem->size()); - EXPECT_EQ("static mem", - std::string(reinterpret_cast<const char*>(mem->front()), - mem->size())); -} - -TEST(RefCountedMemoryUnitTest, RefCountedBytes) { - std::vector<uint8> data; - data.push_back(45); - data.push_back(99); - scoped_refptr<RefCountedMemory> mem = RefCountedBytes::TakeVector(&data); - - EXPECT_EQ(0U, data.size()); - - EXPECT_EQ(2U, mem->size()); - EXPECT_EQ(45U, mem->front()[0]); - EXPECT_EQ(99U, mem->front()[1]); -} - -TEST(RefCountedMemoryUnitTest, RefCountedString) { - std::string s("destroy me"); - scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s); - - EXPECT_EQ(0U, s.size()); - - EXPECT_EQ(10U, mem->size()); - EXPECT_EQ('d', mem->front()[0]); - EXPECT_EQ('e', mem->front()[1]); -} - -} // namespace base |