| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/shared_memory.h" | 5 #include "base/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> |
| 11 #include <unistd.h> |
| 10 | 12 |
| 11 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 13 #include "base/platform_thread.h" | 15 #include "base/platform_thread.h" |
| 14 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 15 | 17 |
| 16 namespace base { | 18 namespace base { |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 // Paranoia. Semaphores and shared memory segments should live in different | 21 // Paranoia. Semaphores and shared memory segments should live in different |
| 20 // namespaces, but who knows what's out there. | 22 // namespaces, but who knows what's out there. |
| 21 const char kSemaphoreSuffix[] = "-sem"; | 23 const char kSemaphoreSuffix[] = "-sem"; |
| 22 } | 24 } |
| 23 | 25 |
| 24 SharedMemory::SharedMemory() | 26 SharedMemory::SharedMemory() |
| 25 : mapped_file_(-1), | 27 : mapped_file_(-1), |
| 28 inode_(0), |
| 26 memory_(NULL), | 29 memory_(NULL), |
| 27 read_only_(false), | 30 read_only_(false), |
| 28 max_size_(0) { | 31 max_size_(0) { |
| 29 } | 32 } |
| 30 | 33 |
| 31 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) | 34 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
| 32 : mapped_file_(handle.fd), | 35 : mapped_file_(handle.fd), |
| 36 inode_(0), |
| 33 memory_(NULL), | 37 memory_(NULL), |
| 34 read_only_(read_only), | 38 read_only_(read_only), |
| 35 max_size_(0) { | 39 max_size_(0) { |
| 40 struct stat st; |
| 41 if (fstat(handle.fd, &st) == 0) { |
| 42 // If fstat fails, then the file descriptor is invalid and we'll learn this |
| 43 // fact when Map() fails. |
| 44 inode_ = st.st_ino; |
| 45 } |
| 36 } | 46 } |
| 37 | 47 |
| 38 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, | 48 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
| 39 ProcessHandle process) | 49 ProcessHandle process) |
| 40 : mapped_file_(handle.fd), | 50 : mapped_file_(handle.fd), |
| 41 memory_(NULL), | 51 memory_(NULL), |
| 42 read_only_(read_only), | 52 read_only_(read_only), |
| 43 max_size_(0) { | 53 max_size_(0) { |
| 44 // We don't handle this case yet (note the ignored parameter); let's die if | 54 // We don't handle this case yet (note the ignored parameter); let's die if |
| 45 // someone comes calling. | 55 // someone comes calling. |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 // possibly shrink. | 209 // possibly shrink. |
| 200 if ((ftruncate(fileno(fp), size) != 0) || | 210 if ((ftruncate(fileno(fp), size) != 0) || |
| 201 (fflush(fp) != 0)) { | 211 (fflush(fp) != 0)) { |
| 202 return false; | 212 return false; |
| 203 } | 213 } |
| 204 } | 214 } |
| 205 } | 215 } |
| 206 | 216 |
| 207 mapped_file_ = dup(fileno(fp)); | 217 mapped_file_ = dup(fileno(fp)); |
| 208 DCHECK(mapped_file_ >= 0); | 218 DCHECK(mapped_file_ >= 0); |
| 219 |
| 220 struct stat st; |
| 221 if (fstat(mapped_file_, &st)) |
| 222 NOTREACHED(); |
| 223 inode_ = st.st_ino; |
| 224 |
| 209 return true; | 225 return true; |
| 210 } | 226 } |
| 211 | 227 |
| 212 bool SharedMemory::Map(size_t bytes) { | 228 bool SharedMemory::Map(size_t bytes) { |
| 213 if (mapped_file_ == -1) | 229 if (mapped_file_ == -1) |
| 214 return false; | 230 return false; |
| 215 | 231 |
| 216 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | 232 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
| 217 MAP_SHARED, mapped_file_, 0); | 233 MAP_SHARED, mapped_file_, 0); |
| 218 | 234 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 | 301 |
| 286 void SharedMemory::Unlock() { | 302 void SharedMemory::Unlock() { |
| 287 LockOrUnlockCommon(F_ULOCK); | 303 LockOrUnlockCommon(F_ULOCK); |
| 288 } | 304 } |
| 289 | 305 |
| 290 SharedMemoryHandle SharedMemory::handle() const { | 306 SharedMemoryHandle SharedMemory::handle() const { |
| 291 return FileDescriptor(mapped_file_, false); | 307 return FileDescriptor(mapped_file_, false); |
| 292 } | 308 } |
| 293 | 309 |
| 294 } // namespace base | 310 } // namespace base |
| OLD | NEW |