| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
| 10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 : mapped_file_(handle.fd), | 39 : mapped_file_(handle.fd), |
| 40 inode_(0), | 40 inode_(0), |
| 41 mapped_size_(0), | 41 mapped_size_(0), |
| 42 memory_(NULL), | 42 memory_(NULL), |
| 43 read_only_(read_only), | 43 read_only_(read_only), |
| 44 requested_size_(0) { | 44 requested_size_(0) { |
| 45 NOTREACHED(); | 45 NOTREACHED(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 SharedMemory::~SharedMemory() { | 48 SharedMemory::~SharedMemory() { |
| 49 Unmap(); |
| 49 Close(); | 50 Close(); |
| 50 } | 51 } |
| 51 | 52 |
| 52 // static | 53 // static |
| 53 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { | 54 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
| 54 return handle.fd >= 0; | 55 return handle.fd >= 0; |
| 55 } | 56 } |
| 56 | 57 |
| 57 // static | 58 // static |
| 58 SharedMemoryHandle SharedMemory::NULLHandle() { | 59 SharedMemoryHandle SharedMemory::NULLHandle() { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 memory_ = NULL; | 119 memory_ = NULL; |
| 119 mapped_size_ = 0; | 120 mapped_size_ = 0; |
| 120 return true; | 121 return true; |
| 121 } | 122 } |
| 122 | 123 |
| 123 SharedMemoryHandle SharedMemory::handle() const { | 124 SharedMemoryHandle SharedMemory::handle() const { |
| 124 return FileDescriptor(mapped_file_, false); | 125 return FileDescriptor(mapped_file_, false); |
| 125 } | 126 } |
| 126 | 127 |
| 127 void SharedMemory::Close() { | 128 void SharedMemory::Close() { |
| 128 Unmap(); | |
| 129 if (mapped_file_ > 0) { | 129 if (mapped_file_ > 0) { |
| 130 if (close(mapped_file_) < 0) | 130 if (close(mapped_file_) < 0) |
| 131 DPLOG(ERROR) << "close"; | 131 DPLOG(ERROR) << "close"; |
| 132 mapped_file_ = -1; | 132 mapped_file_ = -1; |
| 133 } | 133 } |
| 134 } | 134 } |
| 135 | 135 |
| 136 void SharedMemory::LockDeprecated() { | 136 void SharedMemory::LockDeprecated() { |
| 137 NOTIMPLEMENTED(); | 137 NOTIMPLEMENTED(); |
| 138 } | 138 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 152 } | 152 } |
| 153 const int new_fd = dup(mapped_file_); | 153 const int new_fd = dup(mapped_file_); |
| 154 if (new_fd < 0) { | 154 if (new_fd < 0) { |
| 155 DPLOG(ERROR) << "dup() failed."; | 155 DPLOG(ERROR) << "dup() failed."; |
| 156 return false; | 156 return false; |
| 157 } | 157 } |
| 158 | 158 |
| 159 new_handle->fd = new_fd; | 159 new_handle->fd = new_fd; |
| 160 new_handle->auto_close = true; | 160 new_handle->auto_close = true; |
| 161 | 161 |
| 162 if (close_self) | 162 if (close_self) { |
| 163 Unmap(); |
| 163 Close(); | 164 Close(); |
| 165 } |
| 164 return true; | 166 return true; |
| 165 } | 167 } |
| 166 | 168 |
| 167 } // namespace base | 169 } // namespace base |
| OLD | NEW |