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 <stdlib.h> 8 9 #include "base/logging.h" 10 11 namespace base { 12 Equals(const scoped_refptr<RefCountedMemory> & other) const13bool RefCountedMemory::Equals( 14 const scoped_refptr<RefCountedMemory>& other) const { 15 return other.get() && 16 size() == other->size() && 17 (memcmp(front(), other->front(), size()) == 0); 18 } 19 RefCountedMemory()20RefCountedMemory::RefCountedMemory() {} 21 ~RefCountedMemory()22RefCountedMemory::~RefCountedMemory() {} 23 front() const24const unsigned char* RefCountedStaticMemory::front() const { 25 return data_; 26 } 27 size() const28size_t RefCountedStaticMemory::size() const { 29 return length_; 30 } 31 ~RefCountedStaticMemory()32RefCountedStaticMemory::~RefCountedStaticMemory() {} 33 RefCountedBytes()34RefCountedBytes::RefCountedBytes() {} 35 RefCountedBytes(const std::vector<unsigned char> & initializer)36RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer) 37 : data_(initializer) { 38 } 39 RefCountedBytes(const unsigned char * p,size_t size)40RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size) 41 : data_(p, p + size) {} 42 TakeVector(std::vector<unsigned char> * to_destroy)43RefCountedBytes* RefCountedBytes::TakeVector( 44 std::vector<unsigned char>* to_destroy) { 45 RefCountedBytes* bytes = new RefCountedBytes; 46 bytes->data_.swap(*to_destroy); 47 return bytes; 48 } 49 front() const50const unsigned char* RefCountedBytes::front() const { 51 // STL will assert if we do front() on an empty vector, but calling code 52 // expects a NULL. 53 return size() ? &data_.front() : NULL; 54 } 55 size() const56size_t RefCountedBytes::size() const { 57 return data_.size(); 58 } 59 ~RefCountedBytes()60RefCountedBytes::~RefCountedBytes() {} 61 RefCountedString()62RefCountedString::RefCountedString() {} 63 ~RefCountedString()64RefCountedString::~RefCountedString() {} 65 66 // static TakeString(std::string * to_destroy)67RefCountedString* RefCountedString::TakeString(std::string* to_destroy) { 68 RefCountedString* self = new RefCountedString; 69 to_destroy->swap(self->data_); 70 return self; 71 } 72 front() const73const unsigned char* RefCountedString::front() const { 74 return data_.empty() ? NULL : 75 reinterpret_cast<const unsigned char*>(data_.data()); 76 } 77 size() const78size_t RefCountedString::size() const { 79 return data_.size(); 80 } 81 RefCountedMallocedMemory(void * data,size_t length)82RefCountedMallocedMemory::RefCountedMallocedMemory( 83 void* data, size_t length) 84 : data_(reinterpret_cast<unsigned char*>(data)), length_(length) { 85 DCHECK(data || length == 0); 86 } 87 front() const88const unsigned char* RefCountedMallocedMemory::front() const { 89 return length_ ? data_ : NULL; 90 } 91 size() const92size_t RefCountedMallocedMemory::size() const { 93 return length_; 94 } 95 ~RefCountedMallocedMemory()96RefCountedMallocedMemory::~RefCountedMallocedMemory() { 97 free(data_); 98 } 99 100 } // namespace base 101