blob: 23a5ffc2566702e63af24a91ea426877ad710c11 [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
[email protected]1dda9772011-07-22 13:22:239#include "base/logging.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)
[email protected]1dda9772011-07-22 13:22:2338 : data_(initializer) {
[email protected]3071754432010-07-28 00:09:5439}
40
[email protected]6a497d72014-04-30 20:30:1841RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size)
42 : data_(p, p + size) {}
43
Reilly Grantc064d342017-12-14 20:23:1744RefCountedBytes::RefCountedBytes(size_t size) : data_(size, 0) {}
45
vmpstr219dc042016-03-16 19:38:2946scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector(
[email protected]9989c9bb2011-01-07 20:23:4347 std::vector<unsigned char>* to_destroy) {
Lei Zhang94b04bb2018-03-23 22:26:0248 auto bytes = MakeRefCounted<RefCountedBytes>();
[email protected]1dda9772011-07-22 13:22:2349 bytes->data_.swap(*to_destroy);
[email protected]9989c9bb2011-01-07 20:23:4350 return bytes;
[email protected]d83a5602010-09-16 00:22:4851}
52
[email protected]3071754432010-07-28 00:09:5453const unsigned char* RefCountedBytes::front() const {
54 // STL will assert if we do front() on an empty vector, but calling code
55 // expects a NULL.
Ivan Kotenkova16212a52017-11-08 12:37:3356 return size() ? &data_.front() : nullptr;
[email protected]3071754432010-07-28 00:09:5457}
58
59size_t RefCountedBytes::size() const {
[email protected]1dda9772011-07-22 13:22:2360 return data_.size();
[email protected]3071754432010-07-28 00:09:5461}
[email protected]9989c9bb2011-01-07 20:23:4362
Chris Watkinsbb7211c2017-11-29 07:16:3863RefCountedBytes::~RefCountedBytes() = default;
[email protected]1dda9772011-07-22 13:22:2364
Chris Watkinsbb7211c2017-11-29 07:16:3865RefCountedString::RefCountedString() = 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
69// static
estadeb6178e62016-01-07 17:19:3970scoped_refptr<RefCountedString> RefCountedString::TakeString(
71 std::string* to_destroy) {
Lei Zhang94b04bb2018-03-23 22:26:0272 auto self = MakeRefCounted<RefCountedString>();
[email protected]1dda9772011-07-22 13:22:2373 to_destroy->swap(self->data_);
74 return self;
75}
76
77const unsigned char* RefCountedString::front() const {
Ivan Kotenkova16212a52017-11-08 12:37:3378 return data_.empty() ? nullptr
79 : reinterpret_cast<const unsigned char*>(data_.data());
[email protected]1dda9772011-07-22 13:22:2380}
81
82size_t RefCountedString::size() const {
83 return data_.size();
84}
85
Lei Zhang90d4dbbb2018-03-30 00:55:1686RefCountedSharedMemory::RefCountedSharedMemory(
87 std::unique_ptr<SharedMemory> shm,
88 size_t size)
89 : shm_(std::move(shm)), size_(size) {
90 DCHECK(shm_);
91 DCHECK(shm_->memory());
92 DCHECK_GT(size_, 0U);
93 DCHECK_LE(size_, shm_->mapped_size());
94}
95
96RefCountedSharedMemory::~RefCountedSharedMemory() = default;
97
98const unsigned char* RefCountedSharedMemory::front() const {
Lei Zhang4569648522018-04-24 20:35:5799 return static_cast<const unsigned char*>(shm_->memory());
Lei Zhang90d4dbbb2018-03-30 00:55:16100}
101
102size_t RefCountedSharedMemory::size() const {
103 return size_;
104}
105
Lei Zhang4569648522018-04-24 20:35:57106RefCountedSharedMemoryMapping::RefCountedSharedMemoryMapping(
107 ReadOnlySharedMemoryMapping mapping)
108 : mapping_(std::move(mapping)), size_(mapping_.size()) {
109 DCHECK_GT(size_, 0U);
110}
111
112RefCountedSharedMemoryMapping::~RefCountedSharedMemoryMapping() = default;
113
114const unsigned char* RefCountedSharedMemoryMapping::front() const {
115 return static_cast<const unsigned char*>(mapping_.memory());
116}
117
118size_t RefCountedSharedMemoryMapping::size() const {
119 return size_;
120}
121
122// static
123scoped_refptr<RefCountedSharedMemoryMapping>
124RefCountedSharedMemoryMapping::CreateFromWholeRegion(
125 const ReadOnlySharedMemoryRegion& region) {
126 ReadOnlySharedMemoryMapping mapping = region.Map();
127 if (!mapping.IsValid())
128 return nullptr;
129 return MakeRefCounted<RefCountedSharedMemoryMapping>(std::move(mapping));
130}
131
[email protected]1dda9772011-07-22 13:22:23132} // namespace base