1 // Copyright 2012 The Chromium Authors
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 <utility>
8
9 #include "base/check_op.h"
10 #include "base/memory/read_only_shared_memory_region.h"
11
12 namespace base {
13
Equals(const scoped_refptr<RefCountedMemory> & other) const14 bool RefCountedMemory::Equals(
15 const scoped_refptr<RefCountedMemory>& other) const {
16 return other.get() && size() == other->size() &&
17 (size() == 0 || (memcmp(front(), other->front(), size()) == 0));
18 }
19
20 RefCountedMemory::RefCountedMemory() = default;
21
22 RefCountedMemory::~RefCountedMemory() = default;
23
front() const24 const unsigned char* RefCountedStaticMemory::front() const {
25 return data_;
26 }
27
size() const28 size_t RefCountedStaticMemory::size() const {
29 return length_;
30 }
31
32 RefCountedStaticMemory::~RefCountedStaticMemory() = default;
33
34 RefCountedBytes::RefCountedBytes() = default;
35
RefCountedBytes(const std::vector<unsigned char> & initializer)36 RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
37 : data_(initializer) {}
38
RefCountedBytes(base::span<const unsigned char> initializer)39 RefCountedBytes::RefCountedBytes(base::span<const unsigned char> initializer)
40 : data_(initializer.begin(), initializer.end()) {}
41
RefCountedBytes(const unsigned char * p,size_t size)42 RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
43 : data_(p, p + size) {}
44
RefCountedBytes(size_t size)45 RefCountedBytes::RefCountedBytes(size_t size) : data_(size, 0) {}
46
TakeVector(std::vector<unsigned char> * to_destroy)47 scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector(
48 std::vector<unsigned char>* to_destroy) {
49 auto bytes = MakeRefCounted<RefCountedBytes>();
50 bytes->data_.swap(*to_destroy);
51 return bytes;
52 }
53
front() const54 const unsigned char* RefCountedBytes::front() const {
55 // STL will assert if we do front() on an empty vector, but calling code
56 // expects a NULL.
57 return size() ? &data_.front() : nullptr;
58 }
59
size() const60 size_t RefCountedBytes::size() const {
61 return data_.size();
62 }
63
64 RefCountedBytes::~RefCountedBytes() = default;
65
66 RefCountedString::RefCountedString() = default;
67
68 RefCountedString::~RefCountedString() = default;
69
RefCountedString(std::string str)70 RefCountedString::RefCountedString(std::string str) : data_(std::move(str)) {}
71
front() const72 const unsigned char* RefCountedString::front() const {
73 return data_.empty() ? nullptr
74 : reinterpret_cast<const unsigned char*>(data_.data());
75 }
76
size() const77 size_t RefCountedString::size() const {
78 return data_.size();
79 }
80
81 RefCountedString16::RefCountedString16() = default;
82
83 RefCountedString16::~RefCountedString16() = default;
84
RefCountedString16(std::u16string str)85 RefCountedString16::RefCountedString16(std::u16string str)
86 : data_(std::move(str)) {}
87
front() const88 const unsigned char* RefCountedString16::front() const {
89 return reinterpret_cast<const unsigned char*>(data_.data());
90 }
91
size() const92 size_t RefCountedString16::size() const {
93 return data_.size() * sizeof(char16_t);
94 }
95
RefCountedSharedMemoryMapping(ReadOnlySharedMemoryMapping mapping)96 RefCountedSharedMemoryMapping::RefCountedSharedMemoryMapping(
97 ReadOnlySharedMemoryMapping mapping)
98 : mapping_(std::move(mapping)), size_(mapping_.size()) {
99 DCHECK_GT(size_, 0U);
100 }
101
102 RefCountedSharedMemoryMapping::~RefCountedSharedMemoryMapping() = default;
103
front() const104 const unsigned char* RefCountedSharedMemoryMapping::front() const {
105 return static_cast<const unsigned char*>(mapping_.memory());
106 }
107
size() const108 size_t RefCountedSharedMemoryMapping::size() const {
109 return size_;
110 }
111
112 // static
113 scoped_refptr<RefCountedSharedMemoryMapping>
CreateFromWholeRegion(const ReadOnlySharedMemoryRegion & region)114 RefCountedSharedMemoryMapping::CreateFromWholeRegion(
115 const ReadOnlySharedMemoryRegion& region) {
116 ReadOnlySharedMemoryMapping mapping = region.Map();
117 if (!mapping.IsValid())
118 return nullptr;
119 return MakeRefCounted<RefCountedSharedMemoryMapping>(std::move(mapping));
120 }
121
122 } // namespace base
123