OLD | NEW |
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/shared_memory.h" | 5 #include "base/shared_memory.h" |
6 | 6 |
7 #include <sys/mman.h> | 7 #include <sys/mman.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "third_party/ashmem/ashmem.h" | 10 #include "third_party/ashmem/ashmem.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 13 |
14 // For Android, we use ashmem to implement SharedMemory. ashmem_create_region | 14 // For Android, we use ashmem to implement SharedMemory. ashmem_create_region |
15 // will automatically pin the region. We never explicitly call pin/unpin. When | 15 // will automatically pin the region. We never explicitly call pin/unpin. When |
16 // all the file descriptors from different processes associated with the region | 16 // all the file descriptors from different processes associated with the region |
17 // are closed, the memory buffer will go away. | 17 // are closed, the memory buffer will go away. |
18 | 18 |
19 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 19 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
20 DCHECK_EQ(-1, mapped_file_ ); | 20 DCHECK_EQ(-1, mapped_file_ ); |
21 | 21 |
22 // "name" is just a label in ashmem. It is visible in /proc/pid/maps. | 22 // "name" is just a label in ashmem. It is visible in /proc/pid/maps. |
23 mapped_file_ = ashmem_create_region( | 23 mapped_file_ = ashmem_create_region( |
24 options.name == NULL ? "" : options.name.c_str(), | 24 options.name == NULL ? "" : options.name->c_str(), |
25 options.size); | 25 options.size); |
26 if (-1 == mapped_file_) { | 26 if (-1 == mapped_file_) { |
27 DLOG(ERROR) << "Shared memory creation failed"; | 27 DLOG(ERROR) << "Shared memory creation failed"; |
28 return false; | 28 return false; |
29 } | 29 } |
30 | 30 |
31 int err = ashmem_set_prot_region(mapped_file_, | 31 int err = ashmem_set_prot_region(mapped_file_, |
32 PROT_READ | PROT_WRITE | PROT_EXEC); | 32 PROT_READ | PROT_WRITE | PROT_EXEC); |
33 if (err < 0) { | 33 if (err < 0) { |
34 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem"; | 34 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem"; |
35 return false; | 35 return false; |
36 } | 36 } |
37 created_size_ = size; | 37 created_size_ = options.size; |
38 | 38 |
39 return true; | 39 return true; |
40 } | 40 } |
41 | 41 |
42 bool SharedMemory::Delete(const std::string& name) { | 42 bool SharedMemory::Delete(const std::string& name) { |
43 // Like on Windows, this is intentionally returning true as ashmem will | 43 // Like on Windows, this is intentionally returning true as ashmem will |
44 // automatically releases the resource when all FDs on it are closed. | 44 // automatically releases the resource when all FDs on it are closed. |
45 return true; | 45 return true; |
46 } | 46 } |
47 | 47 |
48 bool SharedMemory::Open(const std::string& name, bool read_only) { | 48 bool SharedMemory::Open(const std::string& name, bool read_only) { |
49 // ashmem doesn't support name mapping | 49 // ashmem doesn't support name mapping |
50 NOTIMPLEMENTED(); | 50 NOTIMPLEMENTED(); |
51 return false; | 51 return false; |
52 } | 52 } |
53 | 53 |
54 } // namespace base | 54 } // namespace base |
OLD | NEW |