Chromium Code Reviews| 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/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::CreateNamed(const std::string& name, | |
| 20 bool open_existing, uint32 size) { | |
| 21 DCHECK_EQ(-1, mapped_file_ ); | |
| 22 | |
| 23 // "name" is just a label in ashmem. It is visible in /proc/pid/maps. | |
| 24 mapped_file_ = ashmem_create_region(name.c_str(), size); | |
| 25 if (-1 == mapped_file_) { | |
| 26 DLOG(ERROR) << "Shared memory creation failed"; | |
| 27 return false; | |
| 28 } | |
| 29 | |
| 30 int err = ashmem_set_prot_region(mapped_file_, | |
| 31 PROT_READ | PROT_WRITE | PROT_EXEC); | |
| 32 if (err < 0) { | |
| 33 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem"; | |
| 34 return false; | |
| 35 } | |
| 36 created_size_ = size; | |
| 37 | |
| 38 return true; | |
| 39 } | |
| 40 | |
| 41 bool SharedMemory::Delete(const std::string& name) { | |
| 42 // ashmem doesn't support name mapping | |
| 43 NOTIMPLEMENTED(); | |
| 44 return false; | |
| 45 } | |
| 46 | |
| 47 bool SharedMemory::Open(const std::string& name, bool read_only) { | |
| 48 // ashmem doesn't support name mapping | |
| 49 NOTIMPLEMENTED(); | |
| 50 return false; | |
| 51 } | |
| 52 | |
| 53 int SharedMemory::GetAshmenSizeRegion() { | |
| 54 return ashmem_get_size_region(mapped_file_); | |
| 55 } | |
| 56 } // namespace base | |
|
brettw
2011/06/20 03:50:40
Nit: extra blank line before this.
michaelbai
2011/06/20 22:50:36
Done.
| |
| OLD | NEW |