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

Side by Side Diff: base/memory/shared_memory_android.cc

Issue 2875453002: Add a size parameter to SharedMemoryHandle. (Closed)
Patch Set: Fix IPC on POSIX. Created 3 years, 7 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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/memory/shared_memory.h" 5 #include "base/memory/shared_memory.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <sys/mman.h> 8 #include <sys/mman.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "third_party/ashmem/ashmem.h" 11 #include "third_party/ashmem/ashmem.h"
12 12
13 namespace base { 13 namespace base {
14 14
15 // For Android, we use ashmem to implement SharedMemory. ashmem_create_region 15 // For Android, we use ashmem to implement SharedMemory. ashmem_create_region
16 // will automatically pin the region. We never explicitly call pin/unpin. When 16 // will automatically pin the region. We never explicitly call pin/unpin. When
17 // all the file descriptors from different processes associated with the region 17 // all the file descriptors from different processes associated with the region
18 // are closed, the memory buffer will go away. 18 // are closed, the memory buffer will go away.
19 19
20 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { 20 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
21 DCHECK(!shm_.IsValid()); 21 DCHECK(!shm_.IsValid());
22 22
23 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) 23 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max()))
24 return false; 24 return false;
25 25
26 // "name" is just a label in ashmem. It is visible in /proc/pid/maps. 26 // "name" is just a label in ashmem. It is visible in /proc/pid/maps.
27 int fd = ashmem_create_region( 27 int fd = ashmem_create_region(
28 options.name_deprecated == NULL ? "" : options.name_deprecated->c_str(), 28 options.name_deprecated == NULL ? "" : options.name_deprecated->c_str(),
29 options.size); 29 options.size);
30 shm_ = SharedMemoryHandle::ImportHandle(fd); 30 shm_ = SharedMemoryHandle::ImportHandle(fd, options.size);
31 if (!shm_.IsValid()) { 31 if (!shm_.IsValid()) {
32 DLOG(ERROR) << "Shared memory creation failed"; 32 DLOG(ERROR) << "Shared memory creation failed";
33 return false; 33 return false;
34 } 34 }
35 35
36 int err = ashmem_set_prot_region(shm_.GetHandle(), 36 int err = ashmem_set_prot_region(shm_.GetHandle(),
37 PROT_READ | PROT_WRITE | PROT_EXEC); 37 PROT_READ | PROT_WRITE | PROT_EXEC);
38 if (err < 0) { 38 if (err < 0) {
39 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem"; 39 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem";
40 return false; 40 return false;
41 } 41 }
42 42
43 // Android doesn't appear to have a way to drop write access on an ashmem 43 // Android doesn't appear to have a way to drop write access on an ashmem
44 // segment for a single descriptor. http://crbug.com/320865 44 // segment for a single descriptor. http://crbug.com/320865
45 readonly_shm_ = SharedMemoryHandle( 45 readonly_shm_ =
46 base::FileDescriptor(dup(shm_.GetHandle()), false), shm_.GetGUID()); 46 SharedMemoryHandle(base::FileDescriptor(dup(shm_.GetHandle()), false),
Nico 2017/05/15 22:13:36 Maybe SharedMemoryHandle should have a ReadonlyDup
erikchen 2017/05/15 22:57:18 Actually, the GetReadOnlyHandle method is on Share
Nico 2017/05/15 23:11:55 I mean, this here and a few other places in this C
47 shm_.GetSize(), shm_.GetGUID());
47 if (!readonly_shm_.IsValid()) { 48 if (!readonly_shm_.IsValid()) {
48 DPLOG(ERROR) << "dup() failed"; 49 DPLOG(ERROR) << "dup() failed";
49 return false; 50 return false;
50 } 51 }
51 52
52 requested_size_ = options.size; 53 requested_size_ = options.size;
53 54
54 return true; 55 return true;
55 } 56 }
56 57
57 bool SharedMemory::Delete(const std::string& name) { 58 bool SharedMemory::Delete(const std::string& name) {
58 // Like on Windows, this is intentionally returning true as ashmem will 59 // Like on Windows, this is intentionally returning true as ashmem will
59 // automatically releases the resource when all FDs on it are closed. 60 // automatically releases the resource when all FDs on it are closed.
60 return true; 61 return true;
61 } 62 }
62 63
63 bool SharedMemory::Open(const std::string& name, bool read_only) { 64 bool SharedMemory::Open(const std::string& name, bool read_only) {
64 // ashmem doesn't support name mapping 65 // ashmem doesn't support name mapping
65 NOTIMPLEMENTED(); 66 NOTIMPLEMENTED();
66 return false; 67 return false;
67 } 68 }
68 69
69 } // namespace base 70 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698