| 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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 SharedMemoryHandle SharedMemory::NULLHandle() { | 63 SharedMemoryHandle SharedMemory::NULLHandle() { |
| 64 return NULL; | 64 return NULL; |
| 65 } | 65 } |
| 66 | 66 |
| 67 // static | 67 // static |
| 68 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { | 68 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
| 69 DCHECK(handle != NULL); | 69 DCHECK(handle != NULL); |
| 70 ::CloseHandle(handle); | 70 ::CloseHandle(handle); |
| 71 } | 71 } |
| 72 | 72 |
| 73 bool SharedMemory::CreateAndMapAnonymous(uint32 size) { | 73 bool SharedMemory::CreateAndMapAnonymous(size_t size) { |
| 74 return CreateAnonymous(size) && Map(size); | 74 return CreateAnonymous(size) && Map(size); |
| 75 } | 75 } |
| 76 | 76 |
| 77 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 77 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
| 78 DCHECK(!options.executable); | 78 DCHECK(!options.executable); |
| 79 DCHECK(!mapped_file_); | 79 DCHECK(!mapped_file_); |
| 80 if (options.size == 0) | 80 if (options.size == 0) |
| 81 return false; | 81 return false; |
| 82 | 82 |
| 83 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 84 return false; |
| 85 |
| 83 // NaCl's memory allocator requires 0mod64K alignment and size for | 86 // NaCl's memory allocator requires 0mod64K alignment and size for |
| 84 // shared memory objects. To allow passing shared memory to NaCl, | 87 // shared memory objects. To allow passing shared memory to NaCl, |
| 85 // therefore we round the size actually created to the nearest 64K unit. | 88 // therefore we round the size actually created to the nearest 64K unit. |
| 86 // To avoid client impact, we continue to retain the size as the | 89 // To avoid client impact, we continue to retain the size as the |
| 87 // actual requested size. | 90 // actual requested size. |
| 88 uint32 rounded_size = (options.size + 0xffff) & ~0xffff; | 91 uint32 rounded_size = (options.size + 0xffff) & ~0xffff; |
| 89 if (rounded_size < options.size) | 92 if (rounded_size < options.size) |
| 90 return false; | 93 return false; |
| 91 name_ = ASCIIToWide(options.name == NULL ? "" : *options.name); | 94 name_ = ASCIIToWide(options.name == NULL ? "" : *options.name); |
| 92 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, | 95 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 mapped_file_ = OpenFileMapping( | 127 mapped_file_ = OpenFileMapping( |
| 125 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, false, | 128 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, false, |
| 126 name_.empty() ? NULL : name_.c_str()); | 129 name_.empty() ? NULL : name_.c_str()); |
| 127 if (mapped_file_ != NULL) { | 130 if (mapped_file_ != NULL) { |
| 128 // Note: size_ is not set in this case. | 131 // Note: size_ is not set in this case. |
| 129 return true; | 132 return true; |
| 130 } | 133 } |
| 131 return false; | 134 return false; |
| 132 } | 135 } |
| 133 | 136 |
| 134 bool SharedMemory::Map(uint32 bytes) { | 137 bool SharedMemory::Map(size_t bytes) { |
| 135 if (mapped_file_ == NULL) | 138 if (mapped_file_ == NULL) |
| 136 return false; | 139 return false; |
| 137 | 140 |
| 141 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max())) |
| 142 return false; |
| 143 |
| 138 memory_ = MapViewOfFile(mapped_file_, | 144 memory_ = MapViewOfFile(mapped_file_, |
| 139 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, bytes); | 145 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, bytes); |
| 140 if (memory_ != NULL) { | 146 if (memory_ != NULL) { |
| 141 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & | 147 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & |
| 142 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); | 148 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); |
| 143 return true; | 149 return true; |
| 144 } | 150 } |
| 145 return false; | 151 return false; |
| 146 } | 152 } |
| 147 | 153 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 void SharedMemory::Unlock() { | 225 void SharedMemory::Unlock() { |
| 220 DCHECK(lock_ != NULL); | 226 DCHECK(lock_ != NULL); |
| 221 ReleaseMutex(lock_); | 227 ReleaseMutex(lock_); |
| 222 } | 228 } |
| 223 | 229 |
| 224 SharedMemoryHandle SharedMemory::handle() const { | 230 SharedMemoryHandle SharedMemory::handle() const { |
| 225 return mapped_file_; | 231 return mapped_file_; |
| 226 } | 232 } |
| 227 | 233 |
| 228 } // namespace base | 234 } // namespace base |
| OLD | NEW |