Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Side by Side Diff: mojo/edk/embedder/simple_platform_shared_buffer.cc

Issue 1685513002: Replace Mojo SimplePlatformSharedMemory implementation with base::SharedMemory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix windows build. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/shared_memory.h"
13 #include "base/process/process_handle.h"
14 #include "base/sys_info.h"
12 #include "mojo/edk/embedder/platform_handle_utils.h" 15 #include "mojo/edk/embedder/platform_handle_utils.h"
13 16
14 namespace mojo { 17 namespace mojo {
15 namespace edk { 18 namespace edk {
16 19
20 namespace {
21
22 // Takes ownership of |memory_handle|.
23 ScopedPlatformHandle SharedMemoryToPlatformHandle(
24 base::SharedMemoryHandle memory_handle) {
25 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
26 return ScopedPlatformHandle(PlatformHandle(memory_handle.fd));
27 #elif defined(OS_WIN)
28 return ScopedPlatformHandle(PlatformHandle(memory_handle.GetHandle()));
29 #else
30 CHECK_EQ(memory_handle.GetType(), base::SharedMemoryHandle::POSIX);
31 return ScopedPlatformHandle(PlatformHandle(
32 memory_handle.GetFileDescriptor().fd));
33 #endif
34 }
35
36 } // namespace
37
17 // static 38 // static
18 SimplePlatformSharedBuffer* SimplePlatformSharedBuffer::Create( 39 SimplePlatformSharedBuffer* SimplePlatformSharedBuffer::Create(
19 size_t num_bytes) { 40 size_t num_bytes) {
20 DCHECK_GT(num_bytes, 0u); 41 DCHECK_GT(num_bytes, 0u);
21 42
22 SimplePlatformSharedBuffer* rv = new SimplePlatformSharedBuffer(num_bytes); 43 SimplePlatformSharedBuffer* rv = new SimplePlatformSharedBuffer(num_bytes);
23 if (!rv->Init()) { 44 if (!rv->Init()) {
24 // We can't just delete it directly, due to the "in destructor" (debug) 45 // We can't just delete it directly, due to the "in destructor" (debug)
25 // check. 46 // check.
26 scoped_refptr<SimplePlatformSharedBuffer> deleter(rv); 47 scoped_refptr<SimplePlatformSharedBuffer> deleter(rv);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 if (length > num_bytes_ - offset) 91 if (length > num_bytes_ - offset)
71 return false; 92 return false;
72 93
73 return true; 94 return true;
74 } 95 }
75 96
76 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapNoCheck( 97 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapNoCheck(
77 size_t offset, 98 size_t offset,
78 size_t length) { 99 size_t length) {
79 DCHECK(IsValidMap(offset, length)); 100 DCHECK(IsValidMap(offset, length));
80 return MapImpl(offset, length); 101 DCHECK(shared_memory_);
102 base::SharedMemoryHandle handle;
103 {
104 base::AutoLock locker(lock_);
105 handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle());
106 }
107 if (handle == base::SharedMemory::NULLHandle())
108 return nullptr;
109
110 scoped_ptr<SimplePlatformSharedBufferMapping> mapping(
111 new SimplePlatformSharedBufferMapping(handle, offset, length));
112 if (mapping->Map())
113 return make_scoped_ptr(mapping.release());
114
115 return nullptr;
81 } 116 }
82 117
83 ScopedPlatformHandle SimplePlatformSharedBuffer::DuplicatePlatformHandle() { 118 ScopedPlatformHandle SimplePlatformSharedBuffer::DuplicatePlatformHandle() {
84 return mojo::edk::DuplicatePlatformHandle(handle_.get()); 119 DCHECK(shared_memory_);
120 base::SharedMemoryHandle handle;
121 {
122 base::AutoLock locker(lock_);
123 handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle());
124 }
125 if (handle == base::SharedMemory::NULLHandle())
126 return ScopedPlatformHandle();
127
128 return SharedMemoryToPlatformHandle(handle);
85 } 129 }
86 130
87 ScopedPlatformHandle SimplePlatformSharedBuffer::PassPlatformHandle() { 131 ScopedPlatformHandle SimplePlatformSharedBuffer::PassPlatformHandle() {
88 DCHECK(HasOneRef()); 132 DCHECK(HasOneRef());
89 return std::move(handle_); 133
134 // The only way to pass a handle from base::SharedMemory is to duplicate it
135 // and close the original.
136 ScopedPlatformHandle handle = DuplicatePlatformHandle();
137
138 base::AutoLock locker(lock_);
139 shared_memory_->Close();
140 return handle;
90 } 141 }
91 142
92 SimplePlatformSharedBuffer::SimplePlatformSharedBuffer(size_t num_bytes) 143 SimplePlatformSharedBuffer::SimplePlatformSharedBuffer(size_t num_bytes)
93 : num_bytes_(num_bytes) { 144 : num_bytes_(num_bytes) {
94 } 145 }
95 146
96 SimplePlatformSharedBuffer::~SimplePlatformSharedBuffer() { 147 SimplePlatformSharedBuffer::~SimplePlatformSharedBuffer() {
97 } 148 }
98 149
150 bool SimplePlatformSharedBuffer::Init() {
151 DCHECK(!shared_memory_);
152
153 base::SharedMemoryCreateOptions options;
154 options.size = num_bytes_;
155 #if defined(OS_MACOSX) && !defined(OS_IOS)
156 // TODO(crbug.com/582468): Support Mach shared memory.
157 options.type = base::SharedMemoryHandle::POSIX;
158 #endif
159
160 shared_memory_.reset(new base::SharedMemory);
161 return shared_memory_->Create(options);
162 }
163
164 bool SimplePlatformSharedBuffer::InitFromPlatformHandle(
165 ScopedPlatformHandle platform_handle) {
166 DCHECK(!shared_memory_);
167
168 #if defined(OS_WIN)
169 base::SharedMemoryHandle handle(platform_handle.release().handle,
170 base::GetCurrentProcId());
171 #else
172 base::SharedMemoryHandle handle(platform_handle.release().handle, false);
173 #endif
174
175 shared_memory_.reset(new base::SharedMemory(handle, false));
176 return true;
177 }
178
99 SimplePlatformSharedBufferMapping::~SimplePlatformSharedBufferMapping() { 179 SimplePlatformSharedBufferMapping::~SimplePlatformSharedBufferMapping() {
100 Unmap(); 180 Unmap();
101 } 181 }
102 182
103 void* SimplePlatformSharedBufferMapping::GetBase() const { 183 void* SimplePlatformSharedBufferMapping::GetBase() const {
104 return base_; 184 return base_;
105 } 185 }
106 186
107 size_t SimplePlatformSharedBufferMapping::GetLength() const { 187 size_t SimplePlatformSharedBufferMapping::GetLength() const {
108 return length_; 188 return length_;
109 } 189 }
110 190
191 bool SimplePlatformSharedBufferMapping::Map() {
192 size_t offset_rounding = offset_ % base::SysInfo::VMAllocationGranularity();
Ken Rockot(use gerrit already) 2016/02/09 04:39:57 nit: might not hurt to explain why this is done
Anand Mistry (off Chromium) 2016/02/09 05:04:59 Done.
193 size_t real_offset = offset_ - offset_rounding;
194 size_t real_length = length_ + offset_rounding;
195
196 if (!shared_memory_.MapAt(static_cast<off_t>(real_offset), real_length))
197 return false;
198
199 base_ = static_cast<char*>(shared_memory_.memory()) + offset_rounding;
200 return true;
201 }
202
203 void SimplePlatformSharedBufferMapping::Unmap() {
204 shared_memory_.Unmap();
205 }
206
111 } // namespace edk 207 } // namespace edk
112 } // namespace mojo 208 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/embedder/simple_platform_shared_buffer.h ('k') | mojo/edk/embedder/simple_platform_shared_buffer_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698