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 11 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 18 RefCountedMemory::RefCountedMemory() {} 19 20 RefCountedMemory::~RefCountedMemory() {} 21 22 const unsigned char* RefCountedStaticMemory::front() const { 23 return data_; 24 } 25 26 size_t RefCountedStaticMemory::size() const { 27 return length_; 28 } 29 30 RefCountedStaticMemory::~RefCountedStaticMemory() {} 31 32 RefCountedBytes::RefCountedBytes() {} 33 34 RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer) 35 : data_(initializer) { 36 } 37 38 RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size) 39 : data_(p, p + size) {} 40 41 scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector( 42 std::vector<unsigned char>* to_destroy) { 43 scoped_refptr<RefCountedBytes> bytes(new RefCountedBytes); 44 bytes->data_.swap(*to_destroy); 45 return bytes; 46 } 47 48 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 54 size_t RefCountedBytes::size() const { 55 return data_.size(); 56 } 57 58 RefCountedBytes::~RefCountedBytes() {} 59 60 RefCountedString::RefCountedString() {} 61 62 RefCountedString::~RefCountedString() {} 63 64 // static 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 72 const unsigned char* RefCountedString::front() const { 73 return data_.empty() ? NULL : 74 reinterpret_cast<const unsigned char*>(data_.data()); 75 } 76 77 size_t RefCountedString::size() const { 78 return data_.size(); 79 } 80 81 } // namespace base 82