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 11namespace base { 12 13bool 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 20RefCountedMemory::RefCountedMemory() {} 21 22RefCountedMemory::~RefCountedMemory() {} 23 24const unsigned char* RefCountedStaticMemory::front() const { 25 return data_; 26} 27 28size_t RefCountedStaticMemory::size() const { 29 return length_; 30} 31 32RefCountedStaticMemory::~RefCountedStaticMemory() {} 33 34RefCountedBytes::RefCountedBytes() {} 35 36RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer) 37 : data_(initializer) { 38} 39 40RefCountedBytes* RefCountedBytes::TakeVector( 41 std::vector<unsigned char>* to_destroy) { 42 RefCountedBytes* bytes = new RefCountedBytes; 43 bytes->data_.swap(*to_destroy); 44 return bytes; 45} 46 47const unsigned char* RefCountedBytes::front() const { 48 // STL will assert if we do front() on an empty vector, but calling code 49 // expects a NULL. 50 return size() ? &data_.front() : NULL; 51} 52 53size_t RefCountedBytes::size() const { 54 return data_.size(); 55} 56 57RefCountedBytes::~RefCountedBytes() {} 58 59RefCountedString::RefCountedString() {} 60 61RefCountedString::~RefCountedString() {} 62 63// static 64RefCountedString* RefCountedString::TakeString(std::string* to_destroy) { 65 RefCountedString* self = new RefCountedString; 66 to_destroy->swap(self->data_); 67 return self; 68} 69 70const unsigned char* RefCountedString::front() const { 71 return data_.empty() ? NULL : 72 reinterpret_cast<const unsigned char*>(data_.data()); 73} 74 75size_t RefCountedString::size() const { 76 return data_.size(); 77} 78 79RefCountedMallocedMemory::RefCountedMallocedMemory( 80 void* data, size_t length) 81 : data_(reinterpret_cast<unsigned char*>(data)), length_(length) { 82 DCHECK(data || length == 0); 83} 84 85const unsigned char* RefCountedMallocedMemory::front() const { 86 return length_ ? data_ : NULL; 87} 88 89size_t RefCountedMallocedMemory::size() const { 90 return length_; 91} 92 93RefCountedMallocedMemory::~RefCountedMallocedMemory() { 94 free(data_); 95} 96 97} // namespace base 98