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.
5 #include "base/memory/ref_counted_memory.h"
9 #include "base/logging.h"
13 bool RefCountedMemory::Equals(
14 const scoped_refptr<RefCountedMemory>& other) const {
16 size() == other->size() &&
17 (memcmp(front(), other->front(), size()) == 0);
20 RefCountedMemory::RefCountedMemory() {}
22 RefCountedMemory::~RefCountedMemory() {}
24 const unsigned char* RefCountedStaticMemory::front() const {
28 size_t RefCountedStaticMemory::size() const {
32 RefCountedStaticMemory::~RefCountedStaticMemory() {}
34 RefCountedBytes::RefCountedBytes() {}
36 RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
37 : data_(initializer) {
40 RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
41 : data_(p, p + size) {}
43 RefCountedBytes* RefCountedBytes::TakeVector(
44 std::vector<unsigned char>* to_destroy) {
45 RefCountedBytes* bytes = new RefCountedBytes;
46 bytes->data_.swap(*to_destroy);
50 const unsigned char* RefCountedBytes::front() const {
51 // STL will assert if we do front() on an empty vector, but calling code
53 return size() ? &data_.front() : NULL;
56 size_t RefCountedBytes::size() const {
60 RefCountedBytes::~RefCountedBytes() {}
62 RefCountedString::RefCountedString() {}
64 RefCountedString::~RefCountedString() {}
67 RefCountedString* RefCountedString::TakeString(std::string* to_destroy) {
68 RefCountedString* self = new RefCountedString;
69 to_destroy->swap(self->data_);
73 const unsigned char* RefCountedString::front() const {
74 return data_.empty() ? NULL :
75 reinterpret_cast<const unsigned char*>(data_.data());
78 size_t RefCountedString::size() const {
82 RefCountedMallocedMemory::RefCountedMallocedMemory(
83 void* data, size_t length)
84 : data_(reinterpret_cast<unsigned char*>(data)), length_(length) {
85 DCHECK(data || length == 0);
88 const unsigned char* RefCountedMallocedMemory::front() const {
89 return length_ ? data_ : NULL;
92 size_t RefCountedMallocedMemory::size() const {
96 RefCountedMallocedMemory::~RefCountedMallocedMemory() {