blob: 09e84d33ad347080d4df6c13bc2bd0db953da4ef [file] [log] [blame]
[email protected]ebbccb952012-04-20 09:51:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]3071754432010-07-28 00:09:542// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]3b63f8f42011-03-28 01:54:155#include "base/memory/ref_counted_memory.h"
[email protected]3071754432010-07-28 00:09:546
Lei Zhang90d4dbbb2018-03-30 00:55:167#include <utility>
8
Hans Wennborgc3cffa62020-04-27 10:09:129#include "base/check_op.h"
Lei Zhang4569648522018-04-24 20:35:5710#include "base/memory/read_only_shared_memory_region.h"
[email protected]1dda9772011-07-22 13:22:2311
[email protected]68c7630b2012-05-02 22:37:4212namespace base {
13
[email protected]556531622013-01-14 18:59:5514bool RefCountedMemory::Equals(
15 const scoped_refptr<RefCountedMemory>& other) const {
16 return other.get() &&
17 size() == other->size() &&
18 (memcmp(front(), other->front(), size()) == 0);
19}
20
Chris Watkinsbb7211c2017-11-29 07:16:3821RefCountedMemory::RefCountedMemory() = default;
[email protected]d4799a32010-09-28 22:54:5822
Chris Watkinsbb7211c2017-11-29 07:16:3823RefCountedMemory::~RefCountedMemory() = default;
[email protected]d4799a32010-09-28 22:54:5824
[email protected]3071754432010-07-28 00:09:5425const unsigned char* RefCountedStaticMemory::front() const {
26 return data_;
27}
28
29size_t RefCountedStaticMemory::size() const {
30 return length_;
31}
32
Chris Watkinsbb7211c2017-11-29 07:16:3833RefCountedStaticMemory::~RefCountedStaticMemory() = default;
[email protected]a9aaa9d12012-04-25 00:42:5134
Chris Watkinsbb7211c2017-11-29 07:16:3835RefCountedBytes::RefCountedBytes() = default;
[email protected]3071754432010-07-28 00:09:5436
37RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer)
Reilly Grante2428b02021-05-15 03:31:2538 : data_(initializer) {}
39
40RefCountedBytes::RefCountedBytes(base::span<const unsigned char> initializer)
41 : data_(initializer.begin(), initializer.end()) {}
[email protected]3071754432010-07-28 00:09:5442
[email protected]6a497d72014-04-30 20:30:1843RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
44 : data_(p, p + size) {}
45
Reilly Grantc064d342017-12-14 20:23:1746RefCountedBytes::RefCountedBytes(size_t size) : data_(size, 0) {}
47
vmpstr219dc042016-03-16 19:38:2948scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector(
[email protected]9989c9bb2011-01-07 20:23:4349 std::vector<unsigned char>* to_destroy) {
Lei Zhang94b04bb2018-03-23 22:26:0250 auto bytes = MakeRefCounted<RefCountedBytes>();
[email protected]1dda9772011-07-22 13:22:2351 bytes->data_.swap(*to_destroy);
[email protected]9989c9bb2011-01-07 20:23:4352 return bytes;
[email protected]d83a5602010-09-16 00:22:4853}
54
[email protected]3071754432010-07-28 00:09:5455const unsigned char* RefCountedBytes::front() const {
56 // STL will assert if we do front() on an empty vector, but calling code
57 // expects a NULL.
Ivan Kotenkova16212a52017-11-08 12:37:3358 return size() ? &data_.front() : nullptr;
[email protected]3071754432010-07-28 00:09:5459}
60
61size_t RefCountedBytes::size() const {
[email protected]1dda9772011-07-22 13:22:2362 return data_.size();
[email protected]3071754432010-07-28 00:09:5463}
[email protected]9989c9bb2011-01-07 20:23:4364
Chris Watkinsbb7211c2017-11-29 07:16:3865RefCountedBytes::~RefCountedBytes() = default;
[email protected]1dda9772011-07-22 13:22:2366
Chris Watkinsbb7211c2017-11-29 07:16:3867RefCountedString::RefCountedString() = default;
[email protected]1dda9772011-07-22 13:22:2368
Chris Watkinsbb7211c2017-11-29 07:16:3869RefCountedString::~RefCountedString() = default;
[email protected]1dda9772011-07-22 13:22:2370
71// static
estadeb6178e62016-01-07 17:19:3972scoped_refptr<RefCountedString> RefCountedString::TakeString(
73 std::string* to_destroy) {
Lei Zhang94b04bb2018-03-23 22:26:0274 auto self = MakeRefCounted<RefCountedString>();
[email protected]1dda9772011-07-22 13:22:2375 to_destroy->swap(self->data_);
76 return self;
77}
78
79const unsigned char* RefCountedString::front() const {
Ivan Kotenkova16212a52017-11-08 12:37:3380 return data_.empty() ? nullptr
81 : reinterpret_cast<const unsigned char*>(data_.data());
[email protected]1dda9772011-07-22 13:22:2382}
83
84size_t RefCountedString::size() const {
85 return data_.size();
86}
87
Joel Hockeyb203b4e2020-10-30 20:18:5588RefCountedString16::RefCountedString16() = default;
89
90RefCountedString16::~RefCountedString16() = default;
91
92// static
93scoped_refptr<RefCountedString16> RefCountedString16::TakeString(
Jan Wilken Dörrie085b2aa2021-03-12 16:26:5794 std::u16string* to_destroy) {
Joel Hockeyb203b4e2020-10-30 20:18:5595 auto self = MakeRefCounted<RefCountedString16>();
96 to_destroy->swap(self->data_);
97 return self;
98}
99
100const unsigned char* RefCountedString16::front() const {
101 return reinterpret_cast<const unsigned char*>(data_.data());
102}
103
104size_t RefCountedString16::size() const {
Jan Wilken Dörrie677e0c872021-03-10 10:04:38105 return data_.size() * sizeof(char16_t);
Joel Hockeyb203b4e2020-10-30 20:18:55106}
107
Lei Zhang4569648522018-04-24 20:35:57108RefCountedSharedMemoryMapping::RefCountedSharedMemoryMapping(
109 ReadOnlySharedMemoryMapping mapping)
110 : mapping_(std::move(mapping)), size_(mapping_.size()) {
111 DCHECK_GT(size_, 0U);
112}
113
114RefCountedSharedMemoryMapping::~RefCountedSharedMemoryMapping() = default;
115
116const unsigned char* RefCountedSharedMemoryMapping::front() const {
117 return static_cast<const unsigned char*>(mapping_.memory());
118}
119
120size_t RefCountedSharedMemoryMapping::size() const {
121 return size_;
122}
123
124// static
125scoped_refptr<RefCountedSharedMemoryMapping>
126RefCountedSharedMemoryMapping::CreateFromWholeRegion(
127 const ReadOnlySharedMemoryRegion& region) {
128 ReadOnlySharedMemoryMapping mapping = region.Map();
129 if (!mapping.IsValid())
130 return nullptr;
131 return MakeRefCounted<RefCountedSharedMemoryMapping>(std::move(mapping));
132}
133
[email protected]1dda9772011-07-22 13:22:23134} // namespace base