Index: base/shared_memory_posix.cc |
diff --git a/base/shared_memory_posix.cc b/base/shared_memory_posix.cc |
index a44581e4849f510f775cc09da2be4c9559f6ee91..843322b69801817458577ed7eaa92871497ff0d9 100644 |
--- a/base/shared_memory_posix.cc |
+++ b/base/shared_memory_posix.cc |
@@ -196,24 +196,52 @@ bool SharedMemory::Open(const std::string& name, bool read_only) { |
return PrepareMapFile(fp); |
} |
-// For the given shmem named |mem_name|, return a filename to mmap() |
-// (and possibly create). Modifies |filename|. Return false on |
-// error, or true of we are happy. |
-bool SharedMemory::FilePathForMemoryName(const std::string& mem_name, |
- FilePath* path) { |
- // mem_name will be used for a filename; make sure it doesn't |
- // contain anything which will confuse us. |
- DCHECK(mem_name.find('/') == std::string::npos); |
- DCHECK(mem_name.find('\0') == std::string::npos); |
+bool SharedMemory::Map(uint32 bytes) { |
+ if (mapped_file_ == -1) |
+ return false; |
- FilePath temp_dir; |
- if (!file_util::GetShmemTempDir(&temp_dir)) |
+ memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
+ MAP_SHARED, mapped_file_, 0); |
+ |
+ if (memory_) |
+ mapped_size_ = bytes; |
+ |
+ bool mmap_succeeded = (memory_ != (void*)-1); |
+ DCHECK(mmap_succeeded) << "Call to mmap failed, errno=" << errno; |
+ return mmap_succeeded; |
+} |
+ |
+bool SharedMemory::Unmap() { |
+ if (memory_ == NULL) |
return false; |
- *path = temp_dir.AppendASCII("com.google.chrome.shmem." + mem_name); |
+ munmap(memory_, mapped_size_); |
+ memory_ = NULL; |
+ mapped_size_ = 0; |
return true; |
} |
+SharedMemoryHandle SharedMemory::handle() const { |
+ return FileDescriptor(mapped_file_, false); |
+} |
+ |
+void SharedMemory::Close() { |
+ Unmap(); |
+ |
+ if (mapped_file_ > 0) { |
+ close(mapped_file_); |
+ mapped_file_ = -1; |
+ } |
+} |
+ |
+void SharedMemory::Lock() { |
+ LockOrUnlockCommon(F_LOCK); |
+} |
+ |
+void SharedMemory::Unlock() { |
+ LockOrUnlockCommon(F_ULOCK); |
+} |
+ |
bool SharedMemory::PrepareMapFile(FILE *fp) { |
DCHECK(mapped_file_ == -1); |
if (fp == NULL) return false; |
@@ -243,55 +271,24 @@ bool SharedMemory::PrepareMapFile(FILE *fp) { |
return true; |
} |
-bool SharedMemory::Map(uint32 bytes) { |
- if (mapped_file_ == -1) |
- return false; |
- |
- memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
- MAP_SHARED, mapped_file_, 0); |
- |
- if (memory_) |
- mapped_size_ = bytes; |
- |
- bool mmap_succeeded = (memory_ != (void*)-1); |
- DCHECK(mmap_succeeded) << "Call to mmap failed, errno=" << errno; |
- return mmap_succeeded; |
-} |
+// For the given shmem named |mem_name|, return a filename to mmap() |
+// (and possibly create). Modifies |filename|. Return false on |
+// error, or true of we are happy. |
+bool SharedMemory::FilePathForMemoryName(const std::string& mem_name, |
+ FilePath* path) { |
+ // mem_name will be used for a filename; make sure it doesn't |
+ // contain anything which will confuse us. |
+ DCHECK(mem_name.find('/') == std::string::npos); |
+ DCHECK(mem_name.find('\0') == std::string::npos); |
-bool SharedMemory::Unmap() { |
- if (memory_ == NULL) |
+ FilePath temp_dir; |
+ if (!file_util::GetShmemTempDir(&temp_dir)) |
return false; |
- munmap(memory_, mapped_size_); |
- memory_ = NULL; |
- mapped_size_ = 0; |
- return true; |
-} |
- |
-bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
- SharedMemoryHandle *new_handle, |
- bool close_self) { |
- const int new_fd = dup(mapped_file_); |
- DCHECK(new_fd >= 0); |
- new_handle->fd = new_fd; |
- new_handle->auto_close = true; |
- |
- if (close_self) |
- Close(); |
- |
+ *path = temp_dir.AppendASCII("com.google.chrome.shmem." + mem_name); |
return true; |
} |
- |
-void SharedMemory::Close() { |
- Unmap(); |
- |
- if (mapped_file_ > 0) { |
- close(mapped_file_); |
- mapped_file_ = -1; |
- } |
-} |
- |
void SharedMemory::LockOrUnlockCommon(int function) { |
DCHECK(mapped_file_ >= 0); |
while (lockf(mapped_file_, function, 0) < 0) { |
@@ -311,16 +308,18 @@ void SharedMemory::LockOrUnlockCommon(int function) { |
} |
} |
-void SharedMemory::Lock() { |
- LockOrUnlockCommon(F_LOCK); |
-} |
+bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
+ SharedMemoryHandle *new_handle, |
+ bool close_self) { |
+ const int new_fd = dup(mapped_file_); |
+ DCHECK(new_fd >= 0); |
+ new_handle->fd = new_fd; |
+ new_handle->auto_close = true; |
-void SharedMemory::Unlock() { |
- LockOrUnlockCommon(F_ULOCK); |
-} |
+ if (close_self) |
+ Close(); |
-SharedMemoryHandle SharedMemory::handle() const { |
- return FileDescriptor(mapped_file_, false); |
+ return true; |
} |
} // namespace base |