ref_counted_memory.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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
9namespace base {
10
11bool 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
18RefCountedMemory::RefCountedMemory() {}
19
20RefCountedMemory::~RefCountedMemory() {}
21
22const unsigned char* RefCountedStaticMemory::front() const {
23  return data_;
24}
25
26size_t RefCountedStaticMemory::size() const {
27  return length_;
28}
29
30RefCountedStaticMemory::~RefCountedStaticMemory() {}
31
32RefCountedBytes::RefCountedBytes() {}
33
34RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
35    : data_(initializer) {
36}
37
38RefCountedBytes* RefCountedBytes::TakeVector(
39    std::vector<unsigned char>* to_destroy) {
40  RefCountedBytes* bytes = new RefCountedBytes;
41  bytes->data_.swap(*to_destroy);
42  return bytes;
43}
44
45const unsigned char* RefCountedBytes::front() const {
46  // STL will assert if we do front() on an empty vector, but calling code
47  // expects a NULL.
48  return size() ? &data_.front() : NULL;
49}
50
51size_t RefCountedBytes::size() const {
52  return data_.size();
53}
54
55RefCountedBytes::~RefCountedBytes() {}
56
57RefCountedString::RefCountedString() {}
58
59RefCountedString::~RefCountedString() {}
60
61// static
62RefCountedString* RefCountedString::TakeString(std::string* to_destroy) {
63  RefCountedString* self = new RefCountedString;
64  to_destroy->swap(self->data_);
65  return self;
66}
67
68const unsigned char* RefCountedString::front() const {
69  return data_.empty() ? NULL :
70         reinterpret_cast<const unsigned char*>(data_.data());
71}
72
73size_t RefCountedString::size() const {
74  return data_.size();
75}
76
77}  //  namespace base
78