[email protected] | ebbccb95 | 2012-04-20 09:51:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 5 | #include "base/memory/ref_counted_memory.h" |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 6 | |
Lei Zhang | 90d4dbbb | 2018-03-30 00:55:16 | [diff] [blame] | 7 | #include <utility> |
8 | |||||
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 9 | #include "base/check_op.h" |
Lei Zhang | 456964852 | 2018-04-24 20:35:57 | [diff] [blame] | 10 | #include "base/memory/read_only_shared_memory_region.h" |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 11 | |
[email protected] | 68c7630b | 2012-05-02 22:37:42 | [diff] [blame] | 12 | namespace base { |
13 | |||||
[email protected] | 55653162 | 2013-01-14 18:59:55 | [diff] [blame] | 14 | bool 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 Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 21 | RefCountedMemory::RefCountedMemory() = default; |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 22 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 23 | RefCountedMemory::~RefCountedMemory() = default; |
[email protected] | d4799a3 | 2010-09-28 22:54:58 | [diff] [blame] | 24 | |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 25 | const unsigned char* RefCountedStaticMemory::front() const { |
26 | return data_; | ||||
27 | } | ||||
28 | |||||
29 | size_t RefCountedStaticMemory::size() const { | ||||
30 | return length_; | ||||
31 | } | ||||
32 | |||||
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 33 | RefCountedStaticMemory::~RefCountedStaticMemory() = default; |
[email protected] | a9aaa9d1 | 2012-04-25 00:42:51 | [diff] [blame] | 34 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 35 | RefCountedBytes::RefCountedBytes() = default; |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 36 | |
37 | RefCountedBytes::RefCountedBytes(const std::vector<unsigned char>& initializer) | ||||
Reilly Grant | e2428b0 | 2021-05-15 03:31:25 | [diff] [blame] | 38 | : data_(initializer) {} |
39 | |||||
40 | RefCountedBytes::RefCountedBytes(base::span<const unsigned char> initializer) | ||||
41 | : data_(initializer.begin(), initializer.end()) {} | ||||
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 42 | |
[email protected] | 6a497d7 | 2014-04-30 20:30:18 | [diff] [blame] | 43 | RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size) |
44 | : data_(p, p + size) {} | ||||
45 | |||||
Reilly Grant | c064d34 | 2017-12-14 20:23:17 | [diff] [blame] | 46 | RefCountedBytes::RefCountedBytes(size_t size) : data_(size, 0) {} |
47 | |||||
vmpstr | 219dc04 | 2016-03-16 19:38:29 | [diff] [blame] | 48 | scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector( |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 49 | std::vector<unsigned char>* to_destroy) { |
Lei Zhang | 94b04bb | 2018-03-23 22:26:02 | [diff] [blame] | 50 | auto bytes = MakeRefCounted<RefCountedBytes>(); |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 51 | bytes->data_.swap(*to_destroy); |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 52 | return bytes; |
[email protected] | d83a560 | 2010-09-16 00:22:48 | [diff] [blame] | 53 | } |
54 | |||||
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 55 | const 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 Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 58 | return size() ? &data_.front() : nullptr; |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 59 | } |
60 | |||||
61 | size_t RefCountedBytes::size() const { | ||||
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 62 | return data_.size(); |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 63 | } |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 64 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 65 | RefCountedBytes::~RefCountedBytes() = default; |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 66 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 67 | RefCountedString::RefCountedString() = default; |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 68 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 69 | RefCountedString::~RefCountedString() = default; |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 70 | |
71 | // static | ||||
estade | b6178e6 | 2016-01-07 17:19:39 | [diff] [blame] | 72 | scoped_refptr<RefCountedString> RefCountedString::TakeString( |
73 | std::string* to_destroy) { | ||||
Lei Zhang | 94b04bb | 2018-03-23 22:26:02 | [diff] [blame] | 74 | auto self = MakeRefCounted<RefCountedString>(); |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 75 | to_destroy->swap(self->data_); |
76 | return self; | ||||
77 | } | ||||
78 | |||||
79 | const unsigned char* RefCountedString::front() const { | ||||
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 80 | return data_.empty() ? nullptr |
81 | : reinterpret_cast<const unsigned char*>(data_.data()); | ||||
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 82 | } |
83 | |||||
84 | size_t RefCountedString::size() const { | ||||
85 | return data_.size(); | ||||
86 | } | ||||
87 | |||||
Joel Hockey | b203b4e | 2020-10-30 20:18:55 | [diff] [blame] | 88 | RefCountedString16::RefCountedString16() = default; |
89 | |||||
90 | RefCountedString16::~RefCountedString16() = default; | ||||
91 | |||||
92 | // static | ||||
93 | scoped_refptr<RefCountedString16> RefCountedString16::TakeString( | ||||
Jan Wilken Dörrie | 085b2aa | 2021-03-12 16:26:57 | [diff] [blame] | 94 | std::u16string* to_destroy) { |
Joel Hockey | b203b4e | 2020-10-30 20:18:55 | [diff] [blame] | 95 | auto self = MakeRefCounted<RefCountedString16>(); |
96 | to_destroy->swap(self->data_); | ||||
97 | return self; | ||||
98 | } | ||||
99 | |||||
100 | const unsigned char* RefCountedString16::front() const { | ||||
101 | return reinterpret_cast<const unsigned char*>(data_.data()); | ||||
102 | } | ||||
103 | |||||
104 | size_t RefCountedString16::size() const { | ||||
Jan Wilken Dörrie | 677e0c87 | 2021-03-10 10:04:38 | [diff] [blame] | 105 | return data_.size() * sizeof(char16_t); |
Joel Hockey | b203b4e | 2020-10-30 20:18:55 | [diff] [blame] | 106 | } |
107 | |||||
Lei Zhang | 456964852 | 2018-04-24 20:35:57 | [diff] [blame] | 108 | RefCountedSharedMemoryMapping::RefCountedSharedMemoryMapping( |
109 | ReadOnlySharedMemoryMapping mapping) | ||||
110 | : mapping_(std::move(mapping)), size_(mapping_.size()) { | ||||
111 | DCHECK_GT(size_, 0U); | ||||
112 | } | ||||
113 | |||||
114 | RefCountedSharedMemoryMapping::~RefCountedSharedMemoryMapping() = default; | ||||
115 | |||||
116 | const unsigned char* RefCountedSharedMemoryMapping::front() const { | ||||
117 | return static_cast<const unsigned char*>(mapping_.memory()); | ||||
118 | } | ||||
119 | |||||
120 | size_t RefCountedSharedMemoryMapping::size() const { | ||||
121 | return size_; | ||||
122 | } | ||||
123 | |||||
124 | // static | ||||
125 | scoped_refptr<RefCountedSharedMemoryMapping> | ||||
126 | RefCountedSharedMemoryMapping::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] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 134 | } // namespace base |