| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/memory/shared_memory.h" | |
| 6 | |
| 7 #include <sys/mman.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "third_party/ashmem/ashmem.h" | |
| 11 | |
| 12 namespace base { | |
| 13 | |
| 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 | |
| 16 // all the file descriptors from different processes associated with the region | |
| 17 // are closed, the memory buffer will go away. | |
| 18 | |
| 19 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | |
| 20 DCHECK_EQ(-1, mapped_file_ ); | |
| 21 | |
| 22 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) | |
| 23 return false; | |
| 24 | |
| 25 // "name" is just a label in ashmem. It is visible in /proc/pid/maps. | |
| 26 mapped_file_ = ashmem_create_region( | |
| 27 options.name_deprecated == NULL ? "" : options.name_deprecated->c_str(), | |
| 28 options.size); | |
| 29 if (-1 == mapped_file_) { | |
| 30 DLOG(ERROR) << "Shared memory creation failed"; | |
| 31 return false; | |
| 32 } | |
| 33 | |
| 34 int err = ashmem_set_prot_region(mapped_file_, | |
| 35 PROT_READ | PROT_WRITE | PROT_EXEC); | |
| 36 if (err < 0) { | |
| 37 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem"; | |
| 38 return false; | |
| 39 } | |
| 40 | |
| 41 // Android doesn't appear to have a way to drop write access on an ashmem | |
| 42 // segment for a single descriptor. http://crbug.com/320865 | |
| 43 readonly_mapped_file_ = dup(mapped_file_); | |
| 44 if (-1 == readonly_mapped_file_) { | |
| 45 DPLOG(ERROR) << "dup() failed"; | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 requested_size_ = options.size; | |
| 50 | |
| 51 return true; | |
| 52 } | |
| 53 | |
| 54 bool SharedMemory::Delete(const std::string& name) { | |
| 55 // Like on Windows, this is intentionally returning true as ashmem will | |
| 56 // automatically releases the resource when all FDs on it are closed. | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 bool SharedMemory::Open(const std::string& name, bool read_only) { | |
| 61 // ashmem doesn't support name mapping | |
| 62 NOTIMPLEMENTED(); | |
| 63 return false; | |
| 64 } | |
| 65 | |
| 66 } // namespace base | |
| OLD | NEW |