[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) | ||||
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 38 | : data_(initializer) { |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 39 | } |
40 | |||||
[email protected] | 6a497d7 | 2014-04-30 20:30:18 | [diff] [blame] | 41 | RefCountedBytes::RefCountedBytes(const unsigned char* p, size_t size) |
42 | : data_(p, p + size) {} | ||||
43 | |||||
Reilly Grant | c064d34 | 2017-12-14 20:23:17 | [diff] [blame] | 44 | RefCountedBytes::RefCountedBytes(size_t size) : data_(size, 0) {} |
45 | |||||
vmpstr | 219dc04 | 2016-03-16 19:38:29 | [diff] [blame] | 46 | scoped_refptr<RefCountedBytes> RefCountedBytes::TakeVector( |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 47 | std::vector<unsigned char>* to_destroy) { |
Lei Zhang | 94b04bb | 2018-03-23 22:26:02 | [diff] [blame] | 48 | auto bytes = MakeRefCounted<RefCountedBytes>(); |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 49 | bytes->data_.swap(*to_destroy); |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 50 | return bytes; |
[email protected] | d83a560 | 2010-09-16 00:22:48 | [diff] [blame] | 51 | } |
52 | |||||
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 53 | const 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 Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 56 | return size() ? &data_.front() : nullptr; |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 57 | } |
58 | |||||
59 | size_t RefCountedBytes::size() const { | ||||
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 60 | return data_.size(); |
[email protected] | 307175443 | 2010-07-28 00:09:54 | [diff] [blame] | 61 | } |
[email protected] | 9989c9bb | 2011-01-07 20:23:43 | [diff] [blame] | 62 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 63 | RefCountedBytes::~RefCountedBytes() = default; |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 64 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 65 | RefCountedString::RefCountedString() = 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 | |
69 | // static | ||||
estade | b6178e6 | 2016-01-07 17:19:39 | [diff] [blame] | 70 | scoped_refptr<RefCountedString> RefCountedString::TakeString( |
71 | std::string* to_destroy) { | ||||
Lei Zhang | 94b04bb | 2018-03-23 22:26:02 | [diff] [blame] | 72 | auto self = MakeRefCounted<RefCountedString>(); |
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 73 | to_destroy->swap(self->data_); |
74 | return self; | ||||
75 | } | ||||
76 | |||||
77 | const unsigned char* RefCountedString::front() const { | ||||
Ivan Kotenkov | a16212a5 | 2017-11-08 12:37:33 | [diff] [blame] | 78 | return data_.empty() ? nullptr |
79 | : reinterpret_cast<const unsigned char*>(data_.data()); | ||||
[email protected] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 80 | } |
81 | |||||
82 | size_t RefCountedString::size() const { | ||||
83 | return data_.size(); | ||||
84 | } | ||||
85 | |||||
Joel Hockey | b203b4e | 2020-10-30 20:18:55 | [diff] [blame] | 86 | RefCountedString16::RefCountedString16() = default; |
87 | |||||
88 | RefCountedString16::~RefCountedString16() = default; | ||||
89 | |||||
90 | // static | ||||
91 | scoped_refptr<RefCountedString16> RefCountedString16::TakeString( | ||||
Jan Wilken Dörrie | 085b2aa | 2021-03-12 16:26:57 | [diff] [blame] | 92 | std::u16string* to_destroy) { |
Joel Hockey | b203b4e | 2020-10-30 20:18:55 | [diff] [blame] | 93 | auto self = MakeRefCounted<RefCountedString16>(); |
94 | to_destroy->swap(self->data_); | ||||
95 | return self; | ||||
96 | } | ||||
97 | |||||
98 | const unsigned char* RefCountedString16::front() const { | ||||
99 | return reinterpret_cast<const unsigned char*>(data_.data()); | ||||
100 | } | ||||
101 | |||||
102 | size_t RefCountedString16::size() const { | ||||
Jan Wilken Dörrie | 677e0c87 | 2021-03-10 10:04:38 | [diff] [blame] | 103 | return data_.size() * sizeof(char16_t); |
Joel Hockey | b203b4e | 2020-10-30 20:18:55 | [diff] [blame] | 104 | } |
105 | |||||
Lei Zhang | 456964852 | 2018-04-24 20:35:57 | [diff] [blame] | 106 | RefCountedSharedMemoryMapping::RefCountedSharedMemoryMapping( |
107 | ReadOnlySharedMemoryMapping mapping) | ||||
108 | : mapping_(std::move(mapping)), size_(mapping_.size()) { | ||||
109 | DCHECK_GT(size_, 0U); | ||||
110 | } | ||||
111 | |||||
112 | RefCountedSharedMemoryMapping::~RefCountedSharedMemoryMapping() = default; | ||||
113 | |||||
114 | const unsigned char* RefCountedSharedMemoryMapping::front() const { | ||||
115 | return static_cast<const unsigned char*>(mapping_.memory()); | ||||
116 | } | ||||
117 | |||||
118 | size_t RefCountedSharedMemoryMapping::size() const { | ||||
119 | return size_; | ||||
120 | } | ||||
121 | |||||
122 | // static | ||||
123 | scoped_refptr<RefCountedSharedMemoryMapping> | ||||
124 | RefCountedSharedMemoryMapping::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] | 1dda977 | 2011-07-22 13:22:23 | [diff] [blame] | 132 | } // namespace base |