Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
| 16 #include "base/safe_strerror_posix.h" | 16 #include "base/safe_strerror_posix.h" |
| 17 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 | 19 |
| 20 #if defined(OS_MACOSX) | 20 #if defined(OS_MACOSX) |
| 21 #include "base/mac/foundation_util.h" | 21 #include "base/mac/foundation_util.h" |
| 22 #endif // OS_MACOSX | 22 #endif // OS_MACOSX |
| 23 | 23 |
| 24 namespace base { | 24 namespace base { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | |
| 27 // Paranoia. Semaphores and shared memory segments should live in different | 28 // Paranoia. Semaphores and shared memory segments should live in different |
| 28 // namespaces, but who knows what's out there. | 29 // namespaces, but who knows what's out there. |
| 29 const char kSemaphoreSuffix[] = "-sem"; | 30 const char kSemaphoreSuffix[] = "-sem"; |
| 31 | |
| 32 #if defined(OS_ANDROID) | |
| 33 // The lockf() function is not available on Android; we translate to flock(). | |
|
darin (slow to review)
2011/06/20 18:44:01
hmm... will this be needed elsewhere? i was about
michaelbai
2011/06/20 22:50:36
Added os_compat_andorid.*
| |
| 34 #define F_LOCK LOCK_EX | |
| 35 #define F_ULOCK LOCK_UN | |
| 36 inline int lockf(int fd, int cmd, off_t ignored_len) { | |
| 37 return flock(fd, cmd); | |
| 30 } | 38 } |
| 39 #endif | |
| 40 | |
| 41 } // namespace. | |
| 31 | 42 |
| 32 SharedMemory::SharedMemory() | 43 SharedMemory::SharedMemory() |
| 33 : mapped_file_(-1), | 44 : mapped_file_(-1), |
| 34 mapped_size_(0), | 45 mapped_size_(0), |
| 35 inode_(0), | 46 inode_(0), |
| 36 memory_(NULL), | 47 memory_(NULL), |
| 37 read_only_(false), | 48 read_only_(false), |
| 38 created_size_(0) { | 49 created_size_(0) { |
| 39 } | 50 } |
| 40 | 51 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 } | 99 } |
| 89 | 100 |
| 90 bool SharedMemory::CreateAndMapAnonymous(uint32 size) { | 101 bool SharedMemory::CreateAndMapAnonymous(uint32 size) { |
| 91 return CreateAnonymous(size) && Map(size); | 102 return CreateAnonymous(size) && Map(size); |
| 92 } | 103 } |
| 93 | 104 |
| 94 bool SharedMemory::CreateAnonymous(uint32 size) { | 105 bool SharedMemory::CreateAnonymous(uint32 size) { |
| 95 return CreateNamed("", false, size); | 106 return CreateNamed("", false, size); |
| 96 } | 107 } |
| 97 | 108 |
| 109 #if !defined(OS_ANDROID) | |
| 98 // Chromium mostly only uses the unique/private shmem as specified by | 110 // Chromium mostly only uses the unique/private shmem as specified by |
| 99 // "name == L"". The exception is in the StatsTable. | 111 // "name == L"". The exception is in the StatsTable. |
| 100 // TODO(jrg): there is no way to "clean up" all unused named shmem if | 112 // TODO(jrg): there is no way to "clean up" all unused named shmem if |
| 101 // we restart from a crash. (That isn't a new problem, but it is a problem.) | 113 // we restart from a crash. (That isn't a new problem, but it is a problem.) |
| 102 // In case we want to delete it later, it may be useful to save the value | 114 // In case we want to delete it later, it may be useful to save the value |
| 103 // of mem_filename after FilePathForMemoryName(). | 115 // of mem_filename after FilePathForMemoryName(). |
| 104 bool SharedMemory::CreateNamed(const std::string& name, | 116 bool SharedMemory::CreateNamed(const std::string& name, |
| 105 bool open_existing, uint32 size) { | 117 bool open_existing, uint32 size) { |
| 106 DCHECK_EQ(-1, mapped_file_); | 118 DCHECK_EQ(-1, mapped_file_); |
| 107 if (size == 0) return false; | 119 if (size == 0) return false; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 if (!FilePathForMemoryName(name, &path)) | 206 if (!FilePathForMemoryName(name, &path)) |
| 195 return false; | 207 return false; |
| 196 | 208 |
| 197 read_only_ = read_only; | 209 read_only_ = read_only; |
| 198 | 210 |
| 199 const char *mode = read_only ? "r" : "r+"; | 211 const char *mode = read_only ? "r" : "r+"; |
| 200 FILE *fp = file_util::OpenFile(path, mode); | 212 FILE *fp = file_util::OpenFile(path, mode); |
| 201 return PrepareMapFile(fp); | 213 return PrepareMapFile(fp); |
| 202 } | 214 } |
| 203 | 215 |
| 216 #endif // !defined(OS_ANDROID) | |
| 217 | |
| 204 bool SharedMemory::Map(uint32 bytes) { | 218 bool SharedMemory::Map(uint32 bytes) { |
| 205 if (mapped_file_ == -1) | 219 if (mapped_file_ == -1) |
| 206 return false; | 220 return false; |
| 207 | 221 |
| 222 #if defined(OS_ANDROID) | |
| 223 if (bytes == 0) { | |
| 224 int ashmem_bytes = GetAshmenSizeRegion(); | |
| 225 if (ashmem_bytes < 0) | |
| 226 return false; | |
| 227 | |
| 228 DCHECK_GE((uint32)ashmem_bytes, bytes); | |
| 229 // The caller wants to determine the map region size from ashmem. | |
| 230 bytes = ashmem_bytes; | |
| 231 // TODO(port): we set the created size here so that it is available in | |
| 232 // transport_dib_android.cc. We should use | |
| 233 // SharedMemory::GetAshmenSizeRegion() in TransportDIB::Map(). | |
| 234 created_size_ = bytes; | |
| 235 } | |
| 236 #endif | |
| 237 | |
| 208 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | 238 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
| 209 MAP_SHARED, mapped_file_, 0); | 239 MAP_SHARED, mapped_file_, 0); |
| 210 | 240 |
| 211 if (memory_) | 241 if (memory_) |
| 212 mapped_size_ = bytes; | 242 mapped_size_ = bytes; |
| 213 | 243 |
| 214 bool mmap_succeeded = (memory_ != (void*)-1); | 244 bool mmap_succeeded = (memory_ != (void*)-1); |
| 215 DCHECK(mmap_succeeded) << "Call to mmap failed, errno=" << errno; | 245 DCHECK(mmap_succeeded) << "Call to mmap failed, errno=" << errno; |
| 216 return mmap_succeeded; | 246 return mmap_succeeded; |
| 217 } | 247 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 241 } | 271 } |
| 242 | 272 |
| 243 void SharedMemory::Lock() { | 273 void SharedMemory::Lock() { |
| 244 LockOrUnlockCommon(F_LOCK); | 274 LockOrUnlockCommon(F_LOCK); |
| 245 } | 275 } |
| 246 | 276 |
| 247 void SharedMemory::Unlock() { | 277 void SharedMemory::Unlock() { |
| 248 LockOrUnlockCommon(F_ULOCK); | 278 LockOrUnlockCommon(F_ULOCK); |
| 249 } | 279 } |
| 250 | 280 |
| 281 #if !defined(OS_ANDROID) | |
| 251 bool SharedMemory::PrepareMapFile(FILE *fp) { | 282 bool SharedMemory::PrepareMapFile(FILE *fp) { |
| 252 DCHECK_EQ(-1, mapped_file_); | 283 DCHECK_EQ(-1, mapped_file_); |
| 253 if (fp == NULL) return false; | 284 if (fp == NULL) return false; |
| 254 | 285 |
| 255 // This function theoretically can block on the disk, but realistically | 286 // This function theoretically can block on the disk, but realistically |
| 256 // the temporary files we create will just go into the buffer cache | 287 // the temporary files we create will just go into the buffer cache |
| 257 // and be deleted before they ever make it out to disk. | 288 // and be deleted before they ever make it out to disk. |
| 258 base::ThreadRestrictions::ScopedAllowIO allow_io; | 289 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 259 | 290 |
| 260 file_util::ScopedFILE file_closer(fp); | 291 file_util::ScopedFILE file_closer(fp); |
| 261 | 292 |
| 262 mapped_file_ = dup(fileno(fp)); | 293 mapped_file_ = dup(fileno(fp)); |
| 263 if (mapped_file_ == -1) { | 294 if (mapped_file_ == -1) { |
| 264 if (errno == EMFILE) { | 295 if (errno == EMFILE) { |
| 265 LOG(WARNING) << "Shared memory creation failed; out of file descriptors"; | 296 LOG(WARNING) << "Shared memory creation failed; out of file descriptors"; |
| 266 return false; | 297 return false; |
| 267 } else { | 298 } else { |
| 268 NOTREACHED() << "Call to dup failed, errno=" << errno; | 299 NOTREACHED() << "Call to dup failed, errno=" << errno; |
| 269 } | 300 } |
| 270 } | 301 } |
| 271 | 302 |
| 272 struct stat st; | 303 struct stat st; |
| 273 if (fstat(mapped_file_, &st)) | 304 if (fstat(mapped_file_, &st)) |
| 274 NOTREACHED(); | 305 NOTREACHED(); |
| 275 inode_ = st.st_ino; | 306 inode_ = st.st_ino; |
| 276 | 307 |
| 277 return true; | 308 return true; |
| 278 } | 309 } |
| 310 #endif | |
| 279 | 311 |
| 280 // For the given shmem named |mem_name|, return a filename to mmap() | 312 // For the given shmem named |mem_name|, return a filename to mmap() |
| 281 // (and possibly create). Modifies |filename|. Return false on | 313 // (and possibly create). Modifies |filename|. Return false on |
| 282 // error, or true of we are happy. | 314 // error, or true of we are happy. |
| 283 bool SharedMemory::FilePathForMemoryName(const std::string& mem_name, | 315 bool SharedMemory::FilePathForMemoryName(const std::string& mem_name, |
| 284 FilePath* path) { | 316 FilePath* path) { |
| 285 // mem_name will be used for a filename; make sure it doesn't | 317 // mem_name will be used for a filename; make sure it doesn't |
| 286 // contain anything which will confuse us. | 318 // contain anything which will confuse us. |
| 287 DCHECK_EQ(std::string::npos, mem_name.find('/')); | 319 DCHECK_EQ(std::string::npos, mem_name.find('/')); |
| 288 DCHECK_EQ(std::string::npos, mem_name.find('\0')); | 320 DCHECK_EQ(std::string::npos, mem_name.find('\0')); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 331 new_handle->fd = new_fd; | 363 new_handle->fd = new_fd; |
| 332 new_handle->auto_close = true; | 364 new_handle->auto_close = true; |
| 333 | 365 |
| 334 if (close_self) | 366 if (close_self) |
| 335 Close(); | 367 Close(); |
| 336 | 368 |
| 337 return true; | 369 return true; |
| 338 } | 370 } |
| 339 | 371 |
| 340 } // namespace base | 372 } // namespace base |
| OLD | NEW |