1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/memory/ref_counted_memory.h"
6 
7 #include "base/logging.h"
8 
9 namespace base {
10 
Equals(const scoped_refptr<RefCountedMemory> & other) const11 bool RefCountedMemory::Equals(
12     const scoped_refptr<RefCountedMemory>& other) const {
13   return other.get() &&
14          size() == other->size() &&
15          (memcmp(front(), other->front(), size()) == 0);
16 }
17 
RefCountedMemory()18 RefCountedMemory::RefCountedMemory() {}
19 
~RefCountedMemory()20 RefCountedMemory::~RefCountedMemory() {}
21 
front() const22 const unsigned char* RefCountedStaticMemory::front() const {
23   return data_;
24 }
25 
size() const26 size_t RefCountedStaticMemory::size() const {
27   return length_;
28 }
29 
~RefCountedStaticMemory()30 RefCountedStaticMemory::~RefCountedStaticMemory() {}
31 
RefCountedBytes()32 RefCountedBytes::RefCountedBytes() {}
33 
RefCountedBytes(const std::vector<unsigned char> & initializer)34 RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
35     : data_(initializer) {
36 }
37 
RefCountedBytes(const unsigned char * p,size_t size)38 RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
39     : data_(p, p + size) {}
40 
TakeVector(std::vector<unsigned char> * to_destroy)41 RefCountedBytes* RefCountedBytes::TakeVector(
42     std::vector<unsigned char>* to_destroy) {
43   RefCountedBytes* bytes = new RefCountedBytes;
44   bytes->data_.swap(*to_destroy);
45   return bytes;
46 }
47 
front() const48 const unsigned char* RefCountedBytes::front() const {
49   // STL will assert if we do front() on an empty vector, but calling code
50   // expects a NULL.
51   return size() ? &data_.front() : NULL;
52 }
53 
size() const54 size_t RefCountedBytes::size() const {
55   return data_.size();
56 }
57 
~RefCountedBytes()58 RefCountedBytes::~RefCountedBytes() {}
59 
RefCountedString()60 RefCountedString::RefCountedString() {}
61 
~RefCountedString()62 RefCountedString::~RefCountedString() {}
63 
64 // static
TakeString(std::string * to_destroy)65 scoped_refptr<RefCountedString> RefCountedString::TakeString(
66     std::string* to_destroy) {
67   scoped_refptr<RefCountedString> self(new RefCountedString);
68   to_destroy->swap(self->data_);
69   return self;
70 }
71 
front() const72 const unsigned char* RefCountedString::front() const {
73   return data_.empty() ? NULL :
74          reinterpret_cast<const unsigned char*>(data_.data());
75 }
76 
size() const77 size_t RefCountedString::size() const {
78   return data_.size();
79 }
80 
81 }  //  namespace base
82