Chromium Code Reviews| Index: base/memory/shared_memory_nacl.cc |
| diff --git a/base/memory/shared_memory_nacl.cc b/base/memory/shared_memory_nacl.cc |
| index 945fc61375a901187ced95dbc5ece779e479c4cf..e93e9ef1266fa69f3cd8e34d4609c5f72547ed4a 100644 |
| --- a/base/memory/shared_memory_nacl.cc |
| +++ b/base/memory/shared_memory_nacl.cc |
| @@ -18,20 +18,14 @@ |
| namespace base { |
| SharedMemory::SharedMemory() |
| - : mapped_file_(-1), |
| - mapped_size_(0), |
| - memory_(NULL), |
| - read_only_(false), |
| - requested_size_(0) { |
| -} |
| + : mapped_size_(0), memory_(NULL), read_only_(false), requested_size_(0) {} |
| SharedMemory::SharedMemory(const SharedMemoryHandle& handle, bool read_only) |
| - : mapped_file_(handle.fd), |
| + : shm_(handle), |
| mapped_size_(0), |
| memory_(NULL), |
| read_only_(read_only), |
| - requested_size_(0) { |
| -} |
| + requested_size_(0) {} |
| SharedMemory::~SharedMemory() { |
| Unmap(); |
| @@ -40,7 +34,7 @@ SharedMemory::~SharedMemory() { |
| // static |
| bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
| - return handle.fd >= 0; |
| + return handle.IsValid(); |
| } |
| // static |
| @@ -50,18 +44,14 @@ SharedMemoryHandle SharedMemory::NULLHandle() { |
| // static |
| void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
| - DCHECK_GE(handle.fd, 0); |
| - if (close(handle.fd) < 0) |
| - DPLOG(ERROR) << "close"; |
| + DCHECK(handle.IsValid()); |
| + handle.Close(); |
| } |
| // 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(); |
| } |
| bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
| @@ -83,7 +73,7 @@ bool SharedMemory::Open(const std::string& name, bool read_only) { |
| } |
| bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
| - if (mapped_file_ == -1) |
| + if (!shm_.IsValid()) |
| return false; |
| if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
| @@ -93,7 +83,7 @@ bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
| return false; |
| memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
| - MAP_SHARED, mapped_file_, offset); |
| + MAP_SHARED, shm_.file_descriptor.fd, offset); |
| bool mmap_succeeded = memory_ != MAP_FAILED && memory_ != NULL; |
| if (mmap_succeeded) { |
| @@ -119,22 +109,24 @@ bool SharedMemory::Unmap() { |
| } |
| SharedMemoryHandle SharedMemory::handle() const { |
| - return FileDescriptor(mapped_file_, false); |
| + SharedMemoryHandle handle_copy = shm_; |
| + handle_copy.file_descriptor.auto_close = false; |
| + return handle_copy; |
| } |
| SharedMemoryHandle SharedMemory::TakeHandle() { |
| - FileDescriptor handle(mapped_file_, true); |
| - mapped_file_ = -1; |
| + SharedMemoryHandle handle_copy = shm_; |
| + handle_copy.file_descriptor.auto_close = true; |
|
Nico
2017/04/27 16:24:44
Isn't this the default?
I'm actually not sure, I
erikchen
2017/04/27 17:08:10
It doesn't matter what the default is [which can c
|
| + shm_ = SharedMemoryHandle(); |
| memory_ = nullptr; |
| mapped_size_ = 0; |
| - return handle; |
| + return handle_copy; |
| } |
| void SharedMemory::Close() { |
| - if (mapped_file_ > 0) { |
| - if (close(mapped_file_) < 0) |
| - DPLOG(ERROR) << "close"; |
| - mapped_file_ = -1; |
| + if (shm_.IsValid()) { |
| + shm_.Close(); |
| + shm_ = SharedMemoryHandle(); |
| } |
| } |
| @@ -147,20 +139,14 @@ bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
| // drop permissions. |
| return false; |
| } |
| - const int new_fd = dup(mapped_file_); |
| - if (new_fd < 0) { |
| - DPLOG(ERROR) << "dup() failed."; |
| - return false; |
| - } |
| - new_handle->fd = new_fd; |
| - new_handle->auto_close = true; |
| + *new_handle = shm_.Duplicate(); |
| if (close_self) { |
| Unmap(); |
| Close(); |
| } |
| - return true; |
| + return new_handle->IsValid(); |
| } |
| } // namespace base |