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 88% |
copy from base/memory/shared_memory_posix.cc |
copy to base/memory/shared_memory_mac.cc |
index 35d746e5394e0e042bbaac33421d94c8406e65e2..7f5e7289314b9e5a4d82f8731bf0d0b40a2b1f52 100644 |
--- a/base/memory/shared_memory_posix.cc |
+++ b/base/memory/shared_memory_mac.cc |
@@ -29,11 +29,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 { |
@@ -57,7 +52,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 |
@@ -106,7 +100,6 @@ bool CreateAnonymousSharedMemory(const SharedMemoryCreateOptions& options, |
} |
return true; |
} |
-#endif // !defined(OS_ANDROID) |
} |
SharedMemory::SharedMemory() |
@@ -118,8 +111,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), |
@@ -127,9 +120,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), |
@@ -147,7 +141,7 @@ SharedMemory::~SharedMemory() { |
// static |
bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
- return handle.fd >= 0; |
+ return GetFdFromSharedMemoryHandle(handle) >= 0; |
} |
// static |
@@ -157,8 +151,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"; |
} |
@@ -170,28 +164,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; |
} |
@@ -298,19 +288,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; |
} |
@@ -348,7 +326,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) |
@@ -360,18 +337,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); |
@@ -398,7 +363,7 @@ bool SharedMemory::Unmap() { |
} |
SharedMemoryHandle SharedMemory::handle() const { |
- return FileDescriptor(mapped_file_, false); |
+ return SharedMemoryHandle(FileDescriptor(mapped_file_, false)); |
} |
void SharedMemory::Close() { |
@@ -424,7 +389,6 @@ void SharedMemory::UnlockDeprecated() { |
g_thread_lock_.Get().Release(); |
} |
-#if !defined(OS_ANDROID) |
bool SharedMemory::PrepareMapFile(ScopedFILE fp, ScopedFD readonly_fd) { |
Nico
2015/06/17 04:03:21
There's a lot of code in this file that's just cop
erikchen
2015/06/17 17:38:55
I don't have a preview to show you, but pretty muc
|
DCHECK_EQ(-1, mapped_file_); |
DCHECK_EQ(-1, readonly_mapped_file_); |
@@ -477,19 +441,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) |
void SharedMemory::LockOrUnlockCommon(int function) { |
DCHECK_GE(mapped_file_, 0); |
@@ -533,8 +488,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(); |