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) const11bool 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()18RefCountedMemory::RefCountedMemory() {} 19 ~RefCountedMemory()20RefCountedMemory::~RefCountedMemory() {} 21 front() const22const unsigned char* RefCountedStaticMemory::front() const { 23 return data_; 24 } 25 size() const26size_t RefCountedStaticMemory::size() const { 27 return length_; 28 } 29 ~RefCountedStaticMemory()30RefCountedStaticMemory::~RefCountedStaticMemory() {} 31 RefCountedBytes()32RefCountedBytes::RefCountedBytes() {} 33 RefCountedBytes(const std::vector<unsigned char> & initializer)34RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer) 35 : data_(initializer) { 36 } 37 RefCountedBytes(const unsigned char * p,size_t size)38RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size) 39 : data_(p, p + size) {} 40 TakeVector(std::vector<unsigned char> * to_destroy)41RefCountedBytes* 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() const48const 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() const54size_t RefCountedBytes::size() const { 55 return data_.size(); 56 } 57 ~RefCountedBytes()58RefCountedBytes::~RefCountedBytes() {} 59 RefCountedString()60RefCountedString::RefCountedString() {} 61 ~RefCountedString()62RefCountedString::~RefCountedString() {} 63 64 // static TakeString(std::string * to_destroy)65scoped_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() const72const unsigned char* RefCountedString::front() const { 73 return data_.empty() ? NULL : 74 reinterpret_cast<const unsigned char*>(data_.data()); 75 } 76 size() const77size_t RefCountedString::size() const { 78 return data_.size(); 79 } 80 81 } // namespace base 82