| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> | 10 #include <sys/stat.h> |
| 11 #include <unistd.h> | 11 #include <unistd.h> |
| 12 | 12 |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/platform_thread.h" | 15 #include "base/platform_thread.h" |
| 16 #include "base/safe_strerror_posix.h" | 16 #include "base/safe_strerror_posix.h" |
| 17 #include "base/thread_restrictions.h" |
| 17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 // Paranoia. Semaphores and shared memory segments should live in different | 23 // Paranoia. Semaphores and shared memory segments should live in different |
| 23 // namespaces, but who knows what's out there. | 24 // namespaces, but who knows what's out there. |
| 24 const char kSemaphoreSuffix[] = "-sem"; | 25 const char kSemaphoreSuffix[] = "-sem"; |
| 25 } | 26 } |
| 26 | 27 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 // Chromium mostly only use the unique/private shmem as specified by | 140 // Chromium mostly only use the unique/private shmem as specified by |
| 140 // "name == L"". The exception is in the StatsTable. | 141 // "name == L"". The exception is in the StatsTable. |
| 141 // TODO(jrg): there is no way to "clean up" all unused named shmem if | 142 // TODO(jrg): there is no way to "clean up" all unused named shmem if |
| 142 // we restart from a crash. (That isn't a new problem, but it is a problem.) | 143 // we restart from a crash. (That isn't a new problem, but it is a problem.) |
| 143 // In case we want to delete it later, it may be useful to save the value | 144 // In case we want to delete it later, it may be useful to save the value |
| 144 // of mem_filename after FilePathForMemoryName(). | 145 // of mem_filename after FilePathForMemoryName(). |
| 145 bool SharedMemory::CreateOrOpen(const std::string& name, | 146 bool SharedMemory::CreateOrOpen(const std::string& name, |
| 146 int posix_flags, uint32 size) { | 147 int posix_flags, uint32 size) { |
| 147 DCHECK(mapped_file_ == -1); | 148 DCHECK(mapped_file_ == -1); |
| 148 | 149 |
| 150 // This function theoretically can block on the disk, but realistically |
| 151 // the temporary files we create will just go into the buffer cache |
| 152 // and be deleted before they ever make it out to disk. |
| 153 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 154 |
| 149 file_util::ScopedFILE file_closer; | 155 file_util::ScopedFILE file_closer; |
| 150 FILE *fp; | 156 FILE *fp; |
| 151 | 157 |
| 152 FilePath path; | 158 FilePath path; |
| 153 if (name.empty()) { | 159 if (name.empty()) { |
| 154 // It doesn't make sense to have a read-only private piece of shmem | 160 // It doesn't make sense to have a read-only private piece of shmem |
| 155 DCHECK(posix_flags & (O_RDWR | O_WRONLY)); | 161 DCHECK(posix_flags & (O_RDWR | O_WRONLY)); |
| 156 | 162 |
| 157 // Q: Why not use the shm_open() etc. APIs? | 163 // Q: Why not use the shm_open() etc. APIs? |
| 158 // A: Because they're limited to 4mb on OS X. FFFFFFFUUUUUUUUUUU | 164 // A: Because they're limited to 4mb on OS X. FFFFFFFUUUUUUUUUUU |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 | 321 |
| 316 void SharedMemory::Unlock() { | 322 void SharedMemory::Unlock() { |
| 317 LockOrUnlockCommon(F_ULOCK); | 323 LockOrUnlockCommon(F_ULOCK); |
| 318 } | 324 } |
| 319 | 325 |
| 320 SharedMemoryHandle SharedMemory::handle() const { | 326 SharedMemoryHandle SharedMemory::handle() const { |
| 321 return FileDescriptor(mapped_file_, false); | 327 return FileDescriptor(mapped_file_, false); |
| 322 } | 328 } |
| 323 | 329 |
| 324 } // namespace base | 330 } // namespace base |
| OLD | NEW |