Index: base/memory/shared_memory_mac.cc |
diff --git a/base/memory/shared_memory_posix.cc b/base/memory/shared_memory_mac.cc |
similarity index 87% |
copy from base/memory/shared_memory_posix.cc |
copy to base/memory/shared_memory_mac.cc |
index 8aaa9ce443890f64140c06cb9e0a4b410bdf711d..0ec8b8544fa5c3abc19ea639868126cc3030c536 100644 |
--- a/base/memory/shared_memory_posix.cc |
+++ b/base/memory/shared_memory_mac.cc |
@@ -23,11 +23,6 @@ |
#include "base/mac/foundation_util.h" |
#endif // OS_MACOSX |
-#if defined(OS_ANDROID) |
-#include "base/os_compat_android.h" |
-#include "third_party/ashmem/ashmem.h" |
-#endif |
- |
namespace base { |
namespace { |
@@ -49,7 +44,6 @@ struct ScopedPathUnlinkerTraits { |
// Unlinks the FilePath when the object is destroyed. |
typedef ScopedGeneric<FilePath*, ScopedPathUnlinkerTraits> ScopedPathUnlinker; |
-#if !defined(OS_ANDROID) |
// Makes a temporary file, fdopens it, and then unlinks it. |fp| is populated |
// with the fdopened FILE. |readonly_fd| is populated with the opened fd if |
// options.share_read_only is true. |path| is populated with the location of |
@@ -98,7 +92,6 @@ bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options, |
} |
return true; |
} |
-#endif // !defined(OS_ANDROID) |
} |
SharedMemory::SharedMemory() |
@@ -110,8 +103,8 @@ SharedMemory::SharedMemory() |
requested_size_(0) { |
} |
-SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
- : mapped_file_(handle.fd), |
+SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) |
+ : mapped_file_(GetFdFromSharedMemoryHandle(handle)), |
readonly_mapped_file_(-1), |
mapped_size_(0), |
memory_(NULL), |
@@ -119,9 +112,10 @@ SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
requested_size_(0) { |
} |
-SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
+SharedMemory::SharedMemory(const SharedMemoryHandle& handle, |
+ bool read_only, |
ProcessHandle process) |
- : mapped_file_(handle.fd), |
+ : mapped_file_(GetFdFromSharedMemoryHandle(handle)), |
readonly_mapped_file_(-1), |
mapped_size_(0), |
memory_(NULL), |
@@ -139,7 +133,7 @@ SharedMemory::~SharedMemory() { |
// static |
bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
- return handle.fd >= 0; |
+ return handle.IsValid(); |
} |
// static |
@@ -149,8 +143,8 @@ SharedMemoryHandle SharedMemory::NULLHandle() { |
// static |
void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
- DCHECK_GE(handle.fd, 0); |
- if (close(handle.fd) < 0) |
+ DCHECK_GE(GetFdFromSharedMemoryHandle(handle), 0); |
+ if (close(GetFdFromSharedMemoryHandle(handle)) < 0) |
DPLOG(ERROR) << "close"; |
} |
@@ -162,28 +156,24 @@ size_t SharedMemory::GetHandleLimit() { |
// static |
SharedMemoryHandle SharedMemory::DuplicateHandle( |
const SharedMemoryHandle& handle) { |
- int duped_handle = HANDLE_EINTR(dup(handle.fd)); |
- if (duped_handle < 0) |
- return base::SharedMemory::NULLHandle(); |
- return base::FileDescriptor(duped_handle, true); |
+ return handle.Duplicate(); |
} |
// static |
int SharedMemory::GetFdFromSharedMemoryHandle( |
const SharedMemoryHandle& handle) { |
- return handle.fd; |
+ return handle.GetFileDescriptor().fd; |
} |
bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
return CreateAnonymous(size) && Map(size); |
} |
-#if !defined(OS_ANDROID) |
// static |
int SharedMemory::GetSizeFromSharedMemoryHandle( |
const SharedMemoryHandle& handle) { |
struct stat st; |
- if (fstat(handle.fd, &st) != 0) |
+ if (fstat(GetFdFromSharedMemoryHandle(handle), &st) != 0) |
return -1; |
return st.st_size; |
} |
@@ -290,19 +280,7 @@ bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
requested_size_ = options.size; |
} |
if (fp == NULL) { |
-#if !defined(OS_MACOSX) |
- PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; |
- FilePath dir = path.DirName(); |
- if (access(dir.value().c_str(), W_OK | X_OK) < 0) { |
- PLOG(ERROR) << "Unable to access(W_OK|X_OK) " << dir.value(); |
- if (dir.value() == "/dev/shm") { |
- LOG(FATAL) << "This is frequently caused by incorrect permissions on " |
- << "/dev/shm. Try 'sudo chmod 1777 /dev/shm' to fix."; |
- } |
- } |
-#else |
PLOG(ERROR) << "Creating shared memory in " << path.value() << " failed"; |
-#endif |
return false; |
} |
@@ -340,7 +318,6 @@ bool SharedMemory::Open(const std::string& name, bool read_only) { |
} |
return PrepareMapFile(fp.Pass(), readonly_fd.Pass()); |
} |
-#endif // !defined(OS_ANDROID) |
bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
if (mapped_file_ == -1) |
@@ -352,18 +329,6 @@ bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
if (memory_) |
return false; |
-#if defined(OS_ANDROID) |
- // On Android, Map can be called with a size and offset of zero to use the |
- // ashmem-determined size. |
- if (bytes == 0) { |
- DCHECK_EQ(0, offset); |
- int ashmem_bytes = ashmem_get_size_region(mapped_file_); |
- if (ashmem_bytes < 0) |
- return false; |
- bytes = ashmem_bytes; |
- } |
-#endif |
- |
memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
MAP_SHARED, mapped_file_, offset); |
@@ -390,7 +355,7 @@ bool SharedMemory::Unmap() { |
} |
SharedMemoryHandle SharedMemory::handle() const { |
- return FileDescriptor(mapped_file_, false); |
+ return SharedMemoryHandle(mapped_file_, false); |
} |
void SharedMemory::Close() { |
@@ -406,7 +371,6 @@ void SharedMemory::Close() { |
} |
} |
-#if !defined(OS_ANDROID) |
bool SharedMemory::PrepareMapFile(ScopedFILE fp, ScopedFD readonly_fd) { |
DCHECK_EQ(-1, mapped_file_); |
DCHECK_EQ(-1, readonly_mapped_file_); |
@@ -459,19 +423,10 @@ bool SharedMemory::FilePathForMemoryName(const std::string& mem_name, |
if (!GetShmemTempDir(false, &temp_dir)) |
return false; |
-#if !defined(OS_MACOSX) |
-#if defined(GOOGLE_CHROME_BUILD) |
- std::string name_base = std::string("com.google.Chrome"); |
-#else |
- std::string name_base = std::string("org.chromium.Chromium"); |
-#endif |
-#else // OS_MACOSX |
std::string name_base = std::string(base::mac::BaseBundleID()); |
-#endif // OS_MACOSX |
*path = temp_dir.AppendASCII(name_base + ".shmem." + mem_name); |
return true; |
} |
-#endif // !defined(OS_ANDROID) |
bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
SharedMemoryHandle* new_handle, |
@@ -496,8 +451,7 @@ bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
return false; |
} |
- new_handle->fd = new_fd; |
- new_handle->auto_close = true; |
+ new_handle->SetFileHandle(new_fd, true); |
if (close_self) { |
Unmap(); |