Chromium Code Reviews| 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/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/lazy_instance.h" | 14 #include "base/lazy_instance.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
| 17 #include "base/safe_strerror_posix.h" | 17 #include "base/safe_strerror_posix.h" |
| 18 #include "base/synchronization/lock.h" | 18 #include "base/synchronization/lock.h" |
| 19 #include "base/sys_info.h" | |
| 19 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" |
| 20 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 21 | 22 |
| 22 #if defined(OS_MACOSX) | 23 #if defined(OS_MACOSX) |
| 23 #include "base/mac/foundation_util.h" | 24 #include "base/mac/foundation_util.h" |
| 24 #endif // OS_MACOSX | 25 #endif // OS_MACOSX |
| 25 | 26 |
| 26 #if defined(OS_ANDROID) | 27 #if defined(OS_ANDROID) |
| 27 #include "base/os_compat_android.h" | 28 #include "base/os_compat_android.h" |
| 28 #include "third_party/ashmem/ashmem.h" | 29 #include "third_party/ashmem/ashmem.h" |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 | 213 |
| 213 read_only_ = read_only; | 214 read_only_ = read_only; |
| 214 | 215 |
| 215 const char *mode = read_only ? "r" : "r+"; | 216 const char *mode = read_only ? "r" : "r+"; |
| 216 FILE *fp = file_util::OpenFile(path, mode); | 217 FILE *fp = file_util::OpenFile(path, mode); |
| 217 return PrepareMapFile(fp); | 218 return PrepareMapFile(fp); |
| 218 } | 219 } |
| 219 | 220 |
| 220 #endif // !defined(OS_ANDROID) | 221 #endif // !defined(OS_ANDROID) |
| 221 | 222 |
| 222 bool SharedMemory::Map(size_t bytes) { | 223 bool SharedMemory::MapAt(off_t offset, size_t bytes) { |
| 223 if (mapped_file_ == -1) | 224 if (mapped_file_ == -1) |
| 224 return false; | 225 return false; |
| 225 | 226 |
| 226 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) | 227 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 227 return false; | 228 return false; |
| 228 | 229 |
| 229 #if defined(OS_ANDROID) | 230 #if defined(OS_ANDROID) |
| 230 if (bytes == 0) { | 231 if (bytes == 0) { |
| 231 int ashmem_bytes = ashmem_get_size_region(mapped_file_); | 232 int ashmem_bytes = ashmem_get_size_region(mapped_file_); |
| 232 if (ashmem_bytes < 0) | 233 if (ashmem_bytes < 0) |
| 233 return false; | 234 return false; |
| 234 | 235 |
| 235 DCHECK_GE(static_cast<uint32>(ashmem_bytes), bytes); | 236 DCHECK_GE(static_cast<uint32>(ashmem_bytes), bytes); |
| 236 // The caller wants to determine the map region size from ashmem. | 237 // The caller wants to determine the map region size from ashmem. |
| 237 bytes = ashmem_bytes; | 238 bytes = ashmem_bytes; |
| 238 // TODO(port): we set the created size here so that it is available in | 239 // TODO(port): we set the created size here so that it is available in |
| 239 // transport_dib_android.cc. We should use ashmem_get_size_region() | 240 // transport_dib_android.cc. We should use ashmem_get_size_region() |
| 240 // in transport_dib_android.cc. | 241 // in transport_dib_android.cc. |
| 241 created_size_ = bytes; | 242 created_size_ = bytes; |
| 242 } | 243 } |
| 243 #endif | 244 #endif |
| 244 | 245 |
| 246 DCHECK_EQ(static_cast<size_t>(0), | |
|
jar (doing other things)
2013/01/15 22:20:29
I assume this call is not made that often... so ho
Vitaly Buka (NO REVIEWS)
2013/01/15 22:58:54
it will return false because memory_ will be inval
| |
| 247 offset & (SysInfo::VMAllocationGranularity() - 1)); | |
|
Mark Mentovai
2013/01/15 22:26:02
My point was that this is sort of an implementatio
Vitaly Buka (NO REVIEWS)
2013/01/15 22:58:54
I don't believe we want crash on mmap returns null
| |
| 245 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), | 248 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), |
| 246 MAP_SHARED, mapped_file_, 0); | 249 MAP_SHARED, mapped_file_, offset); |
| 247 | 250 |
| 248 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL; | 251 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL; |
| 249 if (mmap_succeeded) { | 252 if (mmap_succeeded) { |
| 250 mapped_size_ = bytes; | 253 mapped_size_ = bytes; |
| 251 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 254 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 252 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 255 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 253 } else { | 256 } else { |
| 254 memory_ = NULL; | 257 memory_ = NULL; |
| 255 } | 258 } |
| 256 | 259 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 new_handle->fd = new_fd; | 383 new_handle->fd = new_fd; |
| 381 new_handle->auto_close = true; | 384 new_handle->auto_close = true; |
| 382 | 385 |
| 383 if (close_self) | 386 if (close_self) |
| 384 Close(); | 387 Close(); |
| 385 | 388 |
| 386 return true; | 389 return true; |
| 387 } | 390 } |
| 388 | 391 |
| 389 } // namespace base | 392 } // namespace base |
| OLD | NEW |